use of io.pravega.common.util.ReusableLatch in project pravega by pravega.
the class TcpClientConnectionTest method testAwaitCallbackCompletionBeforeConnectionDrop.
@Test
public void testAwaitCallbackCompletionBeforeConnectionDrop() throws InterruptedException {
ReusableLatch blockCallbackLatch = new ReusableLatch(false);
ReusableLatch isCallbackInvokedLatch = new ReusableLatch(false);
ReplyProcessor rp = spy(new FailingReplyProcessor() {
@SneakyThrows
@Override
public void segmentIsSealed(WireCommands.SegmentIsSealed segmentIsSealed) {
isCallbackInvokedLatch.release();
blockCallbackLatch.await();
}
@Override
public void connectionDropped() {
}
@Override
public void processingFailure(Exception error) {
fail("This call back is not expected");
}
});
TestInputStream tcpStream = new TestInputStream(new WireCommands.SegmentIsSealed(1L, "seg", "", 1234L));
TcpClientConnection.ConnectionReader reader = new TcpClientConnection.ConnectionReader("reader", tcpStream, rp, mock(FlowToBatchSizeTracker.class));
// Trigger a read.
reader.start();
tcpStream.lastByteLatch.release();
// Wait until the ReplyProcessor callback is invoked.
isCallbackInvokedLatch.await();
// Verify if ConnectionReader.stop() is blocked until the ReplyProcessor callback finish its execution.
assertBlocks(reader::stop, blockCallbackLatch::release);
verify(rp, times(1)).process(any(WireCommands.SegmentIsSealed.class));
verify(rp, times(1)).segmentIsSealed(any(WireCommands.SegmentIsSealed.class));
// Verify that connectionDropped callback is invoked at the end.
verify(rp, times(1)).connectionDropped();
// Ensure no other callbacks are invoked.
verifyNoMoreInteractions(rp);
}
use of io.pravega.common.util.ReusableLatch in project pravega by pravega.
the class MultiKeyLatestItemSequentialProcessorTest method testSkipsOverItemsSingleKey.
@Test
public void testSkipsOverItemsSingleKey() throws InterruptedException {
@Cleanup("shutdown") ExecutorService pool = ExecutorServiceHelpers.newScheduledThreadPool(1, "test");
ReusableLatch startedLatch = new ReusableLatch(false);
ReusableLatch latch = new ReusableLatch(false);
Vector<String> processed = new Vector<>();
MultiKeyLatestItemSequentialProcessor<String, String> processor = new MultiKeyLatestItemSequentialProcessor<>((k, v) -> {
startedLatch.release();
latch.awaitUninterruptibly();
processed.add(v);
}, pool);
processor.updateItem("k", "a");
processor.updateItem("k", "b");
processor.updateItem("k", "c");
startedLatch.await();
latch.release();
ExecutorServiceHelpers.shutdown(pool);
assertEquals(ImmutableList.of("a", "c"), processed);
}
use of io.pravega.common.util.ReusableLatch in project pravega by pravega.
the class MultiKeyLatestItemSequentialProcessorTest method testMultipleKeyParallelInvocation.
@Test
public void testMultipleKeyParallelInvocation() {
@Cleanup("shutdown") ExecutorService pool = ExecutorServiceHelpers.newScheduledThreadPool(2, "test");
CountDownLatch key1Check = new CountDownLatch(2);
CountDownLatch key2Check = new CountDownLatch(2);
ReusableLatch latch = new ReusableLatch(false);
MultiKeyLatestItemSequentialProcessor<String, String> processor = new MultiKeyLatestItemSequentialProcessor<>((k, v) -> {
if (k.equals("k1")) {
// count down latch only for k1
key1Check.countDown();
} else {
// count down latch only for k2.
key2Check.countDown();
}
latch.awaitUninterruptibly();
}, pool);
processor.updateItem("k1", "a");
processor.updateItem("k1", "b");
processor.updateItem("k1", "c");
processor.updateItem("k2", "x");
processor.updateItem("k2", "y");
processor.updateItem("k2", "z");
// validate parallel invocation for the same key does not happen.
AssertExtensions.assertBlocks(() -> key1Check.await(), () -> key1Check.countDown());
AssertExtensions.assertBlocks(() -> key2Check.await(), () -> key2Check.countDown());
latch.release();
ExecutorServiceHelpers.shutdown(pool);
}
use of io.pravega.common.util.ReusableLatch in project pravega by pravega.
the class MultiKeyLatestItemSequentialProcessorTest method testNotCalledInParallel.
@Test
public void testNotCalledInParallel() {
@Cleanup("shutdown") ExecutorService pool = ExecutorServiceHelpers.newScheduledThreadPool(2, "test");
CountDownLatch parCheck = new CountDownLatch(2);
ReusableLatch latch = new ReusableLatch(false);
MultiKeyLatestItemSequentialProcessor<String, String> processor = new MultiKeyLatestItemSequentialProcessor<>((k, v) -> {
parCheck.countDown();
latch.awaitUninterruptibly();
}, pool);
processor.updateItem("k", "a");
processor.updateItem("k", "b");
processor.updateItem("k", "c");
AssertExtensions.assertBlocks(() -> parCheck.await(), () -> parCheck.countDown());
latch.release();
ExecutorServiceHelpers.shutdown(pool);
}
use of io.pravega.common.util.ReusableLatch in project pravega by pravega.
the class MultiKeyLatestItemSequentialProcessorTest method testSkipsOverItemsMultipleKey.
@Test
public void testSkipsOverItemsMultipleKey() throws InterruptedException {
@Cleanup("shutdown") ExecutorService pool = ExecutorServiceHelpers.newScheduledThreadPool(1, "test");
ReusableLatch startedLatch = new ReusableLatch(false);
ReusableLatch latch = new ReusableLatch(false);
Vector<String> processed = new Vector<>();
MultiKeyLatestItemSequentialProcessor<String, String> processor = new MultiKeyLatestItemSequentialProcessor<>((k, v) -> {
startedLatch.release();
latch.awaitUninterruptibly();
processed.add(v);
}, pool);
processor.updateItem("k", "a");
processor.updateItem("k1", "x");
processor.updateItem("k", "b");
processor.updateItem("k1", "y");
processor.updateItem("k", "c");
processor.updateItem("k1", "z");
startedLatch.await();
latch.release();
ExecutorServiceHelpers.shutdown(pool);
assertEquals(ImmutableList.of("a", "c", "x", "z"), processed);
}
Aggregations