use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestDisruptor method testStreamNoDisrupt.
@Test
public void testStreamNoDisrupt() throws Exception {
final Map<String, String> properties = new HashMap<>();
final TransportClientFactory factory = new HttpClientFactory.Builder().build();
final TransportClient client = factory.getClient(properties);
final RequestContext requestContext = new RequestContext();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
client.streamRequest(new StreamRequestBuilder(new URI(REQUEST_URI)).build(EntityStreams.emptyStream()), requestContext, new HashMap<>(), response -> {
success.set(!response.hasError() && response.getResponse() != null);
latch.countDown();
});
Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
Assert.assertTrue(success.get(), "Unexpected transport response");
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestRequestCompression method testNoCompression.
@Test(dataProvider = "noCompressionData")
public void testNoCompression(Client client) throws InterruptedException, TimeoutException, ExecutionException {
StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, NO_COMPRESSION_URI)));
BytesWriter writer = new BytesWriter(THRESHOLD - 1, BYTE);
StreamRequest request = builder.build(EntityStreams.newEntityStream(writer));
final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
client.streamRequest(request, callback);
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestRequestCompression method testRequestCompression.
@Test(dataProvider = "requestCompressionData")
public void testRequestCompression(Client client, URI uri) throws InterruptedException, TimeoutException, ExecutionException {
StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, uri)));
BytesWriter writer = new BytesWriter(NUM_BYTES, BYTE);
StreamRequest request = builder.build(EntityStreams.newEntityStream(writer));
final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
client.streamRequest(request, callback);
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestDisruptor method testStreamTimeoutDisrupt.
@Test
public void testStreamTimeoutDisrupt() throws Exception {
final Map<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(REQUEST_TIMEOUT));
final TransportClientFactory factory = new HttpClientFactory.Builder().build();
final TransportClient client = factory.getClient(properties);
final RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.timeout());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
client.streamRequest(new StreamRequestBuilder(new URI(REQUEST_URI)).build(EntityStreams.emptyStream()), requestContext, new HashMap<>(), response -> {
success.set(response.hasError() && response.getError() instanceof TimeoutException);
latch.countDown();
});
Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
Assert.assertTrue(success.get(), "Unexpected transport response");
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestStreamEcho method testBackPressureEcho.
@Test
public void testBackPressureEcho() throws Exception {
for (Client client : clients()) {
TimedBytesWriter writer = new TimedBytesWriter(SMALL_BYTES_NUM, BYTE);
StreamRequest request = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, ECHO_URI)).build(EntityStreams.newEntityStream(writer));
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
final Callback<None> readerCallback = getReaderCallback(latch, error);
final TimedBytesReader reader = new TimedBytesReader(BYTE, readerCallback) {
int count = 0;
@Override
protected void requestMore(final ReadHandle rh) {
count++;
if (count % 16 == 0) {
_scheduler.schedule(new Runnable() {
@Override
public void run() {
rh.request(1);
}
}, INTERVAL, TimeUnit.MILLISECONDS);
} else {
rh.request(1);
}
}
};
Callback<StreamResponse> callback = getCallback(status, readerCallback, reader);
client.streamRequest(request, callback);
latch.await(60000, TimeUnit.MILLISECONDS);
Assert.assertNull(error.get());
Assert.assertEquals(status.get(), RestStatus.OK);
Assert.assertEquals(reader.getTotalBytes(), SMALL_BYTES_NUM);
Assert.assertTrue(reader.allBytesCorrect());
long clientSendTimespan = writer.getStopTime() - writer.getStartTime();
long clientReceiveTimespan = reader.getStopTime() - reader.getStartTime();
double diff = Math.abs(clientReceiveTimespan - clientSendTimespan);
double diffRatio = diff / clientSendTimespan;
// make it generous to reduce the chance occasional test failures
Assert.assertTrue(diffRatio < 0.5, "Send/receive time delta is " + diff + " but expected to be less than 0.5. Send time span is " + clientSendTimespan + " and receive time span is " + clientReceiveTimespan);
}
}
Aggregations