use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class CommitLogReaderTest method testOrderedObserverLifycycle.
@Test
public void testOrderedObserverLifycycle() {
StreamElement update = StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), 0, new byte[] { 1, 2 });
AtomicInteger mask = new AtomicInteger();
CommitLogObserver inner = new CommitLogObserver() {
@Override
public void onCompleted() {
mask.updateAndGet(m -> m | 0x1);
}
@Override
public void onCancelled() {
mask.updateAndGet(m -> m | 0x2);
}
@Override
public boolean onError(Throwable error) {
mask.updateAndGet(m -> m | 0x4);
return true;
}
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
mask.updateAndGet(m -> m | 0x8);
return true;
}
@Override
public void onRepartition(OnRepartitionContext context) {
mask.updateAndGet(m -> m | 0x10);
}
@Override
public void onIdle(OnIdleContext context) {
mask.updateAndGet(m -> m | 0x20);
}
};
assertEquals(0x0, mask.get());
inner.onCompleted();
assertEquals(0x1, mask.get());
inner.onCancelled();
assertEquals(0x3, mask.get());
inner.onError(null);
assertEquals(0x7, mask.get());
inner.onNext(update, asOnNextContext(0));
assertEquals(0x0F, mask.get());
inner.onRepartition(Collections::emptyList);
assertEquals(0x1F, mask.get());
inner.onIdle(() -> 0);
assertEquals(0x3F, mask.get());
}
use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class CommitLogReaderTest method testObserveOrdered.
@Test(timeout = 10000)
public void testObserveOrdered() throws InterruptedException {
List<StreamElement> received = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(100);
reader.observe("test", CommitLogObservers.withSortBuffer(new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
received.add(ingest);
latch.countDown();
context.confirm();
return true;
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
}, Duration.ofMillis(500)));
long now = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 99 - i, new byte[] { 1, 2 }), (succ, exc) -> {
});
}
// put one latecomer to test it is dropped
writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 99 - 10000, new byte[] { 1, 2 }), (succ, exc) -> {
});
latch.await();
assertEquals(100, received.size());
assertEquals(LongStream.range(0, 100).boxed().collect(Collectors.toList()), received.stream().map(s -> s.getStamp() - now).collect(Collectors.toList()));
}
use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class CommitLogReaderTest method testObserveWithError.
@Test(timeout = 10000)
public void testObserveWithError() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Throwable> caught = new AtomicReference<>();
reader.observe("test", new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
throw new RuntimeException("fail");
}
@Override
public boolean onError(Throwable error) {
caught.set(error);
latch.countDown();
return false;
}
});
writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2 }), (succ, exc) -> {
});
latch.await();
assertNotNull(caught.get());
}
use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class CommitLogReaderTest method testObserveOrderedPerPartition.
@Test(timeout = 10000)
public void testObserveOrderedPerPartition() throws InterruptedException {
List<StreamElement> received = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(100);
reader.observe("test", CommitLogObservers.withSortBufferWithinPartition(new CommitLogObserver() {
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
received.add(ingest);
latch.countDown();
context.confirm();
return true;
}
@Override
public boolean onError(Throwable error) {
throw new RuntimeException(error);
}
}, Duration.ofMillis(500)));
long now = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 99 - i, new byte[] { 1, 2 }), (succ, exc) -> {
});
}
// put one latecomer to test it is dropped
writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 99 - 10000, new byte[] { 1, 2 }), (succ, exc) -> {
});
latch.await();
assertEquals(100, received.size());
assertEquals(LongStream.range(0, 100).boxed().collect(Collectors.toList()), received.stream().map(s -> s.getStamp() - now).collect(Collectors.toList()));
}
use of cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext in project proxima-platform by O2-Czech-Republic.
the class RetryableLogObserverTest method testNonRetryableException.
@Test
public void testNonRetryableException() {
final int numRetries = 10;
final AtomicReference<Throwable> exception = new AtomicReference<>();
final RetryableLogObserver observer = new RetryableLogObserver("test", numRetries, withStrategy(exception::set, TerminationStrategy.RETHROW), false, new CommitLogObserver() {
@Override
public boolean onError(Throwable error) {
// Non-Retryable.
return false;
}
@Override
public boolean onNext(StreamElement ingest, OnNextContext context) {
throw new UnsupportedOperationException("Not implemented.");
}
});
try {
observer.onError(new OutOfMemoryError());
Assert.fail("Should have thrown exception");
} catch (OutOfMemoryError ex) {
Assert.assertEquals(ex, exception.get());
}
try {
observer.onError(new Exception("Test."));
Assert.fail("Should have thrown exception");
} catch (IllegalStateException ex) {
Assert.assertEquals(ex.getCause(), exception.get());
}
}
Aggregations