use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestEntityStream method testWriterAndReaderInitError.
@Test
public void testWriterAndReaderInitError() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final TestWriter writer = new TestWriter() {
@Override
public void onInit(WriteHandle wh) {
throw new RuntimeException();
}
};
TestObserver observer = new TestObserver();
final EntityStream es = EntityStreams.newEntityStream(writer);
es.addObserver(observer);
final ControlReader reader = new ControlReader() {
@Override
public void onInit(ReadHandle rh) {
throw new RuntimeException();
}
};
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(new Runnable() {
@Override
public void run() {
es.setReader(reader);
latch.countDown();
}
});
Assert.assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
Assert.assertEquals(reader.errorTimes(), 1);
Assert.assertEquals(reader.getChunkCount(), 0);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(reader.getChunkCount(), 0);
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertEquals(writer.getWritePossibleCount(), 0);
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestEntityStream method testWriterThrow.
public void testWriterThrow(ControlReader reader, TestWriter writerOnAbort, TestWriter writerOnWritePossible) {
TestObserver observer = new TestObserver();
EntityStream es = EntityStreams.newEntityStream(writerOnAbort);
es.addObserver(observer);
es.setReader(reader);
reader.read(1);
writerOnAbort.write();
writerOnAbort.done();
Assert.assertEquals(writerOnAbort.abortedTimes(), 1);
Assert.assertEquals(observer.getChunkCount(), 1);
Assert.assertEquals(observer.doneTimes(), 1);
Assert.assertEquals(observer.errorTimes(), 0);
Assert.assertEquals(reader.getChunkCount(), 1);
Assert.assertEquals(reader.doneTimes(), 1);
Assert.assertEquals(reader.errorTimes(), 0);
reader = new ControlReader();
observer = new TestObserver();
es = EntityStreams.newEntityStream(writerOnWritePossible);
es.addObserver(observer);
es.setReader(reader);
reader.read(1);
writerOnWritePossible.write();
writerOnWritePossible.done();
Assert.assertEquals(writerOnWritePossible.abortedTimes(), 1);
Assert.assertEquals(observer.getChunkCount(), 0);
Assert.assertEquals(observer.doneTimes(), 0);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(reader.getChunkCount(), 0);
Assert.assertEquals(reader.doneTimes(), 0);
Assert.assertEquals(reader.errorTimes(), 1);
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class ResponseUtils method buildStreamException.
public static StreamException buildStreamException(RestLiResponseException restLiResponseException, ContentType contentType) {
RestLiResponse restLiResponse = restLiResponseException.getRestLiResponse();
StreamResponseBuilder responseBuilder = new StreamResponseBuilder().setHeaders(restLiResponse.getHeaders()).setHeader(RestConstants.HEADER_CONTENT_TYPE, contentType.getHeaderKey()).setCookies(CookieUtil.encodeSetCookies(restLiResponse.getCookies())).setStatus(restLiResponse.getStatus() == null ? HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode() : restLiResponse.getStatus().getCode());
EntityStream<ByteString> entityStream = contentType.getStreamCodec().encodeMap(restLiResponse.getDataMap());
StreamResponse response = responseBuilder.build(EntityStreamAdapters.fromGenericEntityStream(entityStream));
return new StreamException(response, restLiResponseException.getCause());
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestStreamRequest method testBackPressure.
@Test
public void testBackPressure() throws Exception {
final long totalBytes = SMALL_BYTES_NUM;
TimedBytesWriter writer = new TimedBytesWriter(totalBytes, BYTE);
EntityStream entityStream = EntityStreams.newEntityStream(writer);
StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, RATE_LIMITED_URI));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
Callback<StreamResponse> callback = expectSuccessCallback(latch, status);
_client.streamRequest(request, callback);
latch.await(60000, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), RestStatus.OK);
TimedBytesReader reader = _rateLimitedRequestHandler.getReader();
Assert.assertNotNull(reader);
Assert.assertEquals(totalBytes, reader.getTotalBytes());
Assert.assertTrue(reader.allBytesCorrect());
long clientSendTimespan = writer.getStopTime() - writer.getStartTime();
long serverReceiveTimespan = reader.getStopTime() - reader.getStartTime();
Assert.assertTrue(serverReceiveTimespan > 1000);
double diff = Math.abs(serverReceiveTimespan - clientSendTimespan);
double diffRatio = diff / clientSendTimespan;
// make it generous to reduce the chance occasional test failures
Assert.assertTrue(diffRatio < 0.2);
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestStreamRequest method test404.
// jetty 404 tests singled out
@Test(enabled = false)
public void test404() throws Exception {
final long totalBytes = TINY_BYTES_NUM;
EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(totalBytes, BYTE));
StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, URI.create("/boo")));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
Callback<StreamResponse> callback = expectErrorCallback(latch, status);
_client.streamRequest(request, callback);
latch.await(60000, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), 404);
}
Aggregations