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());
}
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");
}
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
}
}
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);
}
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);
}
Aggregations