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 *******************************************************************************/
10 package it.polito.escape.verify.test;
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;
23 import javax.ws.rs.core.Response;
25 import org.junit.Assert;
26 import org.junit.Test;
28 import com.fasterxml.jackson.core.JsonParseException;
29 import com.fasterxml.jackson.databind.JsonMappingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
32 import it.polito.escape.verify.client.VerifyClient;
33 import it.polito.escape.verify.client.VerifyClientException;
34 import it.polito.escape.verify.model.Configuration;
35 import it.polito.escape.verify.model.Graph;
36 import it.polito.escape.verify.model.Node;
38 public class MultiThreadedTestCase {
40 private void testUpdateGraphStatus(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
41 final VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verify/api");
43 Response retrieveGraphResponse = verifyClient.retrieveGraph(1);
44 String responseString = retrieveGraphResponse.readEntity(String.class);
45 System.out.println(responseString);
47 Graph graph = new ObjectMapper().readValue(responseString, Graph.class);
49 UpdateGraph task = new UpdateGraph(verifyClient, 1, graph);
51 List<MultiThreadedTestCase.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
52 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
54 List<Future<Response>> futures = executorService.invokeAll(tasks);
55 List<Integer> resultList = new ArrayList<Integer>(futures.size());
57 // Check for exceptions
58 for (Future<Response> future : futures) {
59 // Throws an exception if an exception was thrown by the task.
60 resultList.add(future.get().getStatus());
62 // Validate the dimensions
63 Assert.assertEquals(threadCount, futures.size());
64 List<Integer> expectedList = new ArrayList<Integer>(threadCount);
65 for (int i = 1; i <= threadCount; i++) {
66 expectedList.add(200);
68 // Validate expected results
69 Assert.assertEquals(expectedList, resultList);
72 private void testUpdateGraph(final int threadCount) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
73 final VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verify/api");
75 Response retrieveGraphResponse = verifyClient.retrieveGraph(2L);
76 String responseString = retrieveGraphResponse.readEntity(String.class);
77 System.out.println(responseString);
79 Graph graph = new ObjectMapper().readValue(responseString, Graph.class);
81 Node nodeToEdit = graph.getNodes().get(1L);
82 nodeToEdit.setFunctional_type("endpoint");
83 nodeToEdit.setConfiguration(new Configuration(nodeToEdit.getName(), "", new ObjectMapper().createArrayNode()));
85 String graphAsString = new ObjectMapper().writeValueAsString(graph);
86 System.out.println(graphAsString);
88 UpdateGraph task = new UpdateGraph(verifyClient, 2, graph);
90 List<MultiThreadedTestCase.UpdateGraph> tasks = Collections.nCopies(threadCount, task);
91 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
93 List<Future<Response>> futures = executorService.invokeAll(tasks);
94 List<String> resultList = new ArrayList<String>(futures.size());
96 // Check for exceptions
97 for (Future<Response> future : futures) {
98 // Throws an exception if an exception was thrown by the task.
99 resultList.add(future.get().readEntity(String.class));
101 // Validate dimensions
102 Assert.assertEquals(threadCount, futures.size());
104 List<String> expectedList = new ArrayList<String>(threadCount);
105 for (int i = 1; i <= threadCount; i++) {
106 expectedList.add(graphAsString);
108 // Validate expected results
109 Assert.assertEquals(expectedList, resultList);
112 private void testCreateGraphStatus(final int threadCount, Graph graph) throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException {
113 final VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verify/api");
115 CreateGraph task = new CreateGraph(verifyClient, graph);
117 List<MultiThreadedTestCase.CreateGraph> tasks = Collections.nCopies(threadCount, task);
118 ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
120 List<Future<Response>> futures = executorService.invokeAll(tasks);
121 List<Integer> resultList = new ArrayList<Integer>(futures.size());
123 // Check for exceptions
124 for (Future<Response> future : futures) {
125 // Throws an exception if an exception was thrown by the task.
126 resultList.add(future.get().getStatus());
129 Assert.assertEquals(threadCount, futures.size());
131 List<Integer> expectedList = new ArrayList<Integer>(threadCount);
132 for (int i = 1; i <= threadCount; i++) {
133 expectedList.add(201);
135 // Validate expected results
136 Assert.assertEquals(expectedList, resultList);
139 private int randInt(int min, int max){
140 Random rand = new Random();
141 int randomNum = rand.nextInt((max - min) + 1) + min;
146 public void updateGraphStatusCheck() throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
147 testUpdateGraphStatus(64);
151 public void updateGraphResponseCheck() throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException, VerifyClientException {
156 public void createGraphStatusCheck() throws JsonParseException, JsonMappingException, InterruptedException, ExecutionException, IOException {
157 Graph graph = new Graph();
158 testCreateGraphStatus(8, graph);
161 class UpdateGraph implements Callable<Response> {
163 private VerifyClient verifyClient;
169 public UpdateGraph(VerifyClient verifyClient, int graphId, Graph graph) {
170 this.graphId = graphId;
172 this.verifyClient = verifyClient;
176 public Response call() throws Exception {
177 Thread.sleep(randInt(0,2000));
178 Response updateGraphResponse = this.verifyClient.updateGraph(this.graphId, this.graph);
179 return updateGraphResponse;
183 class CreateGraph implements Callable<Response> {
185 private VerifyClient verifyClient;
189 public CreateGraph(VerifyClient verifyClient, Graph graph) {
191 this.verifyClient = verifyClient;
195 public Response call() throws Exception {
196 Thread.sleep(randInt(0,2000));
197 return this.verifyClient.createGraph(this.graph);