Search in sources :

Example 36 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestComplexKeysResource method testBatchGetEntityEmpty.

@SuppressWarnings("deprecation")
public void testBatchGetEntityEmpty(BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> builder) throws Exception {
    final Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> request = builder.build();
    final FutureCallback<RestResponse> callback = new FutureCallback<RestResponse>();
    getClient().sendRestRequest(request, new RequestContext(), callback);
    final RestResponse result = callback.get();
    final DataMap responseMap = DataMapUtils.readMap(result);
    final DataMap resultsMap = responseMap.getDataMap(BatchKVResponse.RESULTS);
    Assert.assertNotNull(resultsMap, "Response does not contain results map");
    Assert.assertTrue(resultsMap.isEmpty());
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) Message(com.linkedin.restli.examples.greetings.api.Message) RestResponse(com.linkedin.r2.message.rest.RestResponse) RequestContext(com.linkedin.r2.message.RequestContext) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) FutureCallback(com.linkedin.common.callback.FutureCallback) DataMap(com.linkedin.data.DataMap)

Example 37 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 38 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)

Example 39 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestResponseCompression method testEncodingNotAcceptable.

public void testEncodingNotAcceptable(String acceptEncoding) throws TimeoutException, InterruptedException {
    for (Client client : clients()) {
        StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, SMALL_URI)));
        if (acceptEncoding != null) {
            builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
        }
        StreamRequest request = builder.build(EntityStreams.emptyStream());
        final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
        client.streamRequest(request, callback);
        try {
            final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
            Assert.fail("Should have thrown exception when encoding is not acceptable");
        } catch (ExecutionException e) {
            Throwable t = e.getCause();
            Assert.assertTrue(t instanceof StreamException);
            StreamResponse response = ((StreamException) t).getResponse();
            Assert.assertEquals(response.getStatus(), HttpConstants.NOT_ACCEPTABLE);
        }
    }
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Client(com.linkedin.r2.transport.common.Client) ExecutionException(java.util.concurrent.ExecutionException) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) FutureCallback(com.linkedin.common.callback.FutureCallback) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) StreamException(com.linkedin.r2.message.stream.StreamException)

Example 40 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestRestCompressionEcho method testResponseCompression.

@Test(dataProvider = "compressionEchoData")
public void testResponseCompression(Client client, long bytes) throws InterruptedException, TimeoutException, ExecutionException {
    RestRequestBuilder builder = new RestRequestBuilder((Bootstrap.createHttpURI(PORT, ECHO_URI)));
    byte[] content = new byte[(int) bytes];
    for (int i = 0; i < bytes; i++) {
        content[i] = (byte) (i % 256);
    }
    RestRequest request = builder.setEntity(content).build();
    final FutureCallback<RestResponse> callback = new FutureCallback<RestResponse>();
    RequestContext requestContext = new RequestContext();
    // OPERATION is required to enabled response compression
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    client.restRequest(request, requestContext, callback);
    final RestResponse response = callback.get(60, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), RestStatus.OK);
    Assert.assertEquals(response.getEntity().copyBytes(), content);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

FutureCallback (com.linkedin.common.callback.FutureCallback)152 Test (org.testng.annotations.Test)116 None (com.linkedin.common.util.None)77 RestRequest (com.linkedin.r2.message.rest.RestRequest)50 ExecutionException (java.util.concurrent.ExecutionException)50 RequestContext (com.linkedin.r2.message.RequestContext)47 RestResponse (com.linkedin.r2.message.rest.RestResponse)43 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)36 HashMap (java.util.HashMap)26 CountDownLatch (java.util.concurrent.CountDownLatch)25 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)22 ByteString (com.linkedin.data.ByteString)21 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)21 URI (java.net.URI)21 TimeoutException (java.util.concurrent.TimeoutException)21 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)20 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)16 ArrayList (java.util.ArrayList)16 RestException (com.linkedin.r2.message.rest.RestException)15 AsyncSharedPoolImpl (com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl)14