use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class RetryClientTest method testStreamRetry.
@Test
public void testStreamRetry() 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, true);
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.e);
assertNotNull(restCallback.t);
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TestBackupRequestsClient method testStreamRequestWithNoIsFullRequest.
@Test(invocationCount = 3, dataProvider = "isD2Async")
public void testStreamRequestWithNoIsFullRequest(boolean isD2Async) throws Exception {
// 1s till response comes back
int responseDelayNano = 100000000;
// make backup request after 0.5 second
int backupDelayNano = 50000000;
Deque<URI> hostsReceivingRequest = new ConcurrentLinkedDeque<>();
BackupRequestsClient client = createAlwaysBackupClientWithHosts(Arrays.asList("http://test1.com:123", "http://test2.com:123"), hostsReceivingRequest, responseDelayNano, backupDelayNano, isD2Async);
URI uri = URI.create("d2://testService");
// if there is no IS_FULL_REQUEST set, backup requests will not happen
StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.newEntityStream(new ByteStringWriter(CONTENT)));
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.OPERATION, "get");
RequestContext context1 = context.clone();
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<AssertionError> failure = new AtomicReference<>();
client.streamRequest(streamRequest, context1, new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
failure.set(new AssertionError("Callback onError"));
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
try {
assertEquals(result.getStatus(), 200);
assertEquals(result.getHeader("buffered"), "false");
assertEquals(hostsReceivingRequest.size(), 1);
assertEquals(new HashSet<>(hostsReceivingRequest).size(), 1);
hostsReceivingRequest.clear();
} catch (AssertionError e) {
failure.set(e);
}
latch.countDown();
}
});
latch.await(2, TimeUnit.SECONDS);
if (failure.get() != null) {
throw failure.get();
}
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TestBackupRequestsClient method testStreamRequestWithIsFullRequest.
@Test(invocationCount = 3, dataProvider = "isD2Async")
public void testStreamRequestWithIsFullRequest(boolean isD2Async) throws Exception {
// 5s till response comes back
int responseDelayNano = 500000000;
// make backup request after 1 second
int backupDelayNano = 100000000;
Deque<URI> hostsReceivingRequest = new ConcurrentLinkedDeque<>();
BackupRequestsClient client = createAlwaysBackupClientWithHosts(Arrays.asList("http://test1.com:123", "http://test2.com:123"), hostsReceivingRequest, responseDelayNano, backupDelayNano, isD2Async);
URI uri = URI.create("d2://testService");
// if there is IS_FULL_REQUEST set, backup requests will happen
StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.newEntityStream(new ByteStringWriter(CONTENT)));
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.OPERATION, "get");
context.putLocalAttr(R2Constants.IS_FULL_REQUEST, true);
RequestContext context1 = context.clone();
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<AssertionError> failure = new AtomicReference<>();
client.streamRequest(streamRequest, context1, new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
failure.set(new AssertionError("Callback onError"));
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
try {
assertEquals(result.getStatus(), 200);
assertEquals(result.getHeader("buffered"), "true");
assertEquals(hostsReceivingRequest.size(), 2);
assertEquals(new HashSet<>(hostsReceivingRequest).size(), 2);
hostsReceivingRequest.clear();
} catch (AssertionError e) {
failure.set(e);
}
latch.countDown();
}
});
latch.await(6, TimeUnit.SECONDS);
if (failure.get() != null) {
throw failure.get();
}
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class RewriteLoadBalancerClientTestStreamRequest method testEscapingHelper.
private void testEscapingHelper(String hostUri, String serviceName, String path) {
URI uri = URI.create(hostUri);
TestClient wrappedClient = new TestClient();
RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
assertEquals(client.getUri(), uri);
assertEquals(client.getServiceName(), serviceName);
StreamRequest streamRequest = getRequest("d2://" + serviceName + path);
Map<String, String> restWireAttrs = new HashMap<>();
TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<>();
client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
assertFalse(restCallback.response.hasError());
assertEquals(wrappedClient.streamRequest.getHeaders(), streamRequest.getHeaders());
assertEquals(wrappedClient.streamRequest.getMethod(), streamRequest.getMethod());
assertEquals(wrappedClient.streamRequest.getURI(), URI.create(hostUri + path));
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class RewriteLoadBalancerClientTestStreamRequest method testClient.
@Test(groups = { "small", "back-end" })
public void testClient() throws URISyntaxException {
URI uri = URI.create("http://test.linkedin.com/test");
String serviceName = "HistoryService";
TestClient wrappedClient = new TestClient();
RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
assertEquals(client.getUri(), uri);
assertEquals(client.getServiceName(), serviceName);
StreamRequest streamRequest = new StreamRequestBuilder(URI.create("d2://HistoryService/getCube")).build(EntityStreams.emptyStream());
Map<String, String> restWireAttrs = new HashMap<>();
TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<>();
client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
assertFalse(restCallback.response.hasError());
assertEquals(wrappedClient.streamRequest.getHeaders(), streamRequest.getHeaders());
assertEquals(wrappedClient.streamRequest.getMethod(), streamRequest.getMethod());
// check the rewrite
assertEquals(wrappedClient.streamRequest.getURI(), URI.create("http://test.linkedin.com/test/getCube"));
assertEquals(wrappedClient.restWireAttrs, restWireAttrs);
}
Aggregations