@@ -24,14 +24,14 @@ module Concurrent
2424 expect ( executor ) . to receive ( :post ) . with ( no_args )
2525 subject = TimerSet . new ( executor : executor )
2626 subject . post ( 0 ) { nil }
27- sleep ( 0.1 )
2827 end
2928
3029 it 'uses the global io executor be default' do
30+ latch = Concurrent ::CountDownLatch . new ( 1 )
3131 expect ( Concurrent . global_io_executor ) . to receive ( :post ) . with ( no_args )
3232 subject = TimerSet . new
33- subject . post ( 0 ) { nil }
34- sleep ( 0.1 )
33+ subject . post ( 0 ) { latch . count_down }
34+ latch . wait ( 0.1 )
3535 end
3636 end
3737
@@ -85,7 +85,6 @@ module Concurrent
8585 it 'executes a given task when given an interval in seconds, even if longer tasks have been scheduled' do
8686 latch = CountDownLatch . new ( 1 )
8787 subject . post ( 0.5 ) { nil }
88- sleep 0.1
8988 subject . post ( 0.1 ) { latch . count_down }
9089 expect ( latch . wait ( 0.2 ) ) . to be_truthy
9190 end
@@ -106,7 +105,7 @@ module Concurrent
106105 start = Time . now . to_f
107106 subject . post ( 0.2 ) { latch . count_down }
108107 expect ( latch . wait ( 1 ) ) . to be true
109- expect ( Time . now . to_f - start ) . to be >= 0.2
108+ expect ( Time . now . to_f - start ) . to be >= 0.19
110109 end
111110
112111 it 'executes all tasks scheduled for the same time' do
@@ -143,10 +142,9 @@ module Concurrent
143142 end
144143
145144 it 'continues to execute new tasks even after the queue is emptied' do
146- 10 . times do |i |
145+ 3 . times do |i |
147146 task = subject . post ( 0.1 ) { i }
148147 expect ( task . value ) . to eq i
149- sleep ( 0.1 )
150148 end
151149 end
152150 end
@@ -233,7 +231,7 @@ module Concurrent
233231 it 'returns false when not running' do
234232 task = subject . post ( 10 ) { nil }
235233 subject . shutdown
236- subject . wait_for_termination ( 2 )
234+ subject . wait_for_termination ( 1 )
237235 expect ( task . cancel ) . to be false
238236 end
239237 end
@@ -313,7 +311,7 @@ module Concurrent
313311 it 'returns false when not running' do
314312 task = subject . post ( 10 ) { nil }
315313 subject . shutdown
316- subject . wait_for_termination ( 2 )
314+ subject . wait_for_termination ( 1 )
317315 expected = task . schedule_time
318316 success = task . reschedule ( 10 )
319317 expect ( success ) . to be false
@@ -334,65 +332,89 @@ module Concurrent
334332 context 'termination' do
335333
336334 it 'cancels all pending tasks on #shutdown' do
335+ count = 10
336+ latch = Concurrent ::CountDownLatch . new ( count )
337337 expected = AtomicFixnum . new ( 0 )
338- 10 . times { subject . post ( 0.2 ) { expected . increment } }
339- sleep ( 0.1 )
338+
339+ count . times do |i |
340+ subject . post ( 0.2 ) { expected . increment }
341+ latch . count_down
342+ end
343+
344+ latch . wait ( 1 )
340345 subject . shutdown
341- sleep ( 0.2 )
346+ subject . wait_for_termination ( 1 )
347+
342348 expect ( expected . value ) . to eq 0
343349 end
344350
345351 it 'cancels all pending tasks on #kill' do
352+ count = 10
353+ latch = Concurrent ::CountDownLatch . new ( count )
346354 expected = AtomicFixnum . new ( 0 )
347- 10 . times { subject . post ( 0.2 ) { expected . increment } }
348- sleep ( 0.1 )
355+
356+ count . times do |i |
357+ subject . post ( 0.2 ) { expected . increment }
358+ latch . count_down
359+ end
360+
361+ latch . wait ( 1 )
349362 subject . kill
350- sleep ( 0.2 )
363+ subject . wait_for_termination ( 1 )
364+
351365 expect ( expected . value ) . to eq 0
352366 end
353367
354368 it 'stops the monitor thread on #shutdown' do
355369 timer_executor = subject . instance_variable_get ( :@timer_executor )
356370 subject . shutdown
357- sleep ( 0. 1)
371+ subject . wait_for_termination ( 1 )
358372 expect ( timer_executor ) . not_to be_running
359373 end
360374
361375 it 'kills the monitor thread on #kill' do
362376 timer_executor = subject . instance_variable_get ( :@timer_executor )
363377 subject . kill
364- sleep ( 0. 1)
378+ subject . wait_for_termination ( 1 )
365379 expect ( timer_executor ) . not_to be_running
366380 end
367381
368382 it 'rejects tasks once shutdown' do
383+ latch = Concurrent ::CountDownLatch . new ( 1 )
369384 expected = AtomicFixnum . new ( 0 )
385+
370386 subject . shutdown
371- sleep ( 0.1 )
372- expect ( subject . post ( 0 ) { expected . increment } ) . to be_falsey
373- sleep ( 0.1 )
387+ subject . wait_for_termination ( 1 )
388+
389+ expect ( subject . post ( 0 ) { expected . increment ; latch . count_down } ) . to be_falsey
390+ latch . wait ( 0.1 )
374391 expect ( expected . value ) . to eq 0
375392 end
376393
377394 it 'rejects tasks once killed' do
395+ latch = Concurrent ::CountDownLatch . new ( 1 )
378396 expected = AtomicFixnum . new ( 0 )
397+
379398 subject . kill
380- sleep ( 0.1 )
381- expect ( subject . post ( 0 ) { expected . increment } ) . to be_falsey
382- sleep ( 0.1 )
399+ subject . wait_for_termination ( 1 )
400+
401+ expect ( subject . post ( 0 ) { expected . increment ; latch . count_down } ) . to be_falsey
402+ latch . wait ( 0.1 )
383403 expect ( expected . value ) . to eq 0
384404 end
385405
386406 specify '#wait_for_termination returns true if shutdown completes before timeout' do
387- subject . post ( 0.1 ) { nil }
388- sleep ( 0.1 )
407+ latch = Concurrent ::CountDownLatch . new ( 1 )
408+ subject . post ( 0 ) { latch . count_down }
409+ latch . wait ( 1 )
389410 subject . shutdown
390411 expect ( subject . wait_for_termination ( 0.1 ) ) . to be_truthy
391412 end
392413
393414 specify '#wait_for_termination returns false on timeout' do
394- subject . post ( 0.1 ) { nil }
395- sleep ( 0.1 )
415+ latch = Concurrent ::CountDownLatch . new ( 1 )
416+ subject . post ( 0 ) { latch . count_down }
417+ latch . wait ( 0.1 )
396418 # do not call shutdown -- force timeout
397419 expect ( subject . wait_for_termination ( 0.1 ) ) . to be_falsey
398420 end
@@ -413,14 +435,14 @@ module Concurrent
413435
414436 it 'is shutdown? after shutdown completes' do
415437 subject . shutdown
416- sleep ( 0. 1)
438+ subject . wait_for_termination ( 1 )
417439 expect ( subject ) . not_to be_running
418440 expect ( subject ) . to be_shutdown
419441 end
420442
421443 it 'is shutdown? after being killed' do
422444 subject . kill
423- sleep ( 0. 1)
445+ subject . wait_for_termination ( 1 )
424446 expect ( subject ) . not_to be_running
425447 expect ( subject ) . to be_shutdown
426448 end
0 commit comments