use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestStreamRequest method testErrorWriter.
@Test
public void testErrorWriter() throws Exception {
final long totalBytes = SMALL_BYTES_NUM;
EntityStream entityStream = EntityStreams.newEntityStream(new ErrorWriter(totalBytes, BYTE));
StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, FOOBAR_URI));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<>();
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
error.set(e);
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
latch.countDown();
}
};
_client.streamRequest(request, callback);
latch.await();
Assert.assertNotNull(error.get());
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestStreamingTimeout method testStreamTimeoutWithStreamingTimeoutInServerStream.
@Test
public void testStreamTimeoutWithStreamingTimeoutInServerStream() throws Exception {
final EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(SMALL_BYTES_NUM, BYTE));
final StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, RATE_LIMITED_URI));
final StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicReference<Throwable> throwable = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
final Callback<StreamResponse> callback = expectErrorCallback(latch, throwable);
_client.streamRequest(request, callback);
latch.await(HTTP_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertNotNull(throwable.get());
final Throwable rootCause = ExceptionUtils.getRootCause(throwable.get());
Assert.assertTrue(rootCause instanceof TimeoutException);
final TimeoutException timeoutException = (TimeoutException) rootCause;
Assert.assertEquals(timeoutException.getMessage(), String.format(StreamingTimeout.STREAMING_TIMEOUT_MESSAGE, HTTP_STREAMING_TIMEOUT));
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestStreamingTimeout method testStreamTimeoutWhenGreaterThanRequestTimeout.
@Test
public void testStreamTimeoutWhenGreaterThanRequestTimeout() throws Exception {
final EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(SMALL_BYTES_NUM, BYTE));
final StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, RATE_LIMITED_URI));
final StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicReference<Throwable> throwable = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
final Callback<StreamResponse> callback = expectErrorCallback(latch, throwable);
Map<String, Object> clientProperties = getHttpClientProperties();
clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(HTTP_STREAMING_TIMEOUT));
clientProperties.put(HttpClientFactory.HTTP_STREAMING_TIMEOUT, String.valueOf(HTTP_STREAMING_TIMEOUT));
Client client = _clientProvider.createClient(getClientFilterChain(), clientProperties);
client.streamRequest(request, callback);
latch.await(HTTP_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertNotNull(throwable.get());
final Throwable rootCause = ExceptionUtils.getRootCause(throwable.get());
Assert.assertTrue(rootCause instanceof TimeoutException);
final TimeoutException timeoutException = (TimeoutException) rootCause;
Assert.assertEquals(timeoutException.getMessage(), String.format(REQUEST_TIMEOUT_MESSAGE, HTTP_STREAMING_TIMEOUT));
tearDown(client);
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestStreamingTimeout method testStreamTimeoutWithStreamTimeoutInClientStream.
@Test
public void testStreamTimeoutWithStreamTimeoutInClientStream() throws Exception {
final EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(LARGE_BYTES_NUM, BYTE) {
int count = 2;
@Override
protected void afterWrite(WriteHandle wh, long written) {
count = count * 2;
long delay = Math.min(count, HTTP_STREAMING_TIMEOUT);
try {
Thread.sleep(delay);
} catch (Exception ex) {
// Do Nothing
}
}
});
final StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, NON_RATE_LIMITED_URI));
final StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicReference<Throwable> throwable = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
final Callback<StreamResponse> callback = expectErrorCallback(latch, throwable);
_client.streamRequest(request, callback);
latch.await(30000, TimeUnit.MILLISECONDS);
Assert.assertNotNull(throwable.get());
final Throwable rootCause = ExceptionUtils.getRootCause(throwable.get());
Assert.assertTrue(rootCause instanceof TimeoutException);
final TimeoutException timeoutException = (TimeoutException) rootCause;
Assert.assertEquals(timeoutException.getMessage(), String.format(StreamingTimeout.STREAMING_TIMEOUT_MESSAGE, HTTP_STREAMING_TIMEOUT));
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestStreamingTimeout method testStreamSuccessWithoutStreamingTimeout.
@Test
public void testStreamSuccessWithoutStreamingTimeout() throws Exception {
final long totalBytes = TINY_BYTES_NUM;
final EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(totalBytes, BYTE));
final StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, NON_RATE_LIMITED_URI));
final StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
final Callback<StreamResponse> callback = expectSuccessCallback(latch, status);
_client.streamRequest(request, callback);
latch.await(HTTP_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), RestStatus.OK);
final BytesReader reader = _requestHandler.getReader();
Assert.assertNotNull(reader);
Assert.assertEquals(totalBytes, reader.getTotalBytes());
Assert.assertTrue(reader.allBytesCorrect());
}
Aggregations