-
Notifications
You must be signed in to change notification settings - Fork 45
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
add BarrierEvent #60
Conversation
Codecov Report
@@ Coverage Diff @@
## master #60 +/- ##
============================================
+ Coverage 84.53% 84.60% +0.06%
- Complexity 738 745 +7
============================================
Files 57 58 +1
Lines 2115 2150 +35
Branches 316 320 +4
============================================
+ Hits 1788 1819 +31
Misses 188 188
- Partials 139 143 +4
Continue to review full report at Codecov.
|
} | ||
} | ||
|
||
public boolean await(long time) throws InterruptedException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also add doc
and rename to timeout
|
||
public boolean await(long time) throws InterruptedException { | ||
E.checkArgument(time >= 0L, | ||
"The time must be >= 0, but got '%d'.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
timeout
import com.baidu.hugegraph.testutil.Assert; | ||
|
||
public class BarrierEventTest { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test for every method
barrierEvent.signalAll(); | ||
signaled = barrierEvent.await(1L); | ||
Assert.assertTrue(signaled); | ||
} |
There was a problem hiding this comment.
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
@@ -65,6 +68,40 @@ public void testSignal() throws InterruptedException { | |||
Assert.assertTrue(signaled); | |||
} | |||
|
|||
@Test(timeout = 5000) | |||
public void testSignalMultiThread() throws InterruptedException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
testSignalByMultiThread
barrierEvent.await(); | ||
result.incrementAndGet(); | ||
} catch (InterruptedException e) { | ||
// Do nothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interruptedCount
try { | ||
waitLatch.countDown(); | ||
barrierEvent.await(); | ||
result.incrementAndGet(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eventCount
barrierEvent.signal(); | ||
signalLatch.countDown(); | ||
}); | ||
signalThread.start(); |
There was a problem hiding this comment.
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
barrierEvent.signalAll(); | ||
latch.countDown(); | ||
}); | ||
signalThread.start(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
waitLatch.await(); | ||
signalLatch.await(); | ||
TimeUnit.MICROSECONDS.sleep(100); | ||
executorService.shutdownNow(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also call executorService.await()
…ignalAll after await with multy thread.
signalLatch.await(); | ||
TimeUnit.MICROSECONDS.sleep(100); | ||
TimeUnit.MILLISECONDS.sleep(10L); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
must sleep?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if reach this line, it indicates the signal thread has signaled, but the wait thread may not be notified and executed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please explain why instead of what,I mean seems we can remove the sleep
executorService.awaitTermination(1L, TimeUnit.SECONDS); | ||
Assert.assertEquals(waitThreadNum, eventCount.get()); | ||
Assert.assertEquals(0, waitThreadInterruptedCount.get()); | ||
Assert.assertEquals(0, signalThreadInterruptedCount.get()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add testSignalAllByMultiThreadWithSignalAwaitConcurrent for signalAll and await at the same time
AtomicInteger eventCount = new AtomicInteger(0); | ||
AtomicInteger waitThreadInterruptedCount = new AtomicInteger(0); | ||
AtomicInteger signalThreadInterruptedCount = new AtomicInteger(0); | ||
int waitThreadNum = 10; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
define const var
signalLatch.await(); | ||
TimeUnit.MICROSECONDS.sleep(100); | ||
TimeUnit.MILLISECONDS.sleep(10L); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please explain why instead of what,I mean seems we can remove the sleep
@@ -6,7 +6,7 @@ | |||
|
|||
<groupId>com.baidu.hugegraph</groupId> | |||
<artifactId>hugegraph-common</artifactId> | |||
<version>1.8.2</version> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also change line 269
|
||
private final Lock lock = new ReentrantLock(); | ||
private final Condition cond = lock.newCondition(); | ||
private boolean signaled = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
volatile
No description provided.