Support TOSCA in verigraph (gRPC service)
[parser.git] / verigraph / src / it / polito / verigraph / 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 package it.polito.verigraph.grpc.test;
10
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Random;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.Future;
21
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.FixMethodOrder;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 import org.junit.runners.MethodSorters;
30
31 import com.fasterxml.jackson.core.JsonParseException;
32 import com.fasterxml.jackson.databind.JsonMappingException;
33
34 import it.polito.verigraph.grpc.GraphGrpc;
35 import it.polito.verigraph.grpc.NewGraph;
36 import it.polito.verigraph.grpc.NodeGrpc;
37 import it.polito.verigraph.client.VerifyClientException;
38 import it.polito.verigraph.grpc.client.Client;
39 import it.polito.verigraph.grpc.server.Service;
40
41 /**
42  * Unit tests for gRPC project.
43  * For testing concurrency on server side.
44  */
45 @RunWith(JUnit4.class)
46 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
47 public class MultiThreadTest {
48     private Service server;
49     private Client client;
50
51     @Before
52     public void setUp() throws Exception {
53         client = new Client("localhost" , 50051);
54         server = new Service(50051);
55
56         server.start();
57     }
58
59     @After
60     public void tearDown() throws Exception {
61         server.stop();
62         client.shutdown();
63     }
64
65     private void testUpdateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
66         GraphGrpc retrieveGraphResponse =client.getGraph(1);
67
68         UpdateGraph task = new UpdateGraph(client, 1, retrieveGraphResponse);
69
70         List<MultiThreadTest.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
71         ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
72
73         List<Future<NewGraph>> futures = executorService.invokeAll(tasks);
74         List<Boolean> resultList = new ArrayList<Boolean>(futures.size());
75
76         // Check for exceptions
77         for (Future<NewGraph> future : futures) {
78             // Throws an exception if an exception was thrown by the task.
79             resultList.add(future.get().getSuccess());
80         }
81         // Validate the dimensions
82         Assert.assertEquals(threadCount, futures.size());
83
84         List<Boolean> expectedList = new ArrayList<Boolean>(threadCount);
85         for (int i = 1; i <= threadCount; i++) {
86             expectedList.add(true);
87         }
88         // Validate expected results
89         Assert.assertEquals(expectedList, resultList);
90     }
91
92     private void testUpdateGraph(final int threadCount) throws Exception {
93         GraphGrpc retrieveGraph = client.getGraph(2L);
94
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 }