use of com.linkedin.r2.message.stream.entitystream.ReadHandle in project rest.li by linkedin.
the class TestServerTimeout method testServerTimeout.
@Test
public void testServerTimeout() throws Exception {
final StreamRequest request = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, BUGGY_SERVER_URI)).build(EntityStreams.emptyStream());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger status = new AtomicInteger(-1);
_client.streamRequest(request, new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
status.set(result.getStatus());
result.getEntityStream().setReader(new Reader() {
private ReadHandle _rh;
@Override
public void onInit(ReadHandle rh) {
_rh = rh;
_rh.request(Integer.MAX_VALUE);
}
@Override
public void onDataAvailable(ByteString data) {
// do nothing
}
@Override
public void onDone() {
// server would close the connection if TimeoutException, and netty would end the chunked transferring
// with an empty chunk
latch.countDown();
}
@Override
public void onError(Throwable e) {
latch.countDown();
}
});
}
});
// server should timeout so await should return true
Assert.assertTrue(latch.await(SERVER_IOHANDLER_TIMEOUT * 2, TimeUnit.MILLISECONDS));
Assert.assertEquals(status.get(), RestStatus.OK);
}
use of com.linkedin.r2.message.stream.entitystream.ReadHandle in project rest.li by linkedin.
the class TestEntityStream method testReaderInitErrorThrowInStreaming.
@Test
public void testReaderInitErrorThrowInStreaming() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final TestWriter writer = new TestWriter() {
@Override
public void onWritePossible() {
super.onWritePossible();
write();
}
};
TestObserver observer = new TestObserver();
final EntityStream es = EntityStreams.newEntityStream(writer);
es.addObserver(observer);
final ControlReader reader = new ControlReader() {
@Override
public void onInit(ReadHandle rh) {
rh.request(1);
}
@Override
public void onDataAvailable(ByteString data) {
super.onDataAvailable(data);
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(), 1);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(reader.getChunkCount(), 1);
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertEquals(writer.getWritePossibleCount(), 1);
}
use of com.linkedin.r2.message.stream.entitystream.ReadHandle in project rest.li by linkedin.
the class TestEntityStream method testNoStackOverflow.
@Test
public void testNoStackOverflow() throws Exception {
Writer dumbWriter = new Writer() {
WriteHandle _wh;
long _count = 0;
final int _total = 1024 * 1024 * 1024;
@Override
public void onInit(WriteHandle wh) {
_wh = wh;
}
@Override
public void onWritePossible() {
while (_wh.remaining() > 0 && _count < _total) {
byte[] bytes = new byte[(int) Math.min(4096, _total - _count)];
_wh.write(ByteString.copy(bytes));
_count += bytes.length;
}
if (_count >= _total) {
_wh.done();
}
}
@Override
public void onAbort(Throwable ex) {
// do nothing
}
};
Reader dumbReader = new Reader() {
ReadHandle _rh;
@Override
public void onInit(ReadHandle rh) {
_rh = rh;
_rh.request(1);
}
@Override
public void onDataAvailable(ByteString data) {
_rh.request(1);
}
@Override
public void onDone() {
}
@Override
public void onError(Throwable e) {
}
};
EntityStreams.newEntityStream(dumbWriter).setReader(dumbReader);
}
use of com.linkedin.r2.message.stream.entitystream.ReadHandle in project rest.li by linkedin.
the class TestEntityStream method testReaderInitError.
@Test
public void testReaderInitError() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final TestWriter writer = new TestWriter();
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(observer.errorTimes(), 1);
Assert.assertEquals(writer.abortedTimes(), 1);
}
use of com.linkedin.r2.message.stream.entitystream.ReadHandle in project rest.li by linkedin.
the class TestServerTimeout method testServerTimeout.
@Test
public void testServerTimeout() throws Exception {
final StreamRequest request = new StreamRequestBuilder(getHttpUri(BUGGY_SERVER_URI)).build(EntityStreams.emptyStream());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger status = new AtomicInteger(-1);
_client.streamRequest(request, new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
status.set(result.getStatus());
result.getEntityStream().setReader(new Reader() {
private ReadHandle _rh;
@Override
public void onInit(ReadHandle rh) {
_rh = rh;
_rh.request(Integer.MAX_VALUE);
}
@Override
public void onDataAvailable(ByteString data) {
// do nothing
}
@Override
public void onDone() {
// server would close the connection if TimeoutException, and netty would end the chunked transferring
// with an empty chunk
latch.countDown();
}
@Override
public void onError(Throwable e) {
latch.countDown();
}
});
}
});
// server should timeout so await should return true
Assert.assertTrue(latch.await(SERVER_IOHANDLER_TIMEOUT * 2, TimeUnit.MILLISECONDS));
Assert.assertEquals(status.get(), RestStatus.OK);
}
Aggregations