Search in sources :

Example 51 with FutureCallback

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

the class TestMultiplexedCallback method testError.

@Test
public void testError() throws Exception {
    FutureCallback<RestResponse> callback1 = new FutureCallback<RestResponse>();
    FutureCallback<RestResponse> callback2 = new FutureCallback<RestResponse>();
    ImmutableMap<Integer, Callback<RestResponse>> individualCallbacks = ImmutableMap.<Integer, Callback<RestResponse>>of(ID1, callback1, ID2, callback2);
    FutureCallback<MultiplexedResponse> aggregatedCallback = new FutureCallback<MultiplexedResponse>();
    MultiplexedCallback multiplexedCallback = new MultiplexedCallback(individualCallbacks, aggregatedCallback);
    RestLiDecodingException exception = new RestLiDecodingException(null, null);
    multiplexedCallback.onError(exception);
    Assert.assertSame(getError(callback1), exception);
    Assert.assertSame(getError(callback2), exception);
    Assert.assertSame(getError(aggregatedCallback), exception);
}
Also used : FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 52 with FutureCallback

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

the class TestMultiplexerIntegration method twoSequentialCalls.

@Test
public void twoSequentialCalls() throws Exception {
    GetRequest<Greeting> request1 = new GreetingsCallbackBuilders().get().id(1L).build();
    FutureCallback<Response<Greeting>> muxCallback1 = new FutureCallback<Response<Greeting>>();
    FutureCallback<Response<Greeting>> directCallback1 = new FutureCallback<Response<Greeting>>();
    GetRequest<Greeting> request2 = new GreetingsCallbackBuilders().get().id(2L).build();
    FutureCallback<Response<Greeting>> muxCallback2 = new FutureCallback<Response<Greeting>>();
    FutureCallback<Response<Greeting>> directCallback2 = new FutureCallback<Response<Greeting>>();
    MultiplexedRequest multiplexedRequest = MultiplexedRequestBuilder.createSequentialRequest().addRequest(request1, muxCallback1).addRequest(request2, muxCallback2).build();
    getClient().sendRequest(multiplexedRequest);
    getClient().sendRequest(request1, directCallback1);
    getClient().sendRequest(request2, directCallback2);
    assertEqualResponses(muxCallback1, directCallback1);
    assertEqualResponses(muxCallback2, directCallback2);
}
Also used : Response(com.linkedin.restli.client.Response) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) FutureCallback(com.linkedin.common.callback.FutureCallback) GreetingsCallbackBuilders(com.linkedin.restli.examples.greetings.client.GreetingsCallbackBuilders) RestLiIntegrationTest(com.linkedin.restli.examples.RestLiIntegrationTest) Test(org.testng.annotations.Test)

Example 53 with FutureCallback

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

the class TestMultiplexerIntegration method twoSequentialCallsWithOneError.

@Test
public void twoSequentialCallsWithOneError() throws Exception {
    GetRequest<Greeting> request1 = new GreetingsCallbackBuilders().get().id(1L).build();
    FutureCallback<Response<Greeting>> muxCallback1 = new FutureCallback<Response<Greeting>>();
    FutureCallback<Response<Greeting>> directCallback1 = new FutureCallback<Response<Greeting>>();
    GetRequest<Greeting> request2 = new GreetingsCallbackBuilders().get().id(Long.MAX_VALUE).build();
    FutureCallback<Response<Greeting>> muxCallback2 = new FutureCallback<Response<Greeting>>();
    FutureCallback<Response<Greeting>> directCallback2 = new FutureCallback<Response<Greeting>>();
    MultiplexedRequest multiplexedRequest = MultiplexedRequestBuilder.createSequentialRequest().addRequest(request1, muxCallback1).addRequest(request2, muxCallback2).build();
    getClient().sendRequest(multiplexedRequest);
    getClient().sendRequest(request1, directCallback1);
    getClient().sendRequest(request2, directCallback2);
    assertEqualResponses(muxCallback1, directCallback1);
    assertEqualErrors(muxCallback2, directCallback2);
}
Also used : Response(com.linkedin.restli.client.Response) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) FutureCallback(com.linkedin.common.callback.FutureCallback) GreetingsCallbackBuilders(com.linkedin.restli.examples.greetings.client.GreetingsCallbackBuilders) RestLiIntegrationTest(com.linkedin.restli.examples.RestLiIntegrationTest) Test(org.testng.annotations.Test)

Example 54 with FutureCallback

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

the class TestMultiplexerIntegration method twoParallelCalls.

