use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestEntityStream method testEntityStream.
@Test
public void testEntityStream() throws Exception {
TestWriter writer = new TestWriter();
ControlReader reader = new ControlReader();
TestObserver ob1 = new TestObserver();
TestObserver ob2 = new TestObserver();
EntityStream stream = EntityStreams.newEntityStream(writer);
stream.addObserver(ob1);
stream.addObserver(ob2);
// write is not possible without a reader
Assert.assertEquals(writer.getWritePossibleCount(), 0);
stream.setReader(reader);
// write is not possible before reader reads
Assert.assertEquals(writer.getWritePossibleCount(), 0);
reader.read(1);
// write become possible
Assert.assertEquals(writer.getWritePossibleCount(), 1);
Assert.assertEquals(writer.remaining(), 1);
writer.write();
Assert.assertEquals(writer.remaining(), 0);
reader.read(10);
// write again become possible
Assert.assertEquals(writer.getWritePossibleCount(), 2);
Assert.assertEquals(writer.remaining(), 10);
while (writer.remaining() > 1) {
writer.write();
}
Assert.assertEquals(writer.remaining(), 1);
reader.read(10);
// write hasn't become impossible when reader reads again, so onWritePossible should not have been invoked again
Assert.assertEquals(writer.getWritePossibleCount(), 2);
while (writer.remaining() > 0) {
writer.write();
}
Assert.assertEquals(ob1.getChunkCount(), 21);
Assert.assertEquals(ob2.getChunkCount(), 21);
Assert.assertEquals(reader.getChunkCount(), 21);
try {
writer.write();
Assert.fail("should fail with IllegalStateException");
} catch (IllegalStateException ex) {
// expected
}
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestEntityStream method testCancelWhenNotWritePossible.
@Test
public void testCancelWhenNotWritePossible() 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(10);
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
final CountDownLatch latch = new CountDownLatch(1);
scheduler.schedule(new Runnable() {
@Override
public void run() {
reader.cancel();
latch.countDown();
}
}, 50, TimeUnit.MILLISECONDS);
while (writer.remaining() > 0) {
writer.write();
}
Assert.assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertEquals(observer.getChunkCount(), 10);
Assert.assertEquals(observer.doneTimes(), 0);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(observer.getLastEvent(), "onError");
Assert.assertEquals(reader.getChunkCount(), 10);
Assert.assertEquals(reader.doneTimes(), 0);
Assert.assertEquals(reader.errorTimes(), 0);
scheduler.shutdown();
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream in project rest.li by linkedin.
the class TestEntityStream method testWriterInitError.
@Test
public void testWriterInitError() 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();
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(new Runnable() {
@Override
public void run() {
es.setReader(reader);
reader.read(5);
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 testRaceBetweenErrorAndCancel.
private void testRaceBetweenErrorAndCancel(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() > 100) {
writer.write();
}
prepareLatch.countDown();
startLatch.await();
writer.error(new RuntimeException("writer has problem"));
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));
// if error wins the race
if (reader.errorTimes() > 0) {
Assert.assertEquals(reader.doneTimes(), 0);
Assert.assertEquals(observer.doneTimes(), 0);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(writer.abortedTimes(), 0);
} else // if cancel wins the race
{
Assert.assertEquals(observer.doneTimes(), 0);
Assert.assertEquals(observer.errorTimes(), 1);
Assert.assertEquals(writer.abortedTimes(), 1);
Assert.assertEquals(reader.doneTimes(), 0);
Assert.assertEquals(reader.errorTimes(), 0);
}
}
use of com.linkedin.r2.message.stream.entitystream.EntityStream 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();
}
Aggregations