Search in sources :

Example 1 with IdenticalTrafficMultiplierDarkClusterStrategy

use of com.linkedin.darkcluster.impl.IdenticalTrafficMultiplierDarkClusterStrategy in project rest.li by linkedin.

the class TestIdenticalTrafficMultiplierDarkClusterStrategy method testHandleRequest.

@Test(dataProvider = "getDarkClusters")
public void testHandleRequest(List<DarkClusterMetadata> darkClusterMetadataList, float expectedRandomNumber) {
    String sourceClusterName = "sourceCluster";
    DarkClusterDispatcher darkClusterDispatcher = new DefaultDarkClusterDispatcher(new MockClient(false));
    Random random = Mockito.mock(Random.class);
    Mockito.when(random.nextFloat()).thenReturn(expectedRandomNumber);
    List<IdenticalTrafficMultiplierDarkClusterStrategy> strategies = darkClusterMetadataList.stream().map(darkClusterMetadata -> {
        BaseDarkClusterDispatcherImpl baseDispatcher = new BaseDarkClusterDispatcherImpl(darkClusterMetadata._darkClusterName, darkClusterDispatcher, new DoNothingNotifier(), new CountingVerifierManager());
        MockClusterInfoProvider mockClusterInfoProvider = new MockClusterInfoProvider();
        mockClusterInfoProvider.putHttpsClusterCount(darkClusterMetadata._darkClusterName, 1);
        mockClusterInfoProvider.putHttpsClusterCount(sourceClusterName, 1);
        return new IdenticalTrafficMultiplierDarkClusterStrategy(sourceClusterName, darkClusterMetadata._darkClusterName, darkClusterMetadata._multiplier, baseDispatcher, new DoNothingNotifier(), mockClusterInfoProvider, random);
    }).collect(Collectors.toList());
    RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("foo")).build();
    RestRequest dummyDarkRequest = new RestRequestBuilder(URI.create("darkfoo")).build();
    RequestContext dummyRequestContext = new RequestContext();
    IntStream.range(0, darkClusterMetadataList.size()).forEach(index -> {
        DarkClusterMetadata darkClusterMetadata = darkClusterMetadataList.get(index);
        DarkClusterStrategy strategy = strategies.get(index);
        boolean darkRequestSent = strategy.handleRequest(dummyRestRequest, dummyDarkRequest, dummyRequestContext);
        Assert.assertEquals(darkRequestSent, darkClusterMetadata._darkRequestSent);
    });
    Assert.assertEquals(dummyRequestContext.getLocalAttr("identicalTrafficMultiplier.randomNumber"), expectedRandomNumber);
    Mockito.verify(random).nextFloat();
}
Also used : IntStream(java.util.stream.IntStream) DarkClusterStrategy(com.linkedin.darkcluster.api.DarkClusterStrategy) RestRequest(com.linkedin.r2.message.rest.RestRequest) Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) IdenticalTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.IdenticalTrafficMultiplierDarkClusterStrategy) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Random(java.util.Random) Test(org.testng.annotations.Test) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) BaseDarkClusterDispatcherImpl(com.linkedin.darkcluster.impl.BaseDarkClusterDispatcherImpl) List(java.util.List) RequestContext(com.linkedin.r2.message.RequestContext) Assert(org.testng.Assert) DarkClusterDispatcher(com.linkedin.darkcluster.api.DarkClusterDispatcher) URI(java.net.URI) DefaultDarkClusterDispatcher(com.linkedin.darkcluster.impl.DefaultDarkClusterDispatcher) DefaultDarkClusterDispatcher(com.linkedin.darkcluster.impl.DefaultDarkClusterDispatcher) IdenticalTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.IdenticalTrafficMultiplierDarkClusterStrategy) DarkClusterDispatcher(com.linkedin.darkcluster.api.DarkClusterDispatcher) DefaultDarkClusterDispatcher(com.linkedin.darkcluster.impl.DefaultDarkClusterDispatcher) DarkClusterStrategy(com.linkedin.darkcluster.api.DarkClusterStrategy) IdenticalTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.IdenticalTrafficMultiplierDarkClusterStrategy) RestRequest(com.linkedin.r2.message.rest.RestRequest) Random(java.util.Random) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) BaseDarkClusterDispatcherImpl(com.linkedin.darkcluster.impl.BaseDarkClusterDispatcherImpl) Test(org.testng.annotations.Test)

Aggregations

DarkClusterDispatcher (com.linkedin.darkcluster.api.DarkClusterDispatcher)1 DarkClusterStrategy (com.linkedin.darkcluster.api.DarkClusterStrategy)1 BaseDarkClusterDispatcherImpl (com.linkedin.darkcluster.impl.BaseDarkClusterDispatcherImpl)1 DefaultDarkClusterDispatcher (com.linkedin.darkcluster.impl.DefaultDarkClusterDispatcher)1 IdenticalTrafficMultiplierDarkClusterStrategy (com.linkedin.darkcluster.impl.IdenticalTrafficMultiplierDarkClusterStrategy)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Mockito (org.mockito.Mockito)1 Assert (org.testng.Assert)1 DataProvider (org.testng.annotations.DataProvider)1 Test (org.testng.annotations.Test)1