@Test
public void twoParallelCalls() throws Exception {
    GetRequest<Greeting> request1 = new GreetingsCallbackBuilders().get().id(1L).build();
    FutureCallback<Response<Greeting>> muxCallback1 = new FutureCallback<Response<Greeting>>();
    FutureCallback<Response<Greeting>> directCallback1 = new FutureCallback<Response<Greeting>>();
    GetRequest<Greeting> request2 = new GreetingsCallbackBuilders().get().id(2L).build();
    FutureCallback<Response<Greeting>> muxCallback2 = new FutureCallback<Response<Greeting>>();
    FutureCallback<Response<Greeting>> directCallback2 = new FutureCallback<Response<Greeting>>();
    MultiplexedRequest multiplexedRequest = MultiplexedRequestBuilder.createParallelRequest().addRequest(request1, muxCallback1).addRequest(request2, muxCallback2).build();
    getClient().sendRequest(multiplexedRequest);
    getClient().sendRequest(request1, directCallback1);
    getClient().sendRequest(request2, directCallback2);
    assertEqualResponses(muxCallback1, directCallback1);
    assertEqualResponses(muxCallback2, directCallback2);
}
Also used : Response(com.linkedin.restli.client.Response) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) FutureCallback(com.linkedin.common.callback.FutureCallback) GreetingsCallbackBuilders(com.linkedin.restli.examples.greetings.client.GreetingsCallbackBuilders) RestLiIntegrationTest(com.linkedin.restli.examples.RestLiIntegrationTest) Test(org.testng.annotations.Test)

Example 55 with FutureCallback

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

the class TestRequestCompression method testUpdate.

@Test(dataProvider = "requestData")
public void testUpdate(CompressionConfig requestCompressionConfig, String supportedEncodings, RestliRequestOptions restliRequestOptions, int messageLength, String testHelpHeader) throws RemoteInvocationException, CloneNotSupportedException, InterruptedException, ExecutionException, TimeoutException {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
    Map<String, CompressionConfig> requestCompressionConfigs = new HashMap<String, CompressionConfig>();
    if (requestCompressionConfig != null) {
        requestCompressionConfigs.put(SERVICE_NAME, requestCompressionConfig);
    }
    HttpClientFactory httpClientFactory = new HttpClientFactory(FilterChains.empty(), new NioEventLoopGroup(), true, executor, true, null, false, AbstractJmxManager.NULL_JMX_MANAGER, // The default compression threshold is between small and large.
    500, requestCompressionConfigs);
    Map<String, String> properties = new HashMap<String, String>();
    properties.put(HttpClientFactory.HTTP_REQUEST_CONTENT_ENCODINGS, supportedEncodings);
    properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
    TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
    RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
    RootBuilderWrapper<Long, Greeting> builders = new RootBuilderWrapper<Long, Greeting>(new GreetingsRequestBuilders(restliRequestOptions));
    // GET
    Request<Greeting> request = builders.get().id(1L).build();
    ResponseFuture<Greeting> future = client.sendRequest(request);
    Response<Greeting> greetingResponse = future.getResponse();
    String response1 = greetingResponse.getEntity().getMessage();
    Assert.assertNotNull(response1);
    // POST
    Greeting greeting = new Greeting(greetingResponse.getEntity().data().copy());
    char[] As = new char[messageLength];
    Arrays.fill(As, 'A');
    String message = new String(As);
    greeting.setMessage(message);
    Request<EmptyRecord> writeRequest = builders.update().id(1L).input(greeting).setHeader(TEST_HELP_HEADER, testHelpHeader).build();
    client.sendRequest(writeRequest).getResponse();
    // GET again, to verify that our POST worked.
    Request<Greeting> request2 = builders.get().id(1L).build();
    ResponseFuture<Greeting> future2 = client.sendRequest(request2);
    String response2 = future2.getResponse().getEntity().getMessage();
    Assert.assertEquals(response2, message);
    FutureCallback<None> callback1 = new FutureCallback<None>();
    client.shutdown(callback1);
    callback1.get(30, TimeUnit.SECONDS);
    FutureCallback<None> callback2 = new FutureCallback<None>();
    httpClientFactory.shutdown(callback2);
    callback2.get(30, TimeUnit.SECONDS);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EmptyRecord(com.linkedin.restli.common.EmptyRecord) HashMap(java.util.HashMap) GreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) FutureCallback(com.linkedin.common.callback.FutureCallback) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) RestClient(com.linkedin.restli.client.RestClient) None(com.linkedin.common.util.None) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) 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