Skip to content

Commit

Permalink
Make sure threads are actually waiting in other ConditionVariable specs
Browse files Browse the repository at this point in the history
* The thread can also have the status "sleep" very shortly when calling Mutex#lock.
eregon committed Jul 23, 2015

Verified

This commit was signed with the committer’s verified signature.
headius Charles Oliver Nutter
1 parent acb9129 commit 8c884c0
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 6 additions & 1 deletion spec/ruby/library/conditionvariable/broadcast_spec.rb
Original file line number Diff line number Diff line change
@@ -10,13 +10,18 @@
it "returns self if something is waiting for a broadcast" do
m = Mutex.new
cv = ConditionVariable.new
in_synchronize = false

th = Thread.new do
m.synchronize do
in_synchronize = true
cv.wait(m)
end
end

# ensures that th grabs m before current thread
# wait for m to acquire the mutex
Thread.pass until in_synchronize
# wait until th is sleeping (ie waiting)
Thread.pass while th.status and th.status != "sleep"

m.synchronize { cv.broadcast }.should == cv
6 changes: 5 additions & 1 deletion spec/ruby/library/conditionvariable/wait_spec.rb
Original file line number Diff line number Diff line change
@@ -5,14 +5,18 @@
it "returns self" do
m = Mutex.new
cv = ConditionVariable.new
in_synchronize = false

th = Thread.new do
m.synchronize do
in_synchronize = true
cv.wait(m).should == cv
end
end

# ensures that th grabs m before current thread
# wait for m to acquire the mutex
Thread.pass until in_synchronize
# wait until th is sleeping (ie waiting)
Thread.pass while th.status and th.status != "sleep"

m.synchronize { cv.signal }

0 comments on commit 8c884c0

Please sign in to comment.