@@ -28,12 +28,10 @@ module Concurrent
2828 context '#set' do
2929
3030 it 'triggers the event' do
31- @expected = false
32- Thread . new { subject . wait ; @expected = true }
33- sleep ( 0.1 )
31+ latch = CountDownLatch . new ( 1 )
32+ Thread . new { subject . wait . tap { latch . count_down } }
3433 subject . set
35- sleep ( 0.1 )
36- @expected . should be_true
34+ latch . wait ( 1 ) . should be_true
3735 end
3836
3937 it 'sets the state to set' do
@@ -67,23 +65,19 @@ module Concurrent
6765 end
6866
6967 it 'does not trigger an unset event' do
70- @expected = false
71- Thread . new { subject . wait ; @expected = true }
72- sleep ( 0.1 )
68+ latch = CountDownLatch . new ( 1 )
69+ Thread . new { subject . wait . tap { latch . count_down } }
7370 subject . reset
74- sleep ( 0.1 )
75- @expected . should be_false
71+ latch . wait ( 0.1 ) . should be_false
7672 end
7773
7874 it 'does not interrupt waiting threads when event is unset' do
79- @expected = false
80- Thread . new { subject . wait ; @expected = true }
81- sleep ( 0.1 )
75+ latch = CountDownLatch . new ( 1 )
76+ Thread . new { subject . wait . tap { latch . count_down } }
8277 subject . reset
83- sleep ( 0.1 )
78+ latch . wait ( 0.1 ) . should be_false
8479 subject . set
85- sleep ( 0.1 )
86- @expected . should be_true
80+ latch . wait ( 0.1 ) . should be_true
8781 end
8882
8983 it 'returns true when called on an unset event' do
@@ -104,15 +98,41 @@ module Concurrent
10498 end
10599 end
106100
101+ #context '#pulse' do
102+
103+ #it 'triggers an unset event' do
104+ #subject.reset
105+ #latch = CountDownLatch.new(1)
106+ #Thread.new{ subject.wait.tap{ puts "Boom!"; latch.count_down } }
107+ #subject.pulse
108+ #latch.wait(0.1).should be_true
109+ #end
110+
111+ #it 'does nothing with a set event' do
112+ #subject.set
113+ #latch = CountDownLatch.new(1)
114+ #Thread.new{ subject.wait.tap{ latch.count_down } }
115+ #subject.pulse
116+ #latch.wait(0.1).should be_true
117+ #end
118+
119+ #it 'leaves the event in the unset state' do
120+ #latch = CountDownLatch.new(1)
121+ #Thread.new{ subject.wait.tap{ latch.count_down } }
122+ #subject.pulse
123+ #latch.wait(0.1)
124+ #subject.should_not be_set
125+ #end
126+ #end
127+
107128 context '#wait' do
108129
109130 it 'returns immediately when the event has been set' do
110131 subject . reset
111- @expected = false
132+ latch = CountDownLatch . new ( 1 )
112133 subject . set
113- Thread . new { subject . wait ( 1000 ) ; @expected = true }
114- sleep ( 1 )
115- @expected . should be_true
134+ Thread . new { subject . wait ( 1000 ) . tap { latch . count_down } }
135+ latch . wait ( 0.1 ) . should be_true
116136 end
117137
118138 it 'returns true once the event is set' do
@@ -122,19 +142,19 @@ module Concurrent
122142
123143 it 'blocks indefinitely when the timer is nil' do
124144 subject . reset
125- @expected = false
126- Thread . new { subject . wait ; @expected = true }
145+ latch = CountDownLatch . new ( 1 )
146+ Thread . new { subject . wait . tap { latch . count_down } }
147+ latch . wait ( 0.1 ) . should be_false
127148 subject . set
128- sleep ( 1 )
129- @expected . should be_true
149+ latch . wait ( 0.1 ) . should be_true
130150 end
131151
132152 it 'stops waiting when the timer expires' do
133153 subject . reset
134- @expected = false
135- Thread . new { subject . wait ( 0.5 ) ; @expected = true }
136- sleep ( 1 )
137- @expected . should be_true
154+ latch = CountDownLatch . new ( 1 )
155+ Thread . new { subject . wait ( 0.2 ) . tap { latch . count_down } }
156+ latch . wait ( 0.1 ) . should be_false
157+ latch . wait . should be_true
138158 end
139159
140160 it 'returns false when the timer expires' do
@@ -172,28 +192,24 @@ def subject.simulate_spurious_wake_up
172192 end
173193
174194 it 'should resist to spurious wake ups without timeout' do
175- @expected = false
176- Thread . new { subject . wait ; @expected = true }
195+ latch = CountDownLatch . new ( 1 )
196+ Thread . new { subject . wait . tap { latch . count_down } }
177197
178198 sleep ( 0.1 )
179199 subject . simulate_spurious_wake_up
180200
181- sleep ( 0.1 )
182- @expected . should be_false
201+ latch . wait ( 0.1 ) . should be_false
183202 end
184203
185204 it 'should resist to spurious wake ups with timeout' do
186- @expected = false
187- Thread . new { subject . wait ( 0.5 ) ; @expected = true }
205+ latch = CountDownLatch . new ( 1 )
206+ Thread . new { subject . wait ( 0.3 ) . tap { latch . count_down } }
188207
189208 sleep ( 0.1 )
190209 subject . simulate_spurious_wake_up
191210
192- sleep ( 0.1 )
193- @expected . should be_false
194-
195- sleep ( 0.4 )
196- @expected . should be_true
211+ latch . wait ( 0.1 ) . should be_false
212+ latch . wait ( 1 ) . should be_true
197213 end
198214 end
199215 end
0 commit comments