1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
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;
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;
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;
31 import com.fasterxml.jackson.core.JsonParseException;
32 import com.fasterxml.jackson.databind.JsonMappingException;
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;
42 * Unit tests for gRPC project.
43 * For testing concurrency on server side.
45 @RunWith(JUnit4.class)
46 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
47 public class MultiThreadTest {
48 private Service server;
49 private Client client;
52 public void setUp() throws Exception {
53 client = new Client("localhost" , 50051);
54 server = new Service(50051);
60 public void tearDown() throws Exception {
65 private void testUpdateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
66 GraphGrpc retrieveGraphResponse =client.getGraph(1);
68 UpdateGraph task = new UpdateGraph(client, 1, retrieveGraphResponse);
70 List<MultiThreadTest.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
71 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
73 List<Future<NewGraph>> futures = executorService.invokeAll(tasks);
74 List<Boolean> resultList = new ArrayList<Boolean>(futures.size());
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());
81 // Validate the dimensions
82 Assert.assertEquals(threadCount, futures.size());
84 List<Boolean> expectedList = new ArrayList<Boolean>(threadCount);
85 for (int i = 1; i <= threadCount; i++) {
86 expectedList.add(true);
88 // Validate expected results
89 Assert.assertEquals(expectedList, resultList);
92 private void testUpdateGraph(final int threadCount) throws Exception {
93 GraphGrpc retrieveGraph = client.getGraph(2L);
95 NodeGrpc nodeToEdit = Client.createNodeGrpc("client",
98 Client.createConfigurationGrpc(null, null, "client", ""));
100 GraphGrpc graphToUpdate = GraphGrpc.newBuilder(retrieveGraph).addNode(nodeToEdit).build();
102 String graphAsString = graphToUpdate.toString();
104 UpdateGraph task = new UpdateGraph(client, 2, graphToUpdate);
106 List<MultiThreadTest.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
107 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
109 List<Future<NewGraph>> futures = executorService.invokeAll(tasks);
110 List<String> resultList = new ArrayList<String>(futures.size());
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());
120 // Validate dimensions
121 Assert.assertEquals(threadCount, futures.size());
123 List<String> expectedList = new ArrayList<String>(threadCount);
124 for (int i = 1; i <= threadCount; i++) {
125 expectedList.add(graphAsString);
127 // Validate expected results
128 Assert.assertEquals(expectedList, resultList);
131 private void testCreateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException {
133 GraphGrpc graph = GraphGrpc.newBuilder().build();
135 CreateGraph task = new CreateGraph(client, graph);
137 List<MultiThreadTest.CreateGraph> tasks = Collections.nCopies(threadCount, task);
138 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
140 List<Future<Boolean>> futures = executorService.invokeAll(tasks);
141 List<Boolean> resultList = new ArrayList<Boolean>(futures.size());
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());
149 Assert.assertEquals(threadCount, futures.size());
151 List<Boolean> expectedList = new ArrayList<Boolean>(threadCount);
152 for (int i = 1; i <= threadCount; i++) {
153 expectedList.add(true);
155 // Validate expected results
156 Assert.assertEquals(expectedList, resultList);
159 private int randInt(int min, int max){
160 Random rand = new Random();
161 int randomNum = rand.nextInt((max - min) + 1) + min;
166 public void updateGraphStatusCheck() throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
167 testUpdateGraphStatus(64);
171 public void updateGraphResponseCheck() throws Exception {
176 public void createGraphStatusCheck() throws JsonParseException, JsonMappingException, InterruptedException, ExecutionException, IOException {
177 testCreateGraphStatus(8);
180 class UpdateGraph implements Callable<NewGraph> {
182 private Client verifyClient;
186 private GraphGrpc graph;
188 public UpdateGraph(Client verifyClient, int graphId, GraphGrpc graph) {
189 this.graphId = graphId;
191 this.verifyClient = verifyClient;
195 public NewGraph call() throws Exception {
196 Thread.sleep(randInt(0,2000));
197 return this.verifyClient.updateGraph(this.graphId, this.graph);
201 class CreateGraph implements Callable<Boolean> {
203 private Client verifyClient;
205 private GraphGrpc graph;
207 public CreateGraph(Client verifyClient, GraphGrpc graph) {
209 this.verifyClient = verifyClient;
213 public Boolean call() throws Exception {
214 Thread.sleep(randInt(0,2000));
215 return this.verifyClient.createGraph(this.graph).getSuccess();