Search in sources :

Example 1 with ReusableLatch

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);
}
Also used : FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) SneakyThrows(lombok.SneakyThrows) IOException(java.io.IOException) ReusableLatch(io.pravega.common.util.ReusableLatch) WireCommands(io.pravega.shared.protocol.netty.WireCommands) FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 2 with ReusableLatch

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);
}
Also used : ReusableLatch(io.pravega.common.util.ReusableLatch) ExecutorService(java.util.concurrent.ExecutorService) Cleanup(lombok.Cleanup) Vector(java.util.Vector) Test(org.junit.Test)

Example 3 with ReusableLatch

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);
}
Also used : ReusableLatch(io.pravega.common.util.ReusableLatch) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 4 with ReusableLatch

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);
}
Also used : ReusableLatch(io.pravega.common.util.ReusableLatch) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 5 with ReusableLatch

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);
}
Also used : ReusableLatch(io.pravega.common.util.ReusableLatch) ExecutorService(java.util.concurrent.ExecutorService) Cleanup(lombok.Cleanup) Vector(java.util.Vector) Test(org.junit.Test)

Aggregations

ReusableLatch (io.pravega.common.util.ReusableLatch)30 Test (org.junit.Test)30 Cleanup (lombok.Cleanup)25 CompletableFuture (java.util.concurrent.CompletableFuture)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 lombok.val (lombok.val)14 AssertExtensions (io.pravega.test.common.AssertExtensions)13 IntentionalException (io.pravega.test.common.IntentionalException)13 IOException (java.io.IOException)13 TimeUnit (java.util.concurrent.TimeUnit)13 Exceptions (io.pravega.common.Exceptions)12 ArrayList (java.util.ArrayList)12 ByteArraySegment (io.pravega.common.util.ByteArraySegment)11 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)11 Duration (java.time.Duration)11 HashMap (java.util.HashMap)11 CancellationException (java.util.concurrent.CancellationException)11 CompletionException (java.util.concurrent.CompletionException)11 Assert (org.junit.Assert)11 Futures (io.pravega.common.concurrent.Futures)10