Search in sources :

Example 1 with KeyMapperProviderMock

use of com.linkedin.d2.balancer.clients.stub.KeyMapperProviderMock 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 2 with KeyMapperProviderMock

use of com.linkedin.d2.balancer.clients.stub.KeyMapperProviderMock 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

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