Search in sources :

Example 1 with LoadBalancerMock

use of com.linkedin.d2.balancer.clients.stub.LoadBalancerMock in project rest.li by linkedin.

the class DynamicClientTest method testUnavailable.

@Test(groups = { "small", "back-end" }, dataProvider = "restOverStreamSwitch")
public void testUnavailable(boolean restOverStream) throws URISyntaxException {
    LoadBalancerMock balancer = new LoadBalancerMock(true);
    DynamicClient client = new DynamicClient(balancer, null, restOverStream);
    URI uri = URI.create("d2://test");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    TestCallback<RestResponse> restCallback = new TestCallback<>();
    client.restRequest(restRequest, restCallback);
    assertNotNull(restCallback.e);
    assertTrue(restCallback.e instanceof ServiceUnavailableException);
    assertNull(restCallback.t);
    Facilities facilities = client.getFacilities();
    assertNull(facilities, "facilities should be null");
}
Also used : LoadBalancerMock(com.linkedin.d2.balancer.clients.stub.LoadBalancerMock) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) TestCallback(com.linkedin.d2.balancer.clients.DegraderTrackerClientTest.TestCallback) URI(java.net.URI) Facilities(com.linkedin.d2.balancer.Facilities) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) Test(org.testng.annotations.Test)

Example 2 with LoadBalancerMock

use of com.linkedin.d2.balancer.clients.stub.LoadBalancerMock in project rest.li by linkedin.

the class DynamicClientTest method testClient.

@Test(groups = { "small", "back-end" }, dataProvider = "restOverStreamSwitch")
@SuppressWarnings("deprecation")
public void testClient(boolean restOverStream) throws URISyntaxException {
    LoadBalancerMock balancer = new LoadBalancerMock(false);
    DirectoryProvider dirProvider = new DirectoryProviderMock();
    KeyMapperProvider keyMapperProvider = new KeyMapperProviderMock();
    ClientFactoryProvider clientFactoryProvider = Mockito.mock(ClientFactoryProvider.class);
    Facilities facilities = new DelegatingFacilities(dirProvider, keyMapperProvider, clientFactoryProvider);
    DynamicClient client = new DynamicClient(balancer, facilities, restOverStream);
    URI uri = URI.create("d2://test");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    TestCallback<RestResponse> restCallback = new TestCallback<>();
    client.restRequest(restRequest, restCallback);
    assertNull(restCallback.e);
    assertNotNull(restCallback.t);
    Facilities myFacilities = client.getFacilities();
    assertNotNull(facilities, "facilities should not be null");
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) KeyMapperProvider(com.linkedin.d2.balancer.util.KeyMapperProvider) ClientFactoryProvider(com.linkedin.d2.balancer.util.ClientFactoryProvider) TestCallback(com.linkedin.d2.balancer.clients.DegraderTrackerClientTest.TestCallback) URI(java.net.URI) Facilities(com.linkedin.d2.balancer.Facilities) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) LoadBalancerMock(com.linkedin.d2.balancer.clients.stub.LoadBalancerMock) DirectoryProviderMock(com.linkedin.d2.balancer.clients.stub.DirectoryProviderMock) RestRequest(com.linkedin.r2.message.rest.RestRequest) DirectoryProvider(com.linkedin.d2.balancer.util.DirectoryProvider) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) KeyMapperProviderMock(com.linkedin.d2.balancer.clients.stub.KeyMapperProviderMock) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) Test(org.testng.annotations.Test)

Example 3 with LoadBalancerMock

use of com.linkedin.d2.balancer.clients.stub.LoadBalancerMock in project rest.li by linkedin.

the class DynamicClientTest method testShutdown.

@Test(groups = { "small", "back-end" })
public void testShutdown() throws URISyntaxException, InterruptedException {
    LoadBalancerMock balancer = new LoadBalancerMock(true);
    DynamicClient client = new DynamicClient(balancer, null, true);
    final CountDownLatch latch = new CountDownLatch(1);
    assertFalse(balancer.shutdown);
    client.shutdown(new Callback<None>() {

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onSuccess(None t) {
            latch.countDown();
        }
    });
    if (!latch.await(5, TimeUnit.SECONDS)) {
        fail("unable to shut down dynamic client");
    }
    assertTrue(balancer.shutdown);
}
Also used : LoadBalancerMock(com.linkedin.d2.balancer.clients.stub.LoadBalancerMock) CountDownLatch(java.util.concurrent.CountDownLatch) None(com.linkedin.common.util.None) Test(org.testng.annotations.Test)

Example 4 with LoadBalancerMock

use of com.linkedin.d2.balancer.clients.stub.LoadBalancerMock in project rest.li by linkedin.

