Search in sources :

Example 61 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestClientShutdown method testShutdown.

@Test
public void testShutdown() throws Exception {
    TransportClientFactory clientFactory = new HttpClientFactory.Builder().build();
    RestRequestBuilder builder = new RestRequestBuilder(_clientProvider.createHttpURI(_port, ECHO_URI));
    byte[] content = new byte[100];
    builder.setEntity(content);
    Future<RestResponse> future = _client.restRequest(builder.build());
    RestResponse response = future.get(30, TimeUnit.SECONDS);
    Assert.assertEquals(response.getEntity().copyBytes(), content);
    final FutureCallback<None> clientShutdownCallback = new FutureCallback<>();
    _client.shutdown(clientShutdownCallback);
    // we should catch those clients that do not shutdown properly in 5 seconds
    clientShutdownCallback.get(5000, TimeUnit.MILLISECONDS);
    final FutureCallback<None> factoryShutdownCallback = new FutureCallback<>();
    clientFactory.shutdown(factoryShutdownCallback);
    factoryShutdownCallback.get();
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) AbstractEchoServiceTest(test.r2.integ.clientserver.providers.AbstractEchoServiceTest) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 62 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestDisruptor method testRestErrorDisrupt.

@Test
public void testRestErrorDisrupt() throws Exception {
    final Map<String, Object> properties = new HashMap<>();
    properties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(REQUEST_TIMEOUT));
    final Client client = _clientProvider.createClient(FilterChains.empty(), properties);
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.error(REQUEST_LATENCY));
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    client.restRequest(new RestRequestBuilder(getHttpURI()).build(), requestContext, new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse result) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            success.set(e instanceof DisruptedException);
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
    Assert.assertTrue(success.get(), "Unexpected transport response");
}
Also used : HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DisruptedException(com.linkedin.r2.disruptor.DisruptedException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Client(com.linkedin.r2.transport.common.Client) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 63 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestHttpClient method testClient.

@Test
public void testClient() throws Exception {
    RestRequestBuilder rb = new RestRequestBuilder(getHttpUri(DISPATCHER_URI));
    rb.setMethod("GET");
    RestRequest request = rb.build();
    Future<RestResponse> f = _client.restRequest(request);
    // This will block
    RestResponse response = f.get();
    final ByteString entity = response.getEntity();
    if (entity != null) {
        System.out.println(entity.asString("UTF-8"));
    } else {
        System.out.println("NOTHING!");
    }
    assertEquals(response.getStatus(), 200);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 64 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestHttpClient method testSimpleURI.

@Test
public void testSimpleURI() throws Exception {
    // Note no trailing slash; the point of the test is to ensure this URI will
    // send a Request-URI of "/".
    URI uri = getHttpUri(null);
    RestRequestBuilder rb = new RestRequestBuilder(uri);
    rb.setMethod("GET");
    RestRequest request = rb.build();
    Future<RestResponse> f = _client.restRequest(request);
    // This will block
    RestResponse response = f.get();
    assertEquals(response.getStatus(), 200);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 65 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestServerTimeoutAsyncEvent method testServerTimeoutAfterResponding.

@Test
public void testServerTimeoutAfterResponding() throws Exception {
    Future<RestResponse> futureResponse = _client.restRequest(new RestRequestBuilder(getHttpUri(TIMEOUT_AFTER_SENDING_RESPONSE_SERVER_URI)).build());
    // server should timeout so get should succeed
    RestResponse response = futureResponse.get(ASYNC_EVENT_TIMEOUT * 2, TimeUnit.MILLISECONDS);
    Assert.assertEquals(response.getStatus(), RestStatus.OK);
    Assert.assertEquals(response.getEntity().length(), RESPONSE_SIZE_WRITTEN_SO_FAR);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Aggregations

RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)336 RestRequest (com.linkedin.r2.message.rest.RestRequest)290 Test (org.testng.annotations.Test)267 URI (java.net.URI)220 RestResponse (com.linkedin.r2.message.rest.RestResponse)192 RequestContext (com.linkedin.r2.message.RequestContext)155 ExecutionException (java.util.concurrent.ExecutionException)55 ByteString (com.linkedin.data.ByteString)46 FutureCallback (com.linkedin.common.callback.FutureCallback)43 RestException (com.linkedin.r2.message.rest.RestException)42 HashMap (java.util.HashMap)36 TimeoutException (java.util.concurrent.TimeoutException)29 AfterTest (org.testng.annotations.AfterTest)26 BeforeTest (org.testng.annotations.BeforeTest)26 Callback (com.linkedin.common.callback.Callback)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)25 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)21 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)20