@@ -20,11 +20,19 @@ module Concurrent
20
20
end
21
21
22
22
let ( :fulfilled_subject ) do
23
- ScheduledTask . new ( 0.1 ) { fulfilled_value } . execute . tap { sleep ( 0.2 ) }
23
+ latch = Concurrent ::CountDownLatch . new ( 1 )
24
+ task = ScheduledTask . new ( 0.1 ) { latch . count_down ; fulfilled_value } . execute
25
+ latch . wait ( 1 )
26
+ sleep ( 0.1 )
27
+ task
24
28
end
25
29
26
30
let ( :rejected_subject ) do
27
- ScheduledTask . new ( 0.1 ) { raise rejected_reason } . execute . tap { sleep ( 0.2 ) }
31
+ latch = Concurrent ::CountDownLatch . new ( 1 )
32
+ task = ScheduledTask . new ( 0.1 ) { latch . count_down ; raise rejected_reason } . execute
33
+ latch . wait ( 1 )
34
+ sleep ( 0.1 )
35
+ task
28
36
end
29
37
30
38
it_should_behave_like :obligation
@@ -180,32 +188,34 @@ def trigger_observable(observable)
180
188
end
181
189
182
190
it 'returns false if the task is already in progress' do
183
- task = ScheduledTask . new ( 0.1 ) { sleep ( 1 ) ; 42 } . execute
184
- sleep ( 0.2 )
191
+ latch = Concurrent ::CountDownLatch . new ( 1 )
192
+ task = ScheduledTask . new ( 0.1 ) {
193
+ latch . count_down
194
+ sleep ( 1 )
195
+ } . execute
196
+ latch . wait ( 1 )
185
197
task . cancel . should be_false
186
198
end
187
199
188
200
it 'cancels the task if it has not yet scheduled' do
189
- @expected = true
190
- task = ScheduledTask . new ( 0.1 ) { @expected = false }
201
+ latch = Concurrent :: CountDownLatch . new ( 1 )
202
+ task = ScheduledTask . new ( 0.1 ) { latch . count_down }
191
203
task . cancel
192
204
task . execute
193
- sleep ( 0.5 )
194
- @expected . should be_true
205
+ latch . wait ( 0.3 ) . should be_false
195
206
end
196
207
197
208
198
209
it 'cancels the task if it has not yet started' do
199
- @expected = true
200
- task = ScheduledTask . new ( 0.3 ) { @expected = false } . execute
210
+ latch = Concurrent :: CountDownLatch . new ( 1 )
211
+ task = ScheduledTask . new ( 0.3 ) { latch . count_down } . execute
201
212
sleep ( 0.1 )
202
213
task . cancel
203
- sleep ( 0.5 )
204
- @expected . should be_true
214
+ latch . wait ( 0.5 ) . should be_false
205
215
end
206
216
207
217
it 'returns true on success' do
208
- task = ScheduledTask . new ( 0.3 ) { @expected = false } . execute
218
+ task = ScheduledTask . new ( 10 ) { nil } . execute
209
219
sleep ( 0.1 )
210
220
task . cancel . should be_true
211
221
end
@@ -221,8 +231,12 @@ def trigger_observable(observable)
221
231
context 'execution' do
222
232
223
233
it 'sets the state to :in_progress when the task is running' do
224
- task = ScheduledTask . new ( 0.1 ) { sleep ( 1 ) ; 42 } . execute
225
- sleep ( 0.2 )
234
+ latch = Concurrent ::CountDownLatch . new ( 1 )
235
+ task = ScheduledTask . new ( 0.1 ) {
236
+ latch . count_down
237
+ sleep ( 1 )
238
+ } . execute
239
+ latch . wait ( 1 )
226
240
task . should be_in_progress
227
241
end
228
242
end
0 commit comments