use of com.linkedin.common.callback.FutureCallback 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.common.callback.FutureCallback in project rest.li by linkedin.
the class RestClientTest method testRestLiRemoteInvocationException.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestOptions")
public void testRestLiRemoteInvocationException(SendRequestOption option, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, TimeoutException, InterruptedException, RestLiDecodingException {
final int HTTP_CODE = 404;
final String ERR_MSG = "WHOOPS!";
RestClient client = mockClient(HTTP_CODE, ERR_MSG, protocolVersion);
Request<EmptyRecord> request = mockRequest(EmptyRecord.class, versionOption);
RequestBuilder<Request<EmptyRecord>> requestBuilder = mockRequestBuilder(request);
FutureCallback<Response<EmptyRecord>> callback = new FutureCallback<Response<EmptyRecord>>();
try {
sendRequest(option, client, request, requestBuilder, callback);
Long l = timeoutOption._l;
TimeUnit timeUnit = timeoutOption._timeUnit;
Response<EmptyRecord> response = l == null ? callback.get() : callback.get(l, timeUnit);
Assert.fail("Should have thrown");
} catch (ExecutionException e) {
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof RemoteInvocationException, "Expected RemoteInvocationException not " + cause.getClass().getName());
RemoteInvocationException rlre = (RemoteInvocationException) cause;
Assert.assertTrue(rlre.getMessage().startsWith("Received error " + HTTP_CODE + " from server"));
Throwable rlCause = rlre.getCause();
Assert.assertTrue(rlCause instanceof RestException, "Excepted RestException not " + rlCause.getClass().getName());
RestException rle = (RestException) rlCause;
Assert.assertEquals(ERR_MSG, rle.getResponse().getEntity().asString("UTF-8"));
Assert.assertEquals(HTTP_CODE, rle.getResponse().getStatus());
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class RestClientTest method testRestLiResponseExceptionCallback.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestOptions")
public void testRestLiResponseExceptionCallback(SendRequestOption option, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, TimeoutException, InterruptedException, RestLiDecodingException {
final String ERR_KEY = "someErr";
final String ERR_VALUE = "WHOOPS!";
final String ERR_MSG = "whoops2";
final int HTTP_CODE = 400;
final int APP_CODE = 666;
RestClient client = mockClient(ERR_KEY, ERR_VALUE, ERR_MSG, HTTP_CODE, APP_CODE, protocolVersion, errorResponseHeaderName);
Request<EmptyRecord> request = mockRequest(EmptyRecord.class, versionOption);
RequestBuilder<Request<EmptyRecord>> requestBuilder = mockRequestBuilder(request);
FutureCallback<Response<EmptyRecord>> callback = new FutureCallback<Response<EmptyRecord>>();
try {
sendRequest(option, client, request, requestBuilder, callback);
Long l = timeoutOption._l;
TimeUnit timeUnit = timeoutOption._timeUnit;
Response<EmptyRecord> response = l == null ? callback.get() : callback.get(l, timeUnit);
Assert.fail("Should have thrown");
} catch (ExecutionException e) {
// New
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof RestLiResponseException, "Expected RestLiResponseException not " + cause.getClass().getName());
RestLiResponseException rlre = (RestLiResponseException) cause;
Assert.assertEquals(HTTP_CODE, rlre.getStatus());
Assert.assertEquals(ERR_VALUE, rlre.getErrorDetails().get(ERR_KEY));
Assert.assertEquals(APP_CODE, rlre.getServiceErrorCode());
Assert.assertEquals(ERR_MSG, rlre.getServiceErrorMessage());
// Old
Assert.assertTrue(cause instanceof RestException, "Expected RestException not " + cause.getClass().getName());
RestException re = (RestException) cause;
RestResponse r = re.getResponse();
ErrorResponse er = new EntityResponseDecoder<ErrorResponse>(ErrorResponse.class).decodeResponse(r).getEntity();
Assert.assertEquals(HTTP_CODE, r.getStatus());
Assert.assertEquals(ERR_VALUE, er.getErrorDetails().data().getString(ERR_KEY));
Assert.assertEquals(APP_CODE, er.getServiceErrorCode().intValue());
Assert.assertEquals(ERR_MSG, er.getMessage());
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestRestCompressionEcho method tearDown.
@AfterClass
public void tearDown() throws Exception {
for (Client client : _clients) {
final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
client.shutdown(clientShutdownCallback);
clientShutdownCallback.get();
}
for (TransportClientFactory factory : _clientFactories) {
final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
factory.shutdown(factoryShutdownCallback);
factoryShutdownCallback.get();
}
if (_server != null) {
_server.stop();
_server.waitForStop();
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestResponseCompression method testResponseCompression.
private void testResponseCompression(URI uri, long bytes, String acceptEncoding, final StreamingCompressor compressor) throws InterruptedException, TimeoutException, ExecutionException {
for (Client client : clients()) {
StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, uri)));
builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
StreamRequest request = builder.build(EntityStreams.emptyStream());
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);
final FutureCallback<None> readerCallback = new FutureCallback<None>();
final BytesReader reader = new BytesReader(BYTE, readerCallback);
final EntityStream decompressedStream = compressor.inflate(response.getEntityStream());
decompressedStream.setReader(reader);
readerCallback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(reader.getTotalBytes(), bytes);
Assert.assertTrue(reader.allBytesCorrect());
}
}
Aggregations