Skip to content

Commit 34a683e

Browse files
committed
Add spec for monitor condition variables.
1 parent cd5758c commit 34a683e

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require_relative '../../spec_helper'
2+
require 'monitor'
3+
4+
describe "Monitor#new_cond" do
5+
it "creates a MonitorMixin::ConditoinVariable" do
6+
m = Monitor.new
7+
c = m.new_cond
8+
c.class.should == MonitorMixin::ConditionVariable
9+
end
10+
11+
it 'returns a condition variable which can be waited on by a thread holding the monitor' do
12+
m = Monitor.new
13+
c = m.new_cond
14+
15+
10.times do
16+
17+
locked = false
18+
thread = Thread.new do
19+
m.synchronize do
20+
locked = true
21+
c.wait
22+
end
23+
:done
24+
end
25+
26+
Thread.pass until locked
27+
Thread.pass until thread.stop?
28+
29+
m.synchronize { c.signal }
30+
31+
thread.join
32+
thread.value.should == :done
33+
end
34+
end
35+
36+
it 'returns a condition variable which can be waited on by a thread holding the monitor inside multiple synchronize blocks' do
37+
m = Monitor.new
38+
c = m.new_cond
39+
40+
10.times do
41+
42+
locked = false
43+
thread = Thread.new do
44+
m.synchronize do
45+
m.synchronize do
46+
locked = true
47+
c.wait
48+
end
49+
end
50+
:done
51+
end
52+
53+
Thread.pass until locked
54+
Thread.pass until thread.stop?
55+
56+
m.synchronize { c.signal }
57+
58+
thread.join
59+
thread.value.should == :done
60+
end
61+
end
62+
63+
it 'returns a condition variable which can be signalled by a thread holding the monitor inside multiple synchronize blocks' do
64+
m = Monitor.new
65+
c = m.new_cond
66+
67+
10.times do
68+
69+
locked = false
70+
thread = Thread.new do
71+
m.synchronize do
72+
locked = true
73+
c.wait
74+
end
75+
:done
76+
end
77+
78+
Thread.pass until locked
79+
Thread.pass until thread.stop?
80+
81+
m.synchronize { m.synchronize { c.signal } }
82+
83+
thread.join
84+
thread.value.should == :done
85+
end
86+
end
87+
88+
end

0 commit comments

Comments
 (0)