use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class TestEntityStream method testObserverThrowRuntimeException.
/**
* This test will check the correct behavior in case of a Runtime Exception for the observer.
* Note the Runtime Exception is not the only unchecked exception, and we have to consider also Error
* which is the other unchecked throwable
*/
@Test
public void testObserverThrowRuntimeException() {
Observer observer = new TestObserver() {
@Override
public void onDone() {
throw new RuntimeException("broken observer throws");
}
@Override
public void onDataAvailable(ByteString data) {
throw new RuntimeException("broken observer throws");
}
@Override
public void onError(Throwable ex) {
throw new RuntimeException("broken observer throws");
}
};
Exception ex = new RuntimeException("writer has problem");
testObserverThrow(observer, ex);
}
use of com.linkedin.data.ByteString 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.data.ByteString 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.data.ByteString in project rest.li by linkedin.
the class TestEntityStream method testNoStackOverflow.
@Test
public void testNoStackOverflow() throws Exception {
Writer dumbWriter = new Writer() {
WriteHandle _wh;
long _count = 0;
final int _total = 1024 * 1024 * 1024;
@Override
public void onInit(WriteHandle wh) {
_wh = wh;
}
@Override
public void onWritePossible() {
while (_wh.remaining() > 0 && _count < _total) {
byte[] bytes = new byte[(int) Math.min(4096, _total - _count)];
_wh.write(ByteString.copy(bytes));
_count += bytes.length;
}
if (_count >= _total) {
_wh.done();
}
}
@Override
public void onAbort(Throwable ex) {
// do nothing
}
};
Reader dumbReader = new Reader() {
ReadHandle _rh;
@Override
public void onInit(ReadHandle rh) {
_rh = rh;
_rh.request(1);
}
@Override
public void onDataAvailable(ByteString data) {
_rh.request(1);
}
@Override
public void onDone() {
}
@Override
public void onError(Throwable e) {
}
};
EntityStreams.newEntityStream(dumbWriter).setReader(dumbReader);
}
use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class TestStreamFilterAdapters method testRequestFilterAdapterChangeRequest.
@Test
public void testRequestFilterAdapterChangeRequest() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onRequest(req.builder().setEntity(req.getEntity().asString("UTF8").replace('1', '0').getBytes()).build(), requestContext, wireAttrs);
}
});
fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
StreamRequest capturedReq = _afterFilter.getRequest();
Assert.assertEquals(capturedReq.getURI(), SIMPLE_URI);
capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("shouldn't have error");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "02345");
}
}));
}
Aggregations