use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestStreamResponseCompression method testEncodingNotAcceptable.
public void testEncodingNotAcceptable(String acceptEncoding) throws TimeoutException, InterruptedException {
StreamRequestBuilder builder = new StreamRequestBuilder((_clientProvider.createHttpURI(_port, SMALL_URI)));
if (acceptEncoding != null) {
builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
}
StreamRequest request = builder.build(EntityStreams.emptyStream());
final FutureCallback<StreamResponse> callback = new FutureCallback<>();
_client.streamRequest(request, callback);
try {
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.fail("Should have thrown exception when encoding is not acceptable");
} catch (ExecutionException e) {
Throwable t = e.getCause();
Assert.assertTrue(t instanceof StreamException);
StreamResponse response = ((StreamException) t).getResponse();
Assert.assertEquals(response.getStatus(), HttpConstants.NOT_ACCEPTABLE);
}
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestStreamClientTimeout method testReadAfterTimeout.
@Test
public void testReadAfterTimeout() throws Exception {
StreamRequest request = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, NORMAL_URI)).build(EntityStreams.emptyStream());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<StreamResponse> response = new AtomicReference<>();
_client.streamRequest(request, new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
response.set(result);
latch.countDown();
}
});
latch.await(5000, TimeUnit.MILLISECONDS);
Assert.assertNotNull(response.get());
// let it timeout before we read
Thread.sleep(5000);
final AtomicReference<Throwable> throwable = new AtomicReference<>();
final CountDownLatch errorLatch = new CountDownLatch(1);
Reader reader = new DrainReader() {
@Override
public void onError(Throwable ex) {
throwable.set(ex);
errorLatch.countDown();
}
};
response.get().getEntityStream().setReader(reader);
errorLatch.await(5000, TimeUnit.MILLISECONDS);
Assert.assertNotNull(throwable.get());
Throwable rootCause = ExceptionUtils.getRootCause(throwable.get());
Assert.assertTrue(rootCause instanceof TimeoutException);
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class RetryClientTest method testIgnoreStreamRetry.
@Test
public void testIgnoreStreamRetry() throws Exception {
SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/good"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
DynamicClient dynamicClient = new DynamicClient(balancer, null);
RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, false);
URI uri = URI.create("d2://retryService?arg1arg2");
StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.newEntityStream(new ByteStringWriter(CONTENT)));
DegraderTrackerClientTest.TestCallback<StreamResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
client.streamRequest(streamRequest, restCallback);
assertNull(restCallback.t);
assertNotNull(restCallback.e);
assertTrue(restCallback.e.getMessage().contains("Data not available"));
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class RetryClientTest method testStreamException.
@Test
public void testStreamException() throws Exception {
SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/bad"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
DynamicClient dynamicClient = new DynamicClient(balancer, null);
RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, true);
URI uri = URI.create("d2://retryService?arg1=empty&arg2=empty");
StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.emptyStream());
DegraderTrackerClientTest.TestCallback<StreamResponse> streamCallback = new DegraderTrackerClientTest.TestCallback<>();
RequestContext context = new RequestContext();
KeyMapper.TargetHostHints.setRequestContextTargetHost(context, URI.create("http://test.linkedin.com/bad"));
client.streamRequest(streamRequest, context, streamCallback);
assertNull(streamCallback.t);
assertNotNull(streamCallback.e);
assertTrue(streamCallback.e.getMessage().contains("exception happens"), streamCallback.e.getMessage());
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class DegraderTrackerClientTest method testClientStreamRequest.
@Test(groups = { "small", "back-end" })
public void testClientStreamRequest() throws URISyntaxException {
URI uri = URI.create("http://test.qa.com:1234/foo");
double weight = 3d;
TestClient wrappedClient = new TestClient(true);
Clock clock = new SettableClock();
Map<Integer, PartitionData> partitionDataMap = createDefaultPartitionData(3d);
DegraderTrackerClient client = new DegraderTrackerClientImpl(uri, partitionDataMap, wrappedClient, clock, null);
Assert.assertEquals(client.getUri(), uri);
Double clientWeight = client.getPartitionWeight(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
Assert.assertEquals(clientWeight, weight);
Assert.assertEquals(client.getTransportClient(), wrappedClient);
StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.emptyStream());
Map<String, String> restWireAttrs = new HashMap<>();
TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<>();
client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
Assert.assertFalse(restCallback.response.hasError());
Assert.assertSame(wrappedClient.streamRequest, streamRequest);
Assert.assertEquals(wrappedClient.restWireAttrs, restWireAttrs);
}
Aggregations