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

add BarrierEvent #60

merged 8 commits into from
Jan 11, 2021

Conversation

houzhizhen
Copy link
Contributor

No description provided.

@codecov
Copy link

codecov bot commented Jan 7, 2021

Codecov Report

Merging #60 (7ffdf85) into master (37de7be) will increase coverage by 0.06%.
The diff coverage is 91.42%.

Impacted file tree graph

@@             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     
Impacted Files Coverage Δ Complexity Δ
...ava/com/baidu/hugegraph/version/CommonVersion.java 50.00% <ø> (ø) 1.00 <0.00> (ø)
...a/com/baidu/hugegraph/concurrent/BarrierEvent.java 91.42% <91.42%> (ø) 8.00 <8.00> (?)
...n/java/com/baidu/hugegraph/concurrent/KeyLock.java 78.43% <0.00%> (-1.97%) 25.00% <0.00%> (-1.00%)
.../main/java/com/baidu/hugegraph/event/EventHub.java 80.55% <0.00%> (-1.39%) 26.00% <0.00%> (-1.00%)
.../main/java/com/baidu/hugegraph/perf/Stopwatch.java 87.75% <0.00%> (+2.04%) 21.00% <0.00%> (+1.00%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 37de7be...7ffdf85. Read the comment docs.

}
}

public boolean await(long time) 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.

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'.",
Copy link
Contributor

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 {

Copy link
Contributor

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);
}
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

@@ -65,6 +68,40 @@ public void testSignal() throws InterruptedException {
Assert.assertTrue(signaled);
}

@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.await();
result.incrementAndGet();
} 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

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

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

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

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()

signalLatch.await();
TimeUnit.MICROSECONDS.sleep(100);
TimeUnit.MILLISECONDS.sleep(10L);
Copy link
Contributor

Choose a reason for hiding this comment

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

must sleep?

Copy link
Contributor Author

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.

Copy link
Contributor

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());
Copy link
Contributor

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;
Copy link
Contributor

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);
Copy link
Contributor

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

javeme
javeme previously approved these changes Jan 10, 2021
@@ -6,7 +6,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.8.2</version>
Copy link
Contributor

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

volatile

@houzhizhen houzhizhen merged commit f4e861b into master Jan 11, 2021
@houzhizhen houzhizhen deleted the barrier-event branch January 11, 2021 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants