use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpNettyClient method testBadAddress.
@Test
public void testBadAddress() throws InterruptedException, IOException, TimeoutException {
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(30000).setIdleTimeout(10000).setShutdownTimeout(500).buildRest();
RestRequest r = new RestRequestBuilder(URI.create("http://this.host.does.not.exist.linkedin.com")).build();
FutureCallback<RestResponse> cb = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(cb);
client.restRequest(r, new RequestContext(), new HashMap<String, String>(), callback);
try {
cb.get(30, TimeUnit.SECONDS);
Assert.fail("Get was supposed to fail");
} catch (ExecutionException e) {
verifyCauseChain(e, RemoteInvocationException.class, UnknownHostException.class);
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestHttpNettyClient method testNoResponseTimeout.
@Test
public void testNoResponseTimeout() throws InterruptedException, IOException {
TestServer testServer = new TestServer();
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(500).setIdleTimeout(10000).setShutdownTimeout(500).buildRest();
RestRequest r = new RestRequestBuilder(testServer.getNoResponseURI()).build();
FutureCallback<RestResponse> cb = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(cb);
client.restRequest(r, new RequestContext(), new HashMap<String, String>(), callback);
try {
// 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);
}
testServer.shutdown();
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestClientShutdown method testShutdown.
@Test(dataProvider = "configs")
public void testShutdown(boolean clientROS, boolean serverROS, String protocolVersion) throws Exception {
_clientFactory = new HttpClientFactory();
Map<String, String> clientProperties = new HashMap<String, String>();
// very long shutdown timeout
clientProperties.put(HttpClientFactory.HTTP_SHUTDOWN_TIMEOUT, "60000");
clientProperties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
_client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), clientROS);
TransportDispatcher dispatcher = new TransportDispatcherBuilder().addRestHandler(ECHO_URI, new EchoHandler()).build();
_server = new HttpServerFactory(HttpJettyServer.ServletType.RAP).createH2cServer(PORT, dispatcher, serverROS);
try {
_server.start();
RestRequestBuilder builder = new RestRequestBuilder(URI.create("http://localhost:" + PORT + ECHO_URI));
byte[] content = new byte[100];
builder.setEntity(content);
Future<RestResponse> future = _client.restRequest(builder.build());
RestResponse response = future.get(30, TimeUnit.SECONDS);
Assert.assertEquals(response.getEntity().copyBytes(), content);
final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
_client.shutdown(clientShutdownCallback);
// we should catch those clients that do not shutdown properly in 5 seconds
clientShutdownCallback.get(5000, TimeUnit.MILLISECONDS);
final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
_clientFactory.shutdown(factoryShutdownCallback);
factoryShutdownCallback.get();
} finally {
if (_server != null) {
_server.stop();
_server.waitForStop();
}
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestCompressionEcho method testResponseCompression.
@Test(dataProvider = "compressionEchoData")
public void testResponseCompression(Client client, long bytes) throws InterruptedException, TimeoutException, ExecutionException {
StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, ECHO_URI)));
BytesWriter writer = new BytesWriter(bytes, BYTE);
StreamRequest request = builder.build(EntityStreams.newEntityStream(writer));
// add operation to enable sending accept encoding
RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.OPERATION, "get");
final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
client.streamRequest(request, requestContext, callback);
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
final FutureCallback<None> readerCallback = new FutureCallback<None>();
final BytesReader reader = new BytesReader(BYTE, readerCallback);
response.getEntityStream().setReader(reader);
readerCallback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(reader.getTotalBytes(), bytes);
Assert.assertTrue(reader.allBytesCorrect());
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestRequestCompression method testNoCompression.
@Test(dataProvider = "noCompressionData")
public void testNoCompression(Client client) throws InterruptedException, TimeoutException, ExecutionException {
StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, NO_COMPRESSION_URI)));
BytesWriter writer = new BytesWriter(THRESHOLD - 1, BYTE);
StreamRequest request = builder.build(EntityStreams.newEntityStream(writer));
final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
client.streamRequest(request, callback);
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
}
Aggregations