use of com.linkedin.r2.message.stream.entitystream.Observer in project rest.li by linkedin.
the class TestEntityStream method testCancelSimple.
@Test
public void testCancelSimple() throws Exception {
TestWriter writer = new TestWriter();
TestObserver observer = new TestObserver();
final ControlReader reader = new ControlReader();
EntityStream es = EntityStreams.newEntityStream(writer);
es.addObserver(observer);
es.setReader(reader);
reader.read(1000);
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(new Runnable() {
@Override
public void run() {
reader.cancel();
}
}, 50, TimeUnit.MILLISECONDS);
while (writer.remaining() > 0) {
writer.write();
Thread.sleep(1);
}
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertTrue(observer.getChunkCount() < 100);
Assert.assertEquals(observer.doneTimes(), 0);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(observer.getLastEvent(), "onError");
Assert.assertTrue(reader.getChunkCount() < 100);
Assert.assertEquals(reader.doneTimes(), 0);
Assert.assertEquals(reader.errorTimes(), 0);
scheduler.shutdown();
}
use of com.linkedin.r2.message.stream.entitystream.Observer 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.Observer in project rest.li by linkedin.
the class TestEntityStream method testObserversThrowUncheckedError.
@Test
public void testObserversThrowUncheckedError() {
Observer observer = new TestObserver() {
@Override
public void onDone() {
throw new Error("broken observer throws");
}
@Override
public void onDataAvailable(ByteString data) {
throw new Error("broken observer throws");
}
@Override
public void onError(Throwable ex) {
throw new Error("broken observer throws");
}
};
Error ex = new Error("writer has problem");
testObserverThrow(observer, ex);
}
use of com.linkedin.r2.message.stream.entitystream.Observer 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.Observer in project rest.li by linkedin.
the class ClientRequestFinalizerFilter method onStreamResponse.
@Override
public void onStreamResponse(StreamResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
res.getEntityStream().addObserver(new Observer() {
@Override
public void onDataAvailable(ByteString data) {
// do nothing
}
@Override
public void onDone() {
doFinalizeRequest(requestContext, res, null);
}
@Override
public void onError(Throwable e) {
doFinalizeRequest(requestContext, res, e);
}
});
nextFilter.onResponse(res, requestContext, wireAttrs);
}
Aggregations