use of com.linkedin.r2.message.stream.entitystream.Observer in project rest.li by linkedin.
the class TestEntityStream method testReaderThrow.
public void testReaderThrow(ControlReader readerOnData, ControlReader readerOnDone, ControlReader readerOnError, Throwable writerProblem) {
TestWriter writer = new TestWriter();
TestObserver observer = new TestObserver();
EntityStream es = EntityStreams.newEntityStream(writer);
es.addObserver(observer);
es.setReader(readerOnData);
readerOnData.read(1);
writer.write();
writer.done();
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertEquals(observer.getChunkCount(), 1);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(observer.doneTimes(), 0);
Assert.assertEquals(readerOnData.getChunkCount(), 1);
Assert.assertEquals(readerOnData.errorTimes(), 1);
Assert.assertEquals(readerOnData.doneTimes(), 0);
writer = new TestWriter();
observer = new TestObserver();
es = EntityStreams.newEntityStream(writer);
es.addObserver(observer);
es.setReader(readerOnDone);
readerOnDone.read(1);
writer.write();
writer.done();
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertEquals(observer.getChunkCount(), 1);
Assert.assertEquals(observer.doneTimes(), 1);
Assert.assertEquals(observer.errorTimes(), 0);
Assert.assertEquals(readerOnDone.getChunkCount(), 1);
Assert.assertEquals(readerOnDone.doneTimes(), 1);
Assert.assertEquals(readerOnDone.errorTimes(), 0);
writer = new TestWriter();
observer = new TestObserver();
es = EntityStreams.newEntityStream(writer);
es.addObserver(observer);
es.setReader(readerOnError);
readerOnError.read(1);
writer.write();
writer.error(writerProblem);
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertEquals(observer.getChunkCount(), 1);
Assert.assertEquals(observer.doneTimes(), 0);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(readerOnError.getChunkCount(), 1);
Assert.assertEquals(readerOnError.doneTimes(), 0);
Assert.assertEquals(readerOnError.errorTimes(), 1);
}
use of com.linkedin.r2.message.stream.entitystream.Observer 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.Observer in project rest.li by linkedin.
the class TestEntityStream method testRaceBetweenWriteAndCancel.
private void testRaceBetweenWriteAndCancel(ExecutorService executor) throws Exception {
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch finishLatch = new CountDownLatch(2);
final CountDownLatch prepareLatch = new CountDownLatch(2);
final 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);
executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (writer.remaining() > 0) {
prepareLatch.countDown();
startLatch.await();
writer.write();
}
finishLatch.countDown();
return null;
}
});
executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
prepareLatch.countDown();
startLatch.await();
reader.cancel();
finishLatch.countDown();
return null;
}
});
prepareLatch.await();
startLatch.countDown();
Assert.assertTrue(finishLatch.await(1000, TimeUnit.MILLISECONDS));
// in any case, reader shouldn't fail
Assert.assertEquals(reader.errorTimes(), 0);
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertEquals(reader.getChunkCount(), observer.getChunkCount());
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(observer.getLastEvent(), "onError");
}
use of com.linkedin.r2.message.stream.entitystream.Observer 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.Observer in project rest.li by linkedin.
the class TestClientRequestFinalizerFilter method testStreamRequestOrdering.
@Test
public void testStreamRequestOrdering() {
final AtomicReference<Observer> observerReference = new AtomicReference<>();
final AtomicInteger addObserverExecutionOrder = new AtomicInteger(0);
when(_streamResponse.getEntityStream()).thenReturn(_entityStream);
doAnswer(invocation -> {
addObserverExecutionOrder.set(_index.incrementAndGet());
observerReference.set((Observer) invocation.getArguments()[0]);
return null;
}).when(_entityStream).addObserver(anyObject());
_filterChain.onStreamRequest(null, _requestContext, null);
registerRequestFinalizer();
_filterChain.onStreamResponse(_streamResponse, _requestContext, null);
observerReference.get().onDone();
Assert.assertEquals(_firstFilter._onRequestExecutionOrder, 1);
Assert.assertEquals(_lastFilter._onRequestExecutionOrder, 2);
Assert.assertEquals(_lastFilter._onResponseExecutionOrder, 3);
Assert.assertEquals(addObserverExecutionOrder.get(), 4, "Expected observer with RequestFinalizer " + "to be added before calling the next filter.");
Assert.assertEquals(_firstFilter._onResponseExecutionOrder, 5);
Assert.assertEquals(_testRequestFinalizer._executionOrder, 6, "Expected request finalizer to be " + "executed last.");
}
Aggregations