use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestEntityStream method testObserverThrow.
public void testObserverThrow(Observer observer, Throwable writeError) {
TestWriter writer = new TestWriter();
ControlReader reader = new ControlReader();
EntityStream es = EntityStreams.newEntityStream(writer);
es.addObserver(observer);
es.setReader(reader);
reader.read(1);
writer.write();
writer.done();
writer.error(writeError);
Assert.assertEquals(writer.abortedTimes(), 0);
Assert.assertEquals(reader.getChunkCount(), 1);
Assert.assertEquals(reader.doneTimes(), 1);
Assert.assertEquals(reader.errorTimes(), 0);
writer = new TestWriter();
reader = new ControlReader();
es = EntityStreams.newEntityStream(writer);
es.addObserver(observer);
es.setReader(reader);
reader.read(1);
writer.write();
writer.error(writeError);
Assert.assertEquals(writer.abortedTimes(), 0);
Assert.assertEquals(reader.getChunkCount(), 1);
Assert.assertEquals(reader.errorTimes(), 1);
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestEntityStream method testRaceBetweenAbnormalAbortAndCancel.
private void testRaceBetweenAbnormalAbortAndCancel(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() {
@Override
public void onDataAvailable(ByteString data) {
try {
prepareLatch.countDown();
startLatch.await();
} catch (Exception ex) {
// ...
}
throw new RuntimeException("broken reader throws");
}
};
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) {
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));
// we should always fail because cancel on reader side wouldn't cause cancel action if
// writer is already in the writing process
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(observer.getChunkCount(), 1);
Assert.assertEquals(observer.getLastEvent(), "onError");
Assert.assertEquals(reader.errorTimes(), 1);
Assert.assertEquals(reader.getChunkCount(), 0);
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream 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.EntityStream 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.EntityStream 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");
}
Aggregations