Search in sources :

Example 1 with FutureCallback

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();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Server(org.eclipse.jetty.server.Server) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 2 with FutureCallback

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());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestException(com.linkedin.r2.message.rest.RestException) RestResponse(com.linkedin.r2.message.rest.RestResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 3 with FutureCallback

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());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestException(com.linkedin.r2.message.rest.RestException) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestResponse(com.linkedin.r2.message.rest.RestResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 4 with FutureCallback

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();
    }
}
Also used : Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) FutureCallback(com.linkedin.common.callback.FutureCallback) AfterClass(org.testng.annotations.AfterClass)

Example 5 with FutureCallback

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());
    }
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Client(com.linkedin.r2.transport.common.Client) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Aggregations

FutureCallback (com.linkedin.common.callback.FutureCallback)241 Test (org.testng.annotations.Test)176 None (com.linkedin.common.util.None)137 ExecutionException (java.util.concurrent.ExecutionException)69 RequestContext (com.linkedin.r2.message.RequestContext)58 RestRequest (com.linkedin.r2.message.rest.RestRequest)58 RestResponse (com.linkedin.r2.message.rest.RestResponse)50 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)43 CountDownLatch (java.util.concurrent.CountDownLatch)33 ArrayList (java.util.ArrayList)32 TimeoutException (java.util.concurrent.TimeoutException)32 HashMap (java.util.HashMap)31 URI (java.net.URI)30 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)28 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)28 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)23 IOException (java.io.IOException)23 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)22 RestException (com.linkedin.r2.message.rest.RestException)20 EchoService (com.linkedin.r2.sample.echo.EchoService)20