use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestHttpClientFactory method testShutdownAfterClients.
@Test
public void testShutdownAfterClients() throws Exception {
NioEventLoopGroup eventLoop = new NioEventLoopGroup();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
HttpClientFactory factory = getHttpClientFactory(eventLoop, true, scheduler, true);
Server server = new HttpServerBuilder().build();
try {
server.start();
List<Client> clients = new ArrayList<Client>();
for (int i = 0; i < 1; i++) {
clients.add(new TransportClientAdapter(factory.getClient(Collections.<String, String>emptyMap()), true));
}
for (Client c : clients) {
RestRequest r = new RestRequestBuilder(new URI(URI)).build();
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
c.restRequest(r, futureCallback);
futureCallback.get(30, TimeUnit.SECONDS);
}
for (Client c : clients) {
FutureCallback<None> callback = new FutureCallback<None>();
c.shutdown(callback);
callback.get(30, TimeUnit.SECONDS);
}
FutureCallback<None> factoryShutdown = new FutureCallback<None>();
factory.shutdown(factoryShutdown);
factoryShutdown.get(30, TimeUnit.SECONDS);
Assert.assertTrue(eventLoop.awaitTermination(30, TimeUnit.SECONDS), "Failed to shut down event-loop");
Assert.assertTrue(scheduler.awaitTermination(30, TimeUnit.SECONDS), "Failed to shut down scheduler");
} finally {
server.stop();
}
}
use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestChannelPoolManagerFactorySharingConnection method makeRequestsWithClients.
/**
* Helper function that creates clientFactory, makes request sequentially, checks the result and shutdowns everything
*/
public void makeRequestsWithClients(boolean shareConnection, ClientGenerator clientGenerator, int expectedConnectionsWithSharing) throws Exception {
HttpClientFactory clientFactory = new HttpClientFactory.Builder().setShareConnection(shareConnection).build();
HttpServerBuilder.HttpServerStatsProvider httpServerStatsProvider = getHttpServerStatsProviderIgnoringOptions();
Server server = new HttpServerBuilder().serverStatsProvider(httpServerStatsProvider).build();
try {
server.start();
List<Client> clients = new ArrayList<>();
for (int i = 0; i < TRIALS; i++) {
clientGenerator.populate(clients, clientFactory);
}
for (Client c : clients) {
RestRequest r = new RestRequestBuilder(new URI(TestHttpClientFactory.URI)).build();
c.restRequest(r).get(30, TimeUnit.SECONDS);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
c.shutdown(shutdownCallback);
shutdownCallback.get(20, TimeUnit.SECONDS);
}
Assert.assertEquals(httpServerStatsProvider.requestCount(), NUMBER_OF_REQUESTS);
int expectedOpenedConnections = shareConnection ? expectedConnectionsWithSharing : OPENED_CONNECTIONS_WITHOUT_SHARING;
Assert.assertEquals(httpServerStatsProvider.clientConnections().size(), expectedOpenedConnections);
} finally {
server.stop();
}
// shutdown the client factory which will trigger the ChannelPoolManagerFactorySharingConnecion shutdown
FutureCallback<None> shutdownCallback = new FutureCallback<>();
clientFactory.shutdown(shutdownCallback);
shutdownCallback.get(10, TimeUnit.SECONDS);
}
use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestHttpClientFactory method testShutdownIOThread.
@Test(dataProvider = "configs")
public void testShutdownIOThread(boolean restOverStream, String protocolVersion) throws Exception {
EventLoopGroup eventLoop = new NioEventLoopGroup();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
ExecutorService callbackExecutor = Executors.newFixedThreadPool(1);
HttpClientFactory factory = getHttpClientFactory(eventLoop, true, scheduler, true, callbackExecutor, false);
CountDownLatch responseLatch = new CountDownLatch(1);
Server server = new HttpServerBuilder().responseLatch(responseLatch).build();
try {
server.start();
HashMap<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
Client client = new TransportClientAdapter(factory.getClient(properties), restOverStream);
URI uri = new URI(URI);
Future<RestResponse> responseFuture = client.restRequest(new RestRequestBuilder(uri).build());
FutureCallback<None> factoryShutdown = new FutureCallback<>();
factory.shutdown(factoryShutdown);
FutureCallback<None> clientShutdown = new FutureCallback<>();
client.shutdown(clientShutdown);
// Client and factory shutdowns are now pending. When we release the latch, the response will
// be returned, which causes the shutdowns to complete on the Netty IO thread that received the
// response.
responseLatch.countDown();
clientShutdown.get(60, TimeUnit.SECONDS);
factoryShutdown.get(60, TimeUnit.SECONDS);
} finally {
server.stop();
}
Assert.assertTrue(eventLoop.awaitTermination(30, TimeUnit.SECONDS), "Failed to shut down event-loop");
Assert.assertTrue(scheduler.awaitTermination(60, TimeUnit.SECONDS), "Failed to shut down scheduler");
callbackExecutor.shutdown();
}
use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestHttpClientFactory method testSuccessfulRequest.
@Test(dataProvider = "configsExpectedRequestCount")
public void testSuccessfulRequest(boolean restOverStream, String protocolVersion, int expectedRequests) throws Exception {
EventLoopGroup eventLoop = new NioEventLoopGroup();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
HttpClientFactory factory = getHttpClientFactory(eventLoop, true, scheduler, true);
HttpServerBuilder.HttpServerStatsProvider httpServerStatsProvider = new HttpServerBuilder.HttpServerStatsProvider();
Server server = new HttpServerBuilder().serverStatsProvider(httpServerStatsProvider).build();
try {
server.start();
List<Client> clients = new ArrayList<>();
int savedTimingKeyCount = TimingKey.getCount();
for (int i = 0; i < 100; i++) {
HashMap<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
clients.add(new TransportClientAdapter(factory.getClient(properties), restOverStream));
}
int addedTimingKeyCount = TimingKey.getCount() - savedTimingKeyCount;
// In current implementation, one client can have around 30 TimingKeys by default.
Assert.assertTrue(addedTimingKeyCount >= 30 * clients.size());
for (Client c : clients) {
RestRequest r = new RestRequestBuilder(new URI(URI)).build();
c.restRequest(r).get(30, TimeUnit.SECONDS);
}
Assert.assertEquals(httpServerStatsProvider.requestCount(), expectedRequests);
savedTimingKeyCount = TimingKey.getCount();
for (Client c : clients) {
FutureCallback<None> callback = new FutureCallback<>();
c.shutdown(callback);
callback.get(30, TimeUnit.SECONDS);
}
FutureCallback<None> factoryShutdown = new FutureCallback<>();
factory.shutdown(factoryShutdown);
factoryShutdown.get(30, TimeUnit.SECONDS);
int removedTimingKeyCount = savedTimingKeyCount - TimingKey.getCount();
Assert.assertEquals(addedTimingKeyCount, removedTimingKeyCount);
} finally {
server.stop();
}
}
use of com.linkedin.r2.testutils.server.HttpServerBuilder in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testNoResponseTimeout.
@Test(dataProvider = "noResponseClients")
public void testNoResponseTimeout(AbstractNettyStreamClient client) throws Exception {
CountDownLatch responseLatch = new CountDownLatch(1);
Server server = new HttpServerBuilder().responseLatch(responseLatch).build();
try {
server.start();
RestRequest r = new RestRequestBuilder(new URI(URL)).build();
FutureCallback<StreamResponse> cb = new FutureCallback<>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
// This timeout needs to be significantly larger than the getTimeout of the netty client;
// we're testing that the client will generate its own timeout
cb.get(30, TimeUnit.SECONDS);
Assert.fail("Get was supposed to time out");
} catch (TimeoutException e) {
// TimeoutException means the timeout for Future.get() elapsed and nothing happened.
// Instead, we are expecting our callback to be invoked before the future timeout
// with a timeout generated by the HttpNettyClient.
Assert.fail("Unexpected TimeoutException, should have been ExecutionException", e);
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
} finally {
responseLatch.countDown();
server.stop();
}
}
Aggregations