Add verigraph code base
[parser.git] / verigraph / src / main / java / it / polito / grpc / test / MultiThreadTest.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9
10 package it.polito.grpc.test;
11
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Random;
17 import java.util.concurrent.Callable;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.Future;
22
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.FixMethodOrder;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 import org.junit.runners.MethodSorters;
31
32 import com.fasterxml.jackson.core.JsonParseException;
33 import com.fasterxml.jackson.databind.JsonMappingException;
34
35 import io.grpc.verigraph.GraphGrpc;
36 import io.grpc.verigraph.NewGraph;
37 import io.grpc.verigraph.NodeGrpc;
38 import it.polito.escape.verify.client.VerifyClientException;
39 import it.polito.grpc.Client;
40 import it.polito.grpc.Service;
41
42 /**
43  * Unit tests for gRPC project.
44  * For testing concurrency on server side.
45  */
46 @RunWith(JUnit4.class)
47 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
48 public class MultiThreadTest {
49         private Service server;
50         private Client client;
51
52         @Before
53         public void setUp() throws Exception {
54                 client = new Client("localhost" , 50051);
55                 server = new Service(50051);
56
57                 server.start();
58         }
59
60         @After
61         public void tearDown() throws Exception {
62                 server.stop();
63                 client.shutdown();
64         }
65
66         private void testUpdateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
67                 GraphGrpc retrieveGraphResponse =client.getGraph(1);
68
69                 UpdateGraph task = new UpdateGraph(client, 1, retrieveGraphResponse);
70
71                 List<MultiThreadTest.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
72                 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
73
74                 List<Future<NewGraph>> futures = executorService.invokeAll(tasks);
75                 List<Boolean> resultList = new ArrayList<Boolean>(futures.size());
76
77                 // Check for exceptions
78                 for (Future<NewGraph> future : futures) {
79                         // Throws an exception if an exception was thrown by the task.
80                         resultList.add(future.get().getSuccess());
81                 }
82                 // Validate the dimensions
83                 Assert.assertEquals(threadCount, futures.size());
84
85                 List<Boolean> expectedList = new ArrayList<Boolean>(threadCount);
86                 for (int i = 1; i <= threadCount; i++) {
87                         expectedList.add(true);
88                 }
89                 // Validate expected results
90                 Assert.assertEquals(expectedList, resultList);
91         }
92
93         private void testUpdateGraph(final int threadCount) throws Exception {
94                 GraphGrpc retrieveGraph = client.getGraph(2L);
95                 NodeGrpc nodeToEdit = Client.createNodeGrpc("client",
96                                                                 "endpoint",
97                                                                 null,
98                                                                 Client.createConfigurationGrpc(null, null, "client", ""));
99
100                 GraphGrpc graphToUpdate = GraphGrpc.newBuilder(retrieveGraph).addNode(nodeToEdit).build();
101
102                 String graphAsString = graphToUpdate.toString();
103
104                 UpdateGraph task = new UpdateGraph(client, 2, graphToUpdate);
105
106                 List<MultiThreadTest.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
107                 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
108
109                 List<Future<NewGraph>> futures = executorService.invokeAll(tasks);
110                 List<String> resultList = new ArrayList<String>(futures.size());
111
112                 // Check for exceptions
113                 for (Future<NewGraph> future : futures) {
114                         // Throws an exception if an exception was thrown by the task.
115                         GraphGrpc graphReceived = future.get().getGraph();
116                         NodeGrpc node = NodeGrpc.newBuilder(graphReceived.getNode(0)).setId(0).build();
117                         GraphGrpc graph = GraphGrpc.newBuilder(graphReceived).setNode(0, node).build();
118                         resultList.add(graph.toString());
119                 }
120                 // Validate dimensions
121                 Assert.assertEquals(threadCount, futures.size());
122
123                 List<String> expectedList = new ArrayList<String>(threadCount);
124                 for (int i = 1; i <= threadCount; i++) {
125                         expectedList.add(graphAsString);
126                 }
127                 // Validate expected results
128                 Assert.assertEquals(expectedList, resultList);
129         }
130
131         private void testCreateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException {
132
133                 GraphGrpc graph = GraphGrpc.newBuilder().build();
134
135                 CreateGraph task = new CreateGraph(client, graph);
136
137                 List<MultiThreadTest.CreateGraph> tasks = Collections.nCopies(threadCount, task);
138                 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
139
140                 List<Future<Boolean>> futures = executorService.invokeAll(tasks);
141                 List<Boolean> resultList = new ArrayList<Boolean>(futures.size());
142
143                 // Check for exceptions
144                 for (Future<Boolean> future : futures) {
145                         // Throws an exception if an exception was thrown by the task.
146                         resultList.add(future.get());
147                 }
148                 // Validate the IDs
149                 Assert.assertEquals(threadCount, futures.size());
150
151                 List<Boolean> expectedList = new ArrayList<Boolean>(threadCount);
152                 for (int i = 1; i <= threadCount; i++) {
153                         expectedList.add(true);
154                 }
155                 // Validate expected results
156                 Assert.assertEquals(expectedList, resultList);
157         }
158
159         private int randInt(int min, int max){
160                 Random rand = new Random();
161             int randomNum = rand.nextInt((max - min) + 1) + min;
162             return randomNum;
163         }
164
165         @Test
166         public void updateGraphStatusCheck() throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
167                 testUpdateGraphStatus(64);
168         }
169
170         @Test
171         public void updateGraphResponseCheck() throws Exception {
172                 testUpdateGraph(16);
173         }
174
175         @Test
176         public void createGraphStatusCheck() throws JsonParseException, JsonMappingException, InterruptedException, ExecutionException, IOException {
177                 testCreateGraphStatus(8);
178         }
179
180         class UpdateGraph implements Callable<NewGraph> {
181
182                 private Client  verifyClient;
183
184                 private int                             graphId;
185
186                 private GraphGrpc                       graph;
187
188                 public UpdateGraph(Client verifyClient, int graphId, GraphGrpc graph) {
189                         this.graphId = graphId;
190                         this.graph = graph;
191                         this.verifyClient = verifyClient;
192                 }
193
194                 @Override
195                 public NewGraph call() throws Exception {
196                         Thread.sleep(randInt(0,2000));
197                         return this.verifyClient.updateGraph(this.graphId, this.graph);
198                 }
199         }
200
201         class CreateGraph implements Callable<Boolean> {
202
203                 private Client  verifyClient;
204
205                 private GraphGrpc               graph;
206
207                 public CreateGraph(Client verifyClient, GraphGrpc graph) {
208                         this.graph = graph;
209                         this.verifyClient = verifyClient;
210                 }
211
212                 @Override
213                 public Boolean call() throws Exception {
214                         Thread.sleep(randInt(0,2000));
215                         return this.verifyClient.createGraph(this.graph).getSuccess();
216                 }
217
218         }
219
220 }