use of com.linkedin.r2.message.stream.entitystream.ReadHandle 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.ReadHandle in project rest.li by linkedin.
the class TestStreamEcho method testBackPressureEcho.
@Test
public void testBackPressureEcho() throws Exception {
for (Client client : clients()) {
TimedBytesWriter writer = new TimedBytesWriter(SMALL_BYTES_NUM, BYTE);
StreamRequest request = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, ECHO_URI)).build(EntityStreams.newEntityStream(writer));
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
final Callback<None> readerCallback = getReaderCallback(latch, error);
final TimedBytesReader reader = new TimedBytesReader(BYTE, readerCallback) {
int count = 0;
@Override
protected void requestMore(final ReadHandle rh) {
count++;
if (count % 16 == 0) {
_scheduler.schedule(new Runnable() {
@Override
public void run() {
rh.request(1);
}
}, INTERVAL, TimeUnit.MILLISECONDS);
} else {
rh.request(1);
}
}
};
Callback<StreamResponse> callback = getCallback(status, readerCallback, reader);
client.streamRequest(request, callback);
latch.await(60000, TimeUnit.MILLISECONDS);
Assert.assertNull(error.get());
Assert.assertEquals(status.get(), RestStatus.OK);
Assert.assertEquals(reader.getTotalBytes(), SMALL_BYTES_NUM);
Assert.assertTrue(reader.allBytesCorrect());
long clientSendTimespan = writer.getStopTime() - writer.getStartTime();
long clientReceiveTimespan = reader.getStopTime() - reader.getStartTime();
double diff = Math.abs(clientReceiveTimespan - clientSendTimespan);
double diffRatio = diff / clientSendTimespan;
// make it generous to reduce the chance occasional test failures
Assert.assertTrue(diffRatio < 0.5, "Send/receive time delta is " + diff + " but expected to be less than 0.5. Send time span is " + clientSendTimespan + " and receive time span is " + clientReceiveTimespan);
}
}
Aggregations