Search in sources :

Example 21 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project incubator-gobblin by apache.

the class HttpTestUtils method assertEqual.

public static void assertEqual(RestRequestBuilder actual, RestRequestBuilder expect) throws IOException {
    // Check entity
    ByteString actualEntity = actual.getEntity();
    ByteString expectedEntity = expect.getEntity();
    if (actualEntity == null) {
        Assert.assertTrue(expectedEntity == null);
    } else {
        Assert.assertEquals(actualEntity.length(), expectedEntity.length());
        Assert.assertEquals(actualEntity.asString(StandardCharsets.UTF_8), expectedEntity.asString(StandardCharsets.UTF_8));
    }
    // Check request
    RestRequest actualRequest = actual.build();
    RestRequest expectedRequest = expect.build();
    Assert.assertEquals(actualRequest.getMethod(), expectedRequest.getMethod());
    Assert.assertEquals(actualRequest.getURI().toString(), expectedRequest.getURI().toString());
    Map<String, String> actualHeaders = actualRequest.getHeaders();
    Map<String, String> expectedHeaders = expectedRequest.getHeaders();
    Assert.assertEquals(actualHeaders.size(), expectedHeaders.size());
    for (String key : actualHeaders.keySet()) {
        Assert.assertEquals(actualHeaders.get(key), expectedHeaders.get(key));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString)

Example 22 with RestRequestBuilder

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

the class RetryClientTest method testRestRetryNoAvailableHosts.

@Test
public void testRestRetryNoAvailableHosts() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/retry2"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
    DynamicClient dynamicClient = new DynamicClient(balancer, null);
    RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, false);
    URI uri = URI.create("d2://retryService?arg1=empty&arg2=empty");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    DegraderTrackerClientTest.TestCallback<RestResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
    client.restRequest(restRequest, restCallback);
    assertNull(restCallback.t);
    assertNotNull(restCallback.e);
    assertTrue(restCallback.e.toString().contains("retryService is in a bad state"));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 23 with RestRequestBuilder

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

the class RewriteLoadBalancerClientTest method testWithEverything.

@Test
public void testWithEverything() {
    URI uri = URI.create("http://username:password@test.linkedin.com:9876/test");
    String serviceName = "HistoryService";
    TestClient wrappedClient = new TestClient();
    RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
    assertEquals(client.getUri(), uri);
    assertEquals(client.getServiceName(), serviceName);
    RestRequest restRequest = new RestRequestBuilder(URI.create("d2://HistoryService/getCube?bar=baz#fragId")).build();
    Map<String, String> restWireAttrs = new HashMap<>();
    TestTransportCallback<RestResponse> restCallback = new TestTransportCallback<>();
    client.restRequest(restRequest, new RequestContext(), restWireAttrs, restCallback);
    assertFalse(restCallback.response.hasError());
    assertEquals(wrappedClient.restRequest.getHeaders(), restRequest.getHeaders());
    assertEquals(wrappedClient.restRequest.getEntity(), restRequest.getEntity());
    assertEquals(wrappedClient.restRequest.getMethod(), restRequest.getMethod());
    assertEquals(wrappedClient.restRequest.getURI(), URI.create("http://username:password@test.linkedin.com:9876/test/getCube?bar=baz#fragId"));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) TestTransportCallback(com.linkedin.d2.balancer.clients.DegraderTrackerClientTest.TestTransportCallback) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 24 with RestRequestBuilder

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

the class RewriteLoadBalancerClientTest method testClient.