the class RequestTimeoutClientTest method testRequestTimeoutAllowed.

/**
 * Check that the timeout are the expected ones
 */
@Test(groups = { "small" }, dataProvider = "allCombinations3x")
@SuppressWarnings("deprecation")
public void testRequestTimeoutAllowed(boolean isHigherThanDefault, boolean ignoreTimeoutIfHigher, int expectedTimeout) throws Exception {
    LoadBalancerSimulator.ClockedExecutor clockedExecutor = new LoadBalancerSimulator.ClockedExecutor();
    LoadBalancerMock balancer = new LoadBalancerMock(false, true, clockedExecutor);
    DirectoryProvider dirProvider = new DirectoryProviderMock();
    KeyMapperProvider keyMapperProvider = new KeyMapperProviderMock();
    ClientFactoryProvider clientFactoryProvider = Mockito.mock(ClientFactoryProvider.class);
    Facilities facilities = new DelegatingFacilities(dirProvider, keyMapperProvider, clientFactoryProvider);
    D2Client client = new DynamicClient(balancer, facilities, true);
    URI uri = URI.create("d2://test");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    client = new RequestTimeoutClient(client, balancer, clockedExecutor);
    RequestContext requestContext = new RequestContext();
    int requestTimeout = isHigherThanDefault ? DEFAULT_REQUEST_TIMEOUT + 100 : DEFAULT_REQUEST_TIMEOUT - 100;
    DegraderTrackerClientTest.TestCallback<RestResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
    requestContext.putLocalAttr(R2Constants.REQUEST_TIMEOUT, requestTimeout);
    if (ignoreTimeoutIfHigher) {
        requestContext.putLocalAttr(R2Constants.REQUEST_TIMEOUT_IGNORE_IF_HIGHER_THAN_DEFAULT, ignoreTimeoutIfHigher);
    }
    client.restRequest(restRequest, requestContext, restCallback);
    clockedExecutor.run(expectedTimeout - 10).get();
    Assert.assertFalse(checkTimeoutFired(restCallback));
    checkRequestTimeoutOrViewSet(requestContext);
    clockedExecutor.run(expectedTimeout + 10).get();
    Assert.assertTrue(checkTimeoutFired(restCallback));
    checkRequestTimeoutOrViewSet(requestContext);
}
Also used : D2Client(com.linkedin.d2.balancer.D2Client) RestResponse(com.linkedin.r2.message.rest.RestResponse) KeyMapperProvider(com.linkedin.d2.balancer.util.KeyMapperProvider) ClientFactoryProvider(com.linkedin.d2.balancer.util.ClientFactoryProvider) URI(java.net.URI) Facilities(com.linkedin.d2.balancer.Facilities) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) LoadBalancerMock(com.linkedin.d2.balancer.clients.stub.LoadBalancerMock) DirectoryProviderMock(com.linkedin.d2.balancer.clients.stub.DirectoryProviderMock) RestRequest(com.linkedin.r2.message.rest.RestRequest) DirectoryProvider(com.linkedin.d2.balancer.util.DirectoryProvider) LoadBalancerSimulator(com.linkedin.d2.balancer.simple.LoadBalancerSimulator) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) KeyMapperProviderMock(com.linkedin.d2.balancer.clients.stub.KeyMapperProviderMock) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) Test(org.testng.annotations.Test)

Aggregations

LoadBalancerMock (com.linkedin.d2.balancer.clients.stub.LoadBalancerMock)4 Test (org.testng.annotations.Test)4 Facilities (com.linkedin.d2.balancer.Facilities)3 DelegatingFacilities (com.linkedin.d2.balancer.util.DelegatingFacilities)3 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 URI (java.net.URI)3 TestCallback (com.linkedin.d2.balancer.clients.DegraderTrackerClientTest.TestCallback)2 DirectoryProviderMock (com.linkedin.d2.balancer.clients.stub.DirectoryProviderMock)2 KeyMapperProviderMock (com.linkedin.d2.balancer.clients.stub.KeyMapperProviderMock)2 ClientFactoryProvider (com.linkedin.d2.balancer.util.ClientFactoryProvider)2 DirectoryProvider (com.linkedin.d2.balancer.util.DirectoryProvider)2 KeyMapperProvider (com.linkedin.d2.balancer.util.KeyMapperProvider)2 None (com.linkedin.common.util.None)1 D2Client (com.linkedin.d2.balancer.D2Client)1 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)1 LoadBalancerSimulator (com.linkedin.d2.balancer.simple.LoadBalancerSimulator)1 RequestContext (com.linkedin.r2.message.RequestContext)1 CountDownLatch (java.util.concurrent.CountDownLatch)1