-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from robdavid/feature/blocking-wait-group
Reduce CPU usage of wait group by blocking. Thanks to @robdavid
- Loading branch information
Showing
2 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require "./spec_helper" | ||
|
||
module XMPP | ||
describe WaitGroup do | ||
|
||
it "Should not block on creation" do | ||
wg = WaitGroup.new | ||
wg.wait | ||
end | ||
|
||
it "Should block whilst not done" do | ||
wg = WaitGroup.new | ||
wg.add.should eq 1 | ||
progress = Atomic(Int32).new(0) | ||
spawn { progress.add(1); wg.wait; progress.add(1) } | ||
sleep 5.milliseconds | ||
# Should get to wait but not beyond it | ||
progress.get.should eq 1 | ||
wg.done.should eq 0 | ||
while progress.get != 2 | ||
sleep 1.milliseconds | ||
end | ||
end | ||
|
||
it "Should support multiple waiters" do | ||
wg = WaitGroup.new | ||
wg.add.should eq 1 | ||
unblocked = Atomic(Int32).new(0) | ||
nwaiters = 10 | ||
(1..nwaiters).each { spawn { wg.wait; unblocked.add(1) } } | ||
sleep 5.milliseconds | ||
unblocked.get.should eq 0 | ||
wg.done.should eq 0 | ||
while unblocked.get != nwaiters | ||
sleep 1.milliseconds | ||
end | ||
end | ||
|
||
it "Won't block again after done" do | ||
wg = WaitGroup.new | ||
wg.add | ||
wg.done | ||
wg.done?.should be_true | ||
wg.wait | ||
wg.add | ||
wg.done?.should be_true | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters