Search in sources :

Example 46 with FutureCallback

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

the class TestHttpsEcho method testHttpEcho.

/**
   * Test that https-enabled server and client can speak plain HTTP as well.
   */
@Test
public void testHttpEcho() throws Exception {
    final EchoService client = new RestEchoClient(Bootstrap.createHttpURI(Bootstrap.getEchoURI()), _client);
    final String msg = "This is a simple http echo message";
    final FutureCallback<String> callback = new FutureCallback<String>();
    client.echo(msg, callback);
    Assert.assertEquals(callback.get(), msg);
}
Also used : EchoService(com.linkedin.r2.sample.echo.EchoService) RestEchoClient(com.linkedin.r2.sample.echo.rest.RestEchoClient) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 47 with FutureCallback

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

the class AbstractEchoServiceTest method testThrowingEchoService.

@Test
public void testThrowingEchoService() throws Exception {
    final EchoService client = getEchoClient(_client, Bootstrap.getThrowingEchoURI());
    final String msg = "This is a simple echo message";
    final FutureCallback<String> callback = new FutureCallback<String>();
    client.echo(msg, callback);
    try {
        callback.get();
        Assert.fail("Should have thrown an exception");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof RemoteInvocationException);
    }
}
Also used : EchoService(com.linkedin.r2.sample.echo.EchoService) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 48 with FutureCallback

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

the class AbstractEchoServiceTest method testBadRestURI.

@Test
public void testBadRestURI() {
    final EchoService client = getEchoClient(_client, URI.create("/unknown-service"));
    if (!(client instanceof RestEchoClient)) {
        return;
    }
    final String msg = "This is a simple echo message";
    final FutureCallback<String> callback = new FutureCallback<String>();
    client.echo(msg, callback);
    try {
        callback.get();
        Assert.fail("Should have thrown an exception");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof ExecutionException);
        Assert.assertTrue(e.getCause() instanceof RestException);
        RestException re = (RestException) e.getCause();
        Assert.assertEquals(re.getResponse().getStatus(), RestStatus.NOT_FOUND);
    }
}
Also used : EchoService(com.linkedin.r2.sample.echo.EchoService) RestEchoClient(com.linkedin.r2.sample.echo.rest.RestEchoClient) RestException(com.linkedin.r2.message.rest.RestException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test)

Example 49 with FutureCallback

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

the class AbstractEchoServiceTest method testFilterChainOnException.

@Test
public void testFilterChainOnException() throws Exception {
    final EchoService client = getEchoClient(_client, URI.create("/unknown-service"));
    final String msg = "This is a simple echo message";
    final FutureCallback<String> callback = new FutureCallback<String>();
    client.echo(msg, callback);
    try {
        callback.get();
        Assert.fail("Should have thrown an exception");
    } catch (Exception e) {
    // expected
    }
    // Make sure the server got its wire attribute
    Assert.assertEquals(_serverCaptureFilter.getRequest().get(_toServerKey), _toServerValue);
    // Make sure the client got its wire attribute, but not the server's wire attribute
    Assert.assertEquals(_clientCaptureFilter.getResponse().get(_toClientKey), _toClientValue);
    Assert.assertNull(_clientCaptureFilter.getResponse().get(_toServerKey));
}
Also used : EchoService(com.linkedin.r2.sample.echo.EchoService) FutureCallback(com.linkedin.common.callback.FutureCallback) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test)

Example 50 with FutureCallback

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

the class TestMultiplexedCallback method testMixed.

@Test
public void testMixed() 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>();
    TestRecord entity1 = fakeEntity(ID1);
    IndividualResponse ir1 = fakeIndividualResponse(entity1);
    IndividualResponse ir2 = fakeIndividualErrorResponse();
    MultiplexedResponseContent responseContent = new MultiplexedResponseContent().setResponses(new IndividualResponseMap(ImmutableMap.of(Integer.toString(ID1), ir1, Integer.toString(ID2), ir2)));
    MultiplexedCallback multiplexedCallback = new MultiplexedCallback(individualCallbacks, aggregatedCallback);
    multiplexedCallback.onSuccess(fakeRestResponse(responseContent));
    assertRestResponseEquals(callback1.get(), fakeRestResponse(entity1));
    RestException actualError = (RestException) getError(callback2);
    assertRestResponseEquals(actualError.getResponse(), fakeRestErrorResponse());
    MultiplexedResponse multiplexedResponse = aggregatedCallback.get();
    Assert.assertEquals(multiplexedResponse.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals(multiplexedResponse.getHeaders(), HEADERS);
}
Also used : MultiplexedResponseContent(com.linkedin.restli.common.multiplexer.MultiplexedResponseContent) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) IndividualResponse(com.linkedin.restli.common.multiplexer.IndividualResponse) FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) TestRecord(com.linkedin.restli.client.test.TestRecord) IndividualResponseMap(com.linkedin.restli.common.multiplexer.IndividualResponseMap) 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