summaryrefslogtreecommitdiffstats
path: root/verigraph/src/main/java/it/polito/grpc/test/MultiThreadTest.java
blob: 5b76bea9a4ee0abb9da6bb76bd0b174a35dd91c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*******************************************************************************
 * Copyright (c) 2017 Politecnico di Torino and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/

package it.polito.grpc.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.MethodSorters;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.grpc.verigraph.GraphGrpc;
import io.grpc.verigraph.NewGraph;
import io.grpc.verigraph.NodeGrpc;
import it.polito.escape.verify.client.VerifyClientException;
import it.polito.grpc.Client;
import it.polito.grpc.Service;

/**
 * Unit tests for gRPC project.
 * For testing concurrency on server side.
 */
@RunWith(JUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MultiThreadTest {
	private Service server;
	private Client client;

	@Before
	public void setUp() throws Exception {
		client = new Client("localhost" , 50051);
		server = new Service(50051);

		server.start();
	}

	@After
	public void tearDown() throws Exception {
		server.stop();
		client.shutdown();
	}

	private void testUpdateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
		GraphGrpc retrieveGraphResponse =client.getGraph(1);

		UpdateGraph task = new UpdateGraph(client, 1, retrieveGraphResponse);

		List<MultiThreadTest.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
		ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

		List<Future<NewGraph>> futures = executorService.invokeAll(tasks);
		List<Boolean> resultList = new ArrayList<Boolean>(futures.size());

		// Check for exceptions
		for (Future<NewGraph> future : futures) {
			// Throws an exception if an exception was thrown by the task.
			resultList.add(future.get().getSuccess());
		}
		// Validate the dimensions
		Assert.assertEquals(threadCount, futures.size());

		List<Boolean> expectedList = new ArrayList<Boolean>(threadCount);
		for (int i = 1; i <= threadCount; i++) {
			expectedList.add(true);
		}
		// Validate expected results
		Assert.assertEquals(expectedList, resultList);
	}

	private void testUpdateGraph(final int threadCount) throws Exception {
		GraphGrpc retrieveGraph = client.getGraph(2L);
		NodeGrpc nodeToEdit = Client.createNodeGrpc("client",
								"endpoint",
								null,
								Client.createConfigurationGrpc(null, null, "client", ""));

		GraphGrpc graphToUpdate = GraphGrpc.newBuilder(retrieveGraph).addNode(nodeToEdit).build();

		String graphAsString = graphToUpdate.toString();

		UpdateGraph task = new UpdateGraph(client, 2, graphToUpdate);

		List<MultiThreadTest.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
		ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

		List<Future<NewGraph>> futures = executorService.invokeAll(tasks);
		List<String> resultList = new ArrayList<String>(futures.size());

		// Check for exceptions
		for (Future<NewGraph> future : futures) {
			// Throws an exception if an exception was thrown by the task.
			GraphGrpc graphReceived = future.get().getGraph();
			NodeGrpc node = NodeGrpc.newBuilder(graphReceived.getNode(0)).setId(0).build();
			GraphGrpc graph = GraphGrpc.newBuilder(graphReceived).setNode(0, node).build();
			resultList.add(graph.toString());
		}
		// Validate dimensions
		Assert.assertEquals(threadCount, futures.size());

		List<String> expectedList = new ArrayList<String>(threadCount);
		for (int i = 1; i <= threadCount; i++) {
			expectedList.add(graphAsString);
		}
		// Validate expected results
		Assert.assertEquals(expectedList, resultList);
	}

	private void testCreateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException {

		GraphGrpc graph = GraphGrpc.newBuilder().build();

		CreateGraph task = new CreateGraph(client, graph);

		List<MultiThreadTest.CreateGraph> tasks = Collections.nCopies(threadCount, task);
		ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

		List<Future<Boolean>> futures = executorService.invokeAll(tasks);
		List<Boolean> resultList = new ArrayList<Boolean>(futures.size());

		// Check for exceptions
		for (Future<Boolean> future : futures) {
			// Throws an exception if an exception was thrown by the task.
			resultList.add(future.get());
		}
		// Validate the IDs
		Assert.assertEquals(threadCount, futures.size());

		List<Boolean> expectedList = new ArrayList<Boolean>(threadCount);
		for (int i = 1; i <= threadCount; i++) {
			expectedList.add(true);
		}
		// Validate expected results
		Assert.assertEquals(expectedList, resultList);
	}

	private int randInt(int min, int max){
		Random rand = new Random();
	    int randomNum = rand.nextInt((max - min) + 1) + min;
	    return randomNum;
	}

	@Test
	public void updateGraphStatusCheck() throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
		testUpdateGraphStatus(64);
	}

	@Test
	public void updateGraphResponseCheck() throws Exception {
		testUpdateGraph(16);
	}

	@Test
	public void createGraphStatusCheck() throws JsonParseException, JsonMappingException, InterruptedException, ExecutionException, IOException {
		testCreateGraphStatus(8);
	}

	class UpdateGraph implements Callable<NewGraph> {

		private Client	verifyClient;

		private int				graphId;

		private GraphGrpc			graph;

		public UpdateGraph(Client verifyClient, int graphId, GraphGrpc graph) {
			this.graphId = graphId;
			this.graph = graph;
			this.verifyClient = verifyClient;
		}

		@Override
		public NewGraph call() throws Exception {
			Thread.sleep(randInt(0,2000));
			return this.verifyClient.updateGraph(this.graphId, this.graph);
		}
	}

	class CreateGraph implements Callable<Boolean> {

		private Client	verifyClient;

		private GraphGrpc		graph;

		public CreateGraph(Client verifyClient, GraphGrpc graph) {
			this.graph = graph;
			this.verifyClient = verifyClient;
		}

		@Override
		public Boolean call() throws Exception {
			Thread.sleep(randInt(0,2000));
			return this.verifyClient.createGraph(this.graph).getSuccess();
		}

	}

}