use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestStreamResponse method testErrorWhileStreaming.
@Test
public void testErrorWhileStreaming() throws Exception {
HttpClientFactory clientFactory = new HttpClientFactory();
Map<String, String> clientProperties = new HashMap<String, String>();
clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, "1000");
Client client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), true);
StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, SERVER_ERROR_URI));
StreamRequest request = builder.build(EntityStreams.emptyStream());
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
final Callback<None> readerCallback = getReaderCallback(latch, error);
final BytesReader reader = new BytesReader(BYTE, readerCallback);
Callback<StreamResponse> callback = getCallback(status, readerCallback, reader);
client.streamRequest(request, callback);
latch.await(2000, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), RestStatus.OK);
Throwable throwable = error.get();
Assert.assertNotNull(throwable);
final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
client.shutdown(clientShutdownCallback);
clientShutdownCallback.get();
final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
clientFactory.shutdown(factoryShutdownCallback);
factoryShutdownCallback.get();
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpClientFactory method testShutdownNoTimeout.
@Test(dataProvider = "configs")
public void testShutdownNoTimeout(boolean restOverStream, String protocolVersion) 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 < 100; i++) {
HashMap<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
clients.add(new TransportClientAdapter(factory.getClient(properties), restOverStream));
}
for (Client c : clients) {
RestRequest r = new RestRequestBuilder(new URI(URI)).build();
c.restRequest(r).get(30, TimeUnit.SECONDS);
}
} finally {
server.stop();
}
FutureCallback<None> factoryShutdown = new FutureCallback<None>();
factory.shutdown(factoryShutdown);
try {
factoryShutdown.get(1, TimeUnit.SECONDS);
Assert.fail("Factory shutdown should have timed out");
} catch (TimeoutException e) {
// Expected
}
Assert.assertFalse(eventLoop.isShutdown(), "Boss should not be shut down");
Assert.assertFalse(scheduler.isShutdown(), "Scheduler should not be shut down");
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpClientFactory method testClientShutdownBeingCalledMultipleTimes.
@Test
public void testClientShutdownBeingCalledMultipleTimes() throws InterruptedException, ExecutionException, TimeoutException {
HttpClientFactory factory = new HttpClientFactory();
TransportClient client = factory.getClient(Collections.<String, Object>emptyMap());
// first shutdown call
FutureCallback<None> clientShutdown = new FutureCallback<None>();
client.shutdown(clientShutdown);
clientShutdown.get(30, TimeUnit.SECONDS);
// second shutdown call
clientShutdown = new FutureCallback<None>();
client.shutdown(clientShutdown);
try {
clientShutdown.get(30, TimeUnit.SECONDS);
Assert.fail("should have thrown exception on the second shutdown call.");
} catch (ExecutionException ex) {
Assert.assertTrue(ex.getCause() instanceof IllegalStateException);
}
FutureCallback<None> shutdownCallback = new FutureCallback<None>();
factory.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpClientFactory method testShutdownBeforeClients.
@Test(dataProvider = "configs")
public void testShutdownBeforeClients(boolean restOverStream, String protocolVersion) 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 < 100; i++) {
HashMap<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
clients.add(new TransportClientAdapter(factory.getClient(properties), restOverStream));
}
for (Client c : clients) {
RestRequest r = new RestRequestBuilder(new URI(URI)).build();
c.restRequest(r).get(30, TimeUnit.SECONDS);
}
FutureCallback<None> factoryShutdown = new FutureCallback<None>();
factory.shutdown(factoryShutdown);
for (Client c : clients) {
FutureCallback<None> callback = new FutureCallback<None>();
c.shutdown(callback);
callback.get(30, TimeUnit.SECONDS);
}
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.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpClientFactory method testShutdownTimeout.
@Test(dataProvider = "configs")
public void testShutdownTimeout(boolean restOverStream, String protocolVersion) 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 < 100; i++) {
HashMap<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
clients.add(new TransportClientAdapter(factory.getClient(properties), restOverStream));
}
for (Client c : clients) {
RestRequest r = new RestRequestBuilder(new URI(URI)).build();
c.restRequest(r).get(30, TimeUnit.SECONDS);
}
FutureCallback<None> factoryShutdown = new FutureCallback<None>();
factory.shutdown(factoryShutdown, 1, TimeUnit.SECONDS);
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();
}
}
Aggregations