-
Notifications
You must be signed in to change notification settings - Fork 340
/
ParallelTest.java
133 lines (116 loc) · 5.14 KB
/
ParallelTest.java
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
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.helidon.connector;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Tests the parallel execution of multiple requests.
*
* @author Stepan Kopriva
*/
public class ParallelTest extends JerseyTest {
private static final Logger LOGGER = Logger.getLogger(ParallelTest.class.getName());
private static final int PARALLEL_CLIENTS = 10;
private static final String PATH = "test";
private static final AtomicInteger receivedCounter = new AtomicInteger(0);
private static final AtomicInteger resourceCounter = new AtomicInteger(0);
private static final CyclicBarrier startBarrier = new CyclicBarrier(PARALLEL_CLIENTS + 1);
private static final CountDownLatch doneLatch = new CountDownLatch(PARALLEL_CLIENTS);
@Path(PATH)
public static class MyResource {
@GET
public String get() {
sleep();
resourceCounter.addAndGet(1);
return "GET";
}
private void sleep() {
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(ParallelTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
protected Application configure() {
return new ResourceConfig(ParallelTest.MyResource.class);
}
@Override
protected void configureClient(ClientConfig config) {
config.connectorProvider(new HelidonConnectorProvider());
}
@Test
public void testParallel() throws BrokenBarrierException, InterruptedException, TimeoutException {
final ScheduledExecutorService executor = Executors.newScheduledThreadPool(PARALLEL_CLIENTS);
try {
final WebTarget target = target();
for (int i = 1; i <= PARALLEL_CLIENTS; i++) {
final int id = i;
executor.submit(new Runnable() {
@Override
public void run() {
try {
startBarrier.await();
Response response;
response = target.path(PATH).request().get();
assertEquals("GET", response.readEntity(String.class));
receivedCounter.incrementAndGet();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
LOGGER.log(Level.WARNING, "Client thread " + id + " interrupted.", ex);
} catch (BrokenBarrierException ex) {
LOGGER.log(Level.INFO, "Client thread " + id + " failed on broken barrier.", ex);
} catch (Throwable t) {
t.printStackTrace();
LOGGER.log(Level.WARNING, "Client thread " + id + " failed on unexpected exception.", t);
} finally {
doneLatch.countDown();
}
}
});
}
startBarrier.await(1, TimeUnit.SECONDS);
assertTrue(doneLatch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS),
"Waiting for clients to finish has timed out.");
assertEquals(PARALLEL_CLIENTS, resourceCounter.get(), "Resource counter");
assertEquals(PARALLEL_CLIENTS, receivedCounter.get(), "Received counter");
} finally {
executor.shutdownNow();
Assertions.assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS), "Executor termination");
}
}
}