Search in sources :

Example 31 with RestRequestBuilder

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

the class TestDarkClusterStrategyFactory method testUpdateStrategyDarkClusterChange.

@Test
public void testUpdateStrategyDarkClusterChange() {
    DarkClusterConfig darkClusterConfig1 = createRelativeTrafficMultiplierConfig(0.5f);
    DarkClusterConfig darkClusterConfig2 = createRelativeTrafficMultiplierConfig(0.1f);
    _clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig1);
    _clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
    DarkClusterStrategy strategy = _strategyFactory.get(DARK_CLUSTER_NAME);
    Assert.assertTrue(strategy instanceof RelativeTrafficMultiplierDarkClusterStrategy);
    Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy).getMultiplier(), 0.5f, "expected 0.5f multiplier");
    // update the strategy.
    _clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig2);
    // now trigger a refresh on the dark cluster. Note that darkClusterConfig1 is ignored since there should already be an entry for this
    // dark cluster, and we should get the strategy associated with darkClusterConfig2 back.
    _clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
    DarkClusterStrategy strategy3 = _strategyFactory.get(DARK_CLUSTER_NAME);
    Assert.assertTrue(strategy3 instanceof RelativeTrafficMultiplierDarkClusterStrategy);
    Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy3).getMultiplier(), 0.1f, "expected 0.1f multiplier");
    // if someone has a handle to old strategies, those should still be usable.
    RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("foo")).build();
    strategy.handleRequest(dummyRestRequest, dummyRestRequest, new RequestContext());
}
Also used : RelativeTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy) RestRequest(com.linkedin.r2.message.rest.RestRequest) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) NoOpDarkClusterStrategy(com.linkedin.darkcluster.api.NoOpDarkClusterStrategy) RelativeTrafficMultiplierDarkClusterStrategy(com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy) DarkClusterStrategy(com.linkedin.darkcluster.api.DarkClusterStrategy) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 32 with RestRequestBuilder

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

the class TestDarkClusterVerifierManager method testVerifierErrorHandling.

@Test
void testVerifierErrorHandling() throws InterruptedException {
    setup(true);
    RestRequest req = new RestRequestBuilder(URI.create("foo")).build();
    _verifierManager.onDarkError(req, new Throwable(), DARK_CLUSTER1_NAME);
    _verifierManager.onDarkError(req, new Throwable(), DARK_CLUSTER1_NAME);
    _verifierManager.onError(req, new Throwable());
    waitForLatch();
    Assert.assertEquals(_verifier.onResponseCount, 1, "expected on response count of 1");
    Assert.assertEquals(_verifier.onDarkResponseCount, 2, "expected on dark response count of 2");
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 33 with RestRequestBuilder

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

the class TestDarkClusterVerifierManager method testSafeVerifier.

@Test
void testSafeVerifier() throws InterruptedException {
    // only use this to set up the executor service.
    setup(false);
    DarkClusterVerifier verifier = new SafeDarkClusterVerifier(new TestThrowingVerifier());
    DarkClusterVerifierManager verifierManager = new DarkClusterVerifierManagerImpl(verifier, _executorService);
    RestRequest req = new RestRequestBuilder(URI.create("foo")).build();
    RestResponse res = new RestResponseBuilder().build();
    verifierManager.onDarkResponse(req, res, DARK_CLUSTER1_NAME);
    verifierManager.onDarkResponse(req, res, DARK_CLUSTER1_NAME);
    verifierManager.onResponse(req, res);
    waitForLatch();
    // if we got here, we successfully caught the exceptions
    // now retry without the SafeDarkClusterVerifier
    DarkClusterVerifier verifier2 = new TestThrowingVerifier();
    DarkClusterVerifierManager verifierManager2 = new DarkClusterVerifierManagerImpl(verifier2, _executorService);
    RestRequest req2 = new RestRequestBuilder(URI.create("foo")).build();
    RestResponse res2 = new RestResponseBuilder().build();
    try {
        verifierManager2.onDarkResponse(req2, res2, DARK_CLUSTER1_NAME);
        verifierManager2.onDarkResponse(req2, res2, DARK_CLUSTER1_NAME);
        verifierManager2.onResponse(req2, res2);
        waitForLatch();
        // we shouldn't get here, should have thrown exception
        fail("shouldn't have gotten here");
    } catch (Throwable t) {
    // expected, because we aren't using the SafeDarkClusterVerifier here
    }
}
Also used : SafeDarkClusterVerifier(com.linkedin.darkcluster.impl.SafeDarkClusterVerifier) DarkClusterVerifierManager(com.linkedin.darkcluster.api.DarkClusterVerifierManager) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) SafeDarkClusterVerifier(com.linkedin.darkcluster.impl.SafeDarkClusterVerifier) DarkClusterVerifier(com.linkedin.darkcluster.api.DarkClusterVerifier) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) DarkClusterVerifierManagerImpl(com.linkedin.darkcluster.impl.DarkClusterVerifierManagerImpl) Test(org.testng.annotations.Test)