@Test(groups = { "small", "back-end" })
public void testClient() throws URISyntaxException {
    URI uri = URI.create("http://test.linkedin.com/test");
    String serviceName = "HistoryService";
    TestClient wrappedClient = new TestClient();
    RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
    assertEquals(client.getUri(), uri);
    assertEquals(client.getServiceName(), serviceName);
    RestRequest restRequest = new RestRequestBuilder(URI.create("d2://HistoryService/getCube")).build();
    Map<String, String> restWireAttrs = new HashMap<>();
    TestTransportCallback<RestResponse> restCallback = new TestTransportCallback<>();
    client.restRequest(restRequest, new RequestContext(), restWireAttrs, restCallback);
    assertFalse(restCallback.response.hasError());
    assertEquals(wrappedClient.restRequest.getHeaders(), restRequest.getHeaders());
    assertEquals(wrappedClient.restRequest.getEntity(), restRequest.getEntity());
    assertEquals(wrappedClient.restRequest.getMethod(), restRequest.getMethod());
    // check the rewrite
    assertEquals(wrappedClient.restRequest.getURI(), URI.create("http://test.linkedin.com/test/getCube"));
    assertEquals(wrappedClient.restWireAttrs, restWireAttrs);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) TestTransportCallback(com.linkedin.d2.balancer.clients.DegraderTrackerClientTest.TestTransportCallback) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 25 with RestRequestBuilder

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

the class TestBackupRequestsClient method testStatsConsumerRemoveOne.

@Test(dataProvider = "isD2Async")
public void testStatsConsumerRemoveOne(boolean isD2Async) throws Exception {
    AtomicReference<ServiceProperties> serviceProperties = new AtomicReference<>();
    TestBackupRequestsStrategyStatsConsumer statsConsumer = new TestBackupRequestsStrategyStatsConsumer();
    serviceProperties.set(createServiceProperties(null));
    BackupRequestsClient client = createClient(serviceProperties::get, statsConsumer, isD2Async);
    URI uri = URI.create("d2://testService");
    RestRequest restRequest = new RestRequestBuilder(uri).setEntity(CONTENT).build();
    RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    Future<RestResponse> response = client.restRequest(restRequest, requestContext);
    assertEquals(response.get().getStatus(), 200);
    List<StatsConsumerEvent> events = statsConsumer.getEvents();
    assertEquals(events.size(), 0);
    serviceProperties.set(createServiceProperties(Arrays.asList(createBackupRequestsConfiguration(5, "get"), createBackupRequestsConfiguration(1, "batch_get"))));
    requestContext = new RequestContext();
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    response = client.restRequest(restRequest, requestContext);
    assertEquals(response.get().getStatus(), 200);
    events = statsConsumer.getEvents();
    assertEquals(events.size(), 2);
    assertEquals(events.get(0).isEventAdd(), true);
    assertEquals(events.get(0).getService(), SERVICE_NAME);
    assertEquals(events.get(0).getOperation(), "get");
    BackupRequestsStrategyStatsProvider statsProvider1 = events.get(0).getStatsProvider();
    assertNotNull(statsProvider1);
    assertEquals(events.get(1).isEventAdd(), true);
    assertEquals(events.get(1).getService(), SERVICE_NAME);
    assertEquals(events.get(1).getOperation(), "batch_get");
    BackupRequestsStrategyStatsProvider statsProvider2 = events.get(1).getStatsProvider();
    assertNotNull(statsProvider2);
    serviceProperties.set(createServiceProperties(Arrays.asList(createBackupRequestsConfiguration(5, "get"))));
    requestContext = new RequestContext();
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    response = client.restRequest(restRequest, requestContext);
    assertEquals(response.get().getStatus(), 200);
    events = statsConsumer.getEvents();
    assertEquals(events.size(), 3);
    assertEquals(events.get(2).isEventAdd(), false);
    assertEquals(events.get(2).getService(), SERVICE_NAME);
    assertEquals(events.get(2).getOperation(), "batch_get");
    BackupRequestsStrategyStatsProvider removedStatsProvider = events.get(2).getStatsProvider();
    assertNotNull(removedStatsProvider);
    assertSame(statsProvider2, removedStatsProvider);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) RestRequest(com.linkedin.r2.message.rest.RestRequest) BackupRequestsStrategyStatsProvider(com.linkedin.d2.backuprequests.BackupRequestsStrategyStatsProvider) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

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