Skip to content

Commit b5074e9

Browse files
committed
Refactored brittle ScheduledTask specs.
1 parent 3c63435 commit b5074e9

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

spec/concurrent/scheduled_task_spec.rb

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ module Concurrent
2020
end
2121

2222
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
2428
end
2529

2630
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
2836
end
2937

3038
it_should_behave_like :obligation
@@ -180,32 +188,34 @@ def trigger_observable(observable)
180188
end
181189

182190
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)
185197
task.cancel.should be_false
186198
end
187199

188200
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 }
191203
task.cancel
192204
task.execute
193-
sleep(0.5)
194-
@expected.should be_true
205+
latch.wait(0.3).should be_false
195206
end
196207

197208

198209
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
201212
sleep(0.1)
202213
task.cancel
203-
sleep(0.5)
204-
@expected.should be_true
214+
latch.wait(0.5).should be_false
205215
end
206216

207217
it 'returns true on success' do
208-
task = ScheduledTask.new(0.3){ @expected = false }.execute
218+
task = ScheduledTask.new(10){ nil }.execute
209219
sleep(0.1)
210220
task.cancel.should be_true
211221
end
@@ -221,8 +231,12 @@ def trigger_observable(observable)
221231
context 'execution' do
222232

223233
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)
226240
task.should be_in_progress
227241
end
228242
end

0 commit comments

Comments
 (0)