Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add BarrierEvent #60

Merged
merged 8 commits into from
Jan 11, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.baidu.hugegraph.unit.concurrent;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;
Expand Down Expand Up @@ -65,6 +68,40 @@ public void testSignal() throws InterruptedException {
Assert.assertTrue(signaled);
javeme marked this conversation as resolved.
Show resolved Hide resolved
}

@Test(timeout = 5000)
public void testSignalMultiThread() throws InterruptedException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testSignalByMultiThread

BarrierEvent barrierEvent = new BarrierEvent();
AtomicInteger result = new AtomicInteger(0);
int waitThreadNum = 10;
ExecutorService executorService =
Executors.newFixedThreadPool(waitThreadNum);
CountDownLatch waitLatch = new CountDownLatch(waitThreadNum);
CountDownLatch signalLatch = new CountDownLatch(1);
for (int i = 0; i < waitThreadNum; i++) {
executorService.submit(() -> {
try {
waitLatch.countDown();
barrierEvent.await();
result.incrementAndGet();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventCount

} catch (InterruptedException e) {
// Do nothing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interruptedCount

}
});
}

Thread signalThread = new Thread(() -> {
barrierEvent.signal();
signalLatch.countDown();
});
signalThread.start();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test signal first or last

waitLatch.await();
signalLatch.await();
TimeUnit.MICROSECONDS.sleep(100);
executorService.shutdownNow();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also call executorService.await()


Assert.assertEquals(1, result.get());
}

@Test
public void testSignalAll() throws InterruptedException {
BarrierEvent barrierEvent = new BarrierEvent();
Expand All @@ -74,4 +111,34 @@ public void testSignalAll() throws InterruptedException {
signaled = barrierEvent.await(1L);
Assert.assertTrue(signaled);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test for multi-threaded scene


@Test(timeout = 5000)
public void testSignalAllMultiThread() throws InterruptedException {
BarrierEvent barrierEvent = new BarrierEvent();
AtomicInteger result = new AtomicInteger(0);
int waitThreadNum = 10;
ExecutorService executorService =
Executors.newFixedThreadPool(waitThreadNum);
CountDownLatch latch = new CountDownLatch(waitThreadNum + 1);
for (int i = 0; i < waitThreadNum; i++) {
executorService.submit(() -> {
try {
barrierEvent.await();
result.incrementAndGet();
latch.countDown();
} catch (InterruptedException e) {
// Do nothing
}
});
}
Thread signalThread = new Thread(() -> {
barrierEvent.signalAll();
latch.countDown();
});
signalThread.start();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

latch.await();
executorService.shutdownNow();

Assert.assertEquals(waitThreadNum, result.get());
}
}