Example 34 with RestRequestBuilder

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

the class TestCapRepFilter method testStreamException.

@Test
public void testStreamException() throws IOException {
    Path dirPath = Files.createTempDirectory("caprep-test");
    CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
    FilterChain fc = FilterChains.createStreamChain(StreamFilterAdapters.adaptRestFilter(lastCallFilter), _filter);
    RestRequest myRequest = new RestRequestBuilder(URI.create("/req1")).setEntity("123".getBytes()).build();
    RestResponse myErrorResponse = new RestResponseBuilder().setStatus(400).setEntity("321".getBytes()).build();
    RestException myRestException = new RestException(myErrorResponse);
    _filter.capture(dirPath.toString());
    RequestContext requestContext = new RequestContext();
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), requestContext, FilterUtil.emptyWireAttrs());
    FilterUtil.fireStreamError(fc, Messages.toStreamException(myRestException), requestContext, FilterUtil.emptyWireAttrs());
    lastCallFilter.reset();
    _filter.replay(dirPath.toString());
    FilterUtil.fireSimpleStreamRequest(fc);
    Assert.assertNull(lastCallFilter.getLastErr());
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest));
    Assert.assertTrue(lastCallFilter.getLastErr() instanceof RestException);
    Assert.assertEquals(((RestException) lastCallFilter.getLastErr()).getResponse(), myErrorResponse);
}
Also used : Path(java.nio.file.Path) RestRequest(com.linkedin.r2.message.rest.RestRequest) CaptureLastCallFilter(com.linkedin.r2.testutils.filter.CaptureLastCallFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) FilterChain(com.linkedin.r2.filter.FilterChain) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 35 with RestRequestBuilder

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

the class TestCapRepFilter method testStreamResponse.

@Test
public void testStreamResponse() throws IOException {
    Path dirPath = Files.createTempDirectory("caprep-test");
    StreamCountFilter after = new StreamCountFilter();
    StreamCountFilter before = new StreamCountFilter();
    CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
    FilterChain fc = FilterChains.createStreamChain(StreamFilterAdapters.adaptRestFilter(lastCallFilter), before, _filter, after);
    RestRequest myRequest = new RestRequestBuilder(URI.create("/req1")).setEntity("123".getBytes()).build();
    RestResponse myResponse = new RestResponseBuilder().setStatus(201).setEntity("321".getBytes()).build();
    _filter.capture(dirPath.toString());
    RequestContext requestContext = new RequestContext();
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), requestContext, FilterUtil.emptyWireAttrs());
    FilterUtil.fireStreamResponse(fc, Messages.toStreamResponse(myResponse), requestContext, FilterUtil.emptyWireAttrs());
    Assert.assertEquals(after.getStreamReqCount(), 1);
    Assert.assertEquals(after.getStreamResCount(), 1);
    Assert.assertEquals(before.getStreamReqCount(), 1);
    Assert.assertEquals(before.getStreamResCount(), 1);
    lastCallFilter.reset();
    _filter.passThrough();
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest));
    Assert.assertEquals(after.getStreamReqCount(), 2);
    Assert.assertEquals(after.getStreamResCount(), 1);
    Assert.assertEquals(before.getStreamReqCount(), 2);
    Assert.assertEquals(before.getStreamResCount(), 1);
    Assert.assertNull(lastCallFilter.getLastRes());
    _filter.replay(dirPath.toString());
    FilterUtil.fireSimpleStreamRequest(fc);
    Assert.assertNull(lastCallFilter.getLastRes());
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), new RequestContext(), FilterUtil.emptyWireAttrs());
    Assert.assertEquals(lastCallFilter.getLastRes(), myResponse);
    Assert.assertEquals(after.getStreamReqCount(), 3);
    Assert.assertEquals(after.getStreamResCount(), 1);
    Assert.assertEquals(before.getStreamReqCount(), 4);
    Assert.assertEquals(before.getStreamResCount(), 2);
}
Also used : Path(java.nio.file.Path) StreamCountFilter(com.linkedin.r2.testutils.filter.StreamCountFilter) RestRequest(com.linkedin.r2.message.rest.RestRequest) CaptureLastCallFilter(com.linkedin.r2.testutils.filter.CaptureLastCallFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) FilterChain(com.linkedin.r2.filter.FilterChain) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

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