ParallelTest.java
/*
* Copyright (c) 2016, 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.netty.connector;
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 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 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 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 NettyConnectorProvider());
}
@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");
}
}
}