Search in sources :

Example 1 with BarrierEvent

use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.

the class BarrierEventTest method testSignalAllByMultiThreadWithSignalLast.

@Test
public void testSignalAllByMultiThreadWithSignalLast() throws InterruptedException {
    BarrierEvent barrierEvent = new BarrierEvent();
    AtomicInteger eventCount = new AtomicInteger(0);
    AtomicInteger waitThreadInterruptedCount = new AtomicInteger(0);
    AtomicInteger signalThreadInterruptedCount = new AtomicInteger(0);
    ExecutorService executorService = Executors.newFixedThreadPool(WAIT_THREADS_COUNT + 1);
    CountDownLatch waitLatch = new CountDownLatch(WAIT_THREADS_COUNT);
    CountDownLatch signalLatch = new CountDownLatch(1);
    for (int i = 0; i < WAIT_THREADS_COUNT; i++) {
        executorService.submit(() -> {
            try {
                waitLatch.countDown();
                barrierEvent.await();
                eventCount.incrementAndGet();
            } catch (InterruptedException e) {
                waitThreadInterruptedCount.incrementAndGet();
            }
        });
    }
    executorService.submit(() -> {
        try {
            waitLatch.await();
        } catch (InterruptedException e) {
            signalThreadInterruptedCount.incrementAndGet();
        }
        barrierEvent.signalAll();
        signalLatch.countDown();
    });
    signalLatch.await();
    executorService.shutdown();
    executorService.awaitTermination(1L, TimeUnit.SECONDS);
    Assert.assertEquals(WAIT_THREADS_COUNT, eventCount.get());
    Assert.assertEquals(0, waitThreadInterruptedCount.get());
    Assert.assertEquals(0, signalThreadInterruptedCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Example 2 with BarrierEvent

use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.

the class BarrierEventTest method testSignalByMultiThreadWithSignalFirst.

@Test
public void testSignalByMultiThreadWithSignalFirst() throws InterruptedException {
    BarrierEvent barrierEvent = new BarrierEvent();
    AtomicInteger eventCount = new AtomicInteger(0);
    AtomicInteger waitThreadInterruptedCount = new AtomicInteger(0);
    ExecutorService executorService = Executors.newFixedThreadPool(WAIT_THREADS_COUNT + 1);
    CountDownLatch waitLatch = new CountDownLatch(WAIT_THREADS_COUNT);
    CountDownLatch signalLatch = new CountDownLatch(1);
    for (int i = 0; i < WAIT_THREADS_COUNT; i++) {
        executorService.submit(() -> {
            try {
                signalLatch.await();
                barrierEvent.await();
                eventCount.incrementAndGet();
            } catch (InterruptedException e) {
                waitThreadInterruptedCount.incrementAndGet();
            } finally {
                waitLatch.countDown();
            }
        });
    }
    executorService.submit(() -> {
        barrierEvent.signal();
        signalLatch.countDown();
    });
    executorService.shutdown();
    executorService.awaitTermination(2L, TimeUnit.SECONDS);
    waitLatch.await();
    Assert.assertEquals(10, eventCount.get());
    Assert.assertEquals(0, waitThreadInterruptedCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Example 3 with BarrierEvent

use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.

the class BarrierEventTest method testSignalByMultiThreadWithSignalLast.

@Test
public void testSignalByMultiThreadWithSignalLast() throws InterruptedException {
    BarrierEvent barrierEvent = new BarrierEvent();
    AtomicInteger eventCount = new AtomicInteger(0);
    AtomicInteger waitThreadInterruptedCount = new AtomicInteger(0);
    AtomicInteger signalThreadInterruptedCount = new AtomicInteger(0);
    ExecutorService executorService = Executors.newFixedThreadPool(WAIT_THREADS_COUNT + 1);
    CountDownLatch waitLatch = new CountDownLatch(WAIT_THREADS_COUNT);
    CountDownLatch signalLatch = new CountDownLatch(1);
    for (int i = 0; i < WAIT_THREADS_COUNT; i++) {
        executorService.submit(() -> {
            try {
                waitLatch.countDown();
                barrierEvent.await();
                eventCount.incrementAndGet();
            } catch (InterruptedException e) {
                waitThreadInterruptedCount.incrementAndGet();
            }
        });
    }
    executorService.submit(() -> {
        try {
            waitLatch.await();
        } catch (InterruptedException e) {
            signalThreadInterruptedCount.incrementAndGet();
        }
        barrierEvent.signal();
        signalLatch.countDown();
    });
    signalLatch.await();
    executorService.shutdownNow();
    executorService.awaitTermination(1L, TimeUnit.SECONDS);
    Assert.assertEquals(1, eventCount.get());
    Assert.assertEquals(WAIT_THREADS_COUNT - 1, waitThreadInterruptedCount.get());
    Assert.assertEquals(0, signalThreadInterruptedCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Example 4 with BarrierEvent

use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.

the class BarrierEventTest method testSignalAllByMultiThreadWithSignalAwaitConcurrent.

@Test
public void testSignalAllByMultiThreadWithSignalAwaitConcurrent() throws InterruptedException {
    BarrierEvent barrierEvent = new BarrierEvent();
    AtomicInteger eventCount = new AtomicInteger(0);
    AtomicInteger waitThreadInterruptedCount = new AtomicInteger(0);
    AtomicInteger signalThreadInterruptedCount = new AtomicInteger(0);
    ExecutorService executorService = Executors.newFixedThreadPool(WAIT_THREADS_COUNT + 1);
    CountDownLatch syncLatch = new CountDownLatch(1);
    for (int i = 0; i < WAIT_THREADS_COUNT; i++) {
        executorService.submit(() -> {
            try {
                syncLatch.await();
                barrierEvent.await();
                eventCount.incrementAndGet();
            } catch (InterruptedException e) {
                waitThreadInterruptedCount.incrementAndGet();
            }
        });
    }
    executorService.submit(() -> {
        try {
            syncLatch.await();
        } catch (InterruptedException e) {
            signalThreadInterruptedCount.incrementAndGet();
        }
        barrierEvent.signalAll();
    });
    syncLatch.countDown();
    executorService.shutdown();
    executorService.awaitTermination(1L, TimeUnit.SECONDS);
    Assert.assertEquals(WAIT_THREADS_COUNT, eventCount.get());
    Assert.assertEquals(0, waitThreadInterruptedCount.get());
    Assert.assertEquals(0, signalThreadInterruptedCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Example 5 with BarrierEvent

use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.

the class BarrierEventTest method testSignalAll.

@Test
public void testSignalAll() throws InterruptedException {
    BarrierEvent barrierEvent = new BarrierEvent();
    boolean signaled = barrierEvent.await(1L);
    Assert.assertFalse(signaled);
    barrierEvent.signalAll();
    signaled = barrierEvent.await(1L);
    Assert.assertTrue(signaled);
}
Also used : BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Aggregations

BarrierEvent (com.baidu.hugegraph.concurrent.BarrierEvent)12 Test (org.junit.Test)10 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ExecutorService (java.util.concurrent.ExecutorService)5 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)2 E (com.baidu.hugegraph.util.E)2 Log (com.baidu.hugegraph.util.Log)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ByteSequence (io.etcd.jetcd.ByteSequence)2 Client (io.etcd.jetcd.Client)2 KV (io.etcd.jetcd.KV)2 KeyValue (io.etcd.jetcd.KeyValue)2 Watch (io.etcd.jetcd.Watch)2 DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)2 GetResponse (io.etcd.jetcd.kv.GetResponse)2 DeleteOption (io.etcd.jetcd.options.DeleteOption)2 GetOption (io.etcd.jetcd.options.GetOption)2 SortOrder (io.etcd.jetcd.options.GetOption.SortOrder)2 WatchOption (io.etcd.jetcd.options.WatchOption)2