@@ -24,14 +24,14 @@ module Concurrent
24
24
expect ( executor ) . to receive ( :post ) . with ( no_args )
25
25
subject = TimerSet . new ( executor : executor )
26
26
subject . post ( 0 ) { nil }
27
- sleep ( 0.1 )
28
27
end
29
28
30
29
it 'uses the global io executor be default' do
30
+ latch = Concurrent ::CountDownLatch . new ( 1 )
31
31
expect ( Concurrent . global_io_executor ) . to receive ( :post ) . with ( no_args )
32
32
subject = TimerSet . new
33
- subject . post ( 0 ) { nil }
34
- sleep ( 0.1 )
33
+ subject . post ( 0 ) { latch . count_down }
34
+ latch . wait ( 0.1 )
35
35
end
36
36
end
37
37
@@ -85,7 +85,6 @@ module Concurrent
85
85
it 'executes a given task when given an interval in seconds, even if longer tasks have been scheduled' do
86
86
latch = CountDownLatch . new ( 1 )
87
87
subject . post ( 0.5 ) { nil }
88
- sleep 0.1
89
88
subject . post ( 0.1 ) { latch . count_down }
90
89
expect ( latch . wait ( 0.2 ) ) . to be_truthy
91
90
end
@@ -106,7 +105,7 @@ module Concurrent
106
105
start = Time . now . to_f
107
106
subject . post ( 0.2 ) { latch . count_down }
108
107
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
110
109
end
111
110
112
111
it 'executes all tasks scheduled for the same time' do
@@ -143,10 +142,9 @@ module Concurrent
143
142
end
144
143
145
144
it 'continues to execute new tasks even after the queue is emptied' do
146
- 10 . times do |i |
145
+ 3 . times do |i |
147
146
task = subject . post ( 0.1 ) { i }
148
147
expect ( task . value ) . to eq i
149
- sleep ( 0.1 )
150
148
end
151
149
end
152
150
end
@@ -233,7 +231,7 @@ module Concurrent
233
231
it 'returns false when not running' do
234
232
task = subject . post ( 10 ) { nil }
235
233
subject . shutdown
236
- subject . wait_for_termination ( 2 )
234
+ subject . wait_for_termination ( 1 )
237
235
expect ( task . cancel ) . to be false
238
236
end
239
237
end
@@ -313,7 +311,7 @@ module Concurrent
313
311
it 'returns false when not running' do
314
312
task = subject . post ( 10 ) { nil }
315
313
subject . shutdown
316
- subject . wait_for_termination ( 2 )
314
+ subject . wait_for_termination ( 1 )
317
315
expected = task . schedule_time
318
316
success = task . reschedule ( 10 )
319
317
expect ( success ) . to be false
@@ -334,65 +332,89 @@ module Concurrent
334
332
context 'termination' do
335
333
336
334
it 'cancels all pending tasks on #shutdown' do
335
+ count = 10
336
+ latch = Concurrent ::CountDownLatch . new ( count )
337
337
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 )
340
345
subject . shutdown
341
- sleep ( 0.2 )
346
+ subject . wait_for_termination ( 1 )
347
+
342
348
expect ( expected . value ) . to eq 0
343
349
end
344
350
345
351
it 'cancels all pending tasks on #kill' do
352
+ count = 10
353
+ latch = Concurrent ::CountDownLatch . new ( count )
346
354
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 )
349
362
subject . kill
350
- sleep ( 0.2 )
363
+ subject . wait_for_termination ( 1 )
364
+
351
365
expect ( expected . value ) . to eq 0
352
366
end
353
367
354
368
it 'stops the monitor thread on #shutdown' do
355
369
timer_executor = subject . instance_variable_get ( :@timer_executor )
356
370
subject . shutdown
357
- sleep ( 0. 1)
371
+ subject . wait_for_termination ( 1 )
358
372
expect ( timer_executor ) . not_to be_running
359
373
end
360
374
361
375
it 'kills the monitor thread on #kill' do
362
376
timer_executor = subject . instance_variable_get ( :@timer_executor )
363
377
subject . kill
364
- sleep ( 0. 1)
378
+ subject . wait_for_termination ( 1 )
365
379
expect ( timer_executor ) . not_to be_running
366
380
end
367
381
368
382
it 'rejects tasks once shutdown' do
383
+ latch = Concurrent ::CountDownLatch . new ( 1 )
369
384
expected = AtomicFixnum . new ( 0 )
385
+
370
386
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 )
374
391
expect ( expected . value ) . to eq 0
375
392
end
376
393
377
394
it 'rejects tasks once killed' do
395
+ latch = Concurrent ::CountDownLatch . new ( 1 )
378
396
expected = AtomicFixnum . new ( 0 )
397
+
379
398
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 )
383
403
expect ( expected . value ) . to eq 0
384
404
end
385
405
386
406
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 )
389
410
subject . shutdown
390
411
expect ( subject . wait_for_termination ( 0.1 ) ) . to be_truthy
391
412
end
392
413
393
414
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 )
396
418
# do not call shutdown -- force timeout
397
419
expect ( subject . wait_for_termination ( 0.1 ) ) . to be_falsey
398
420
end
@@ -413,14 +435,14 @@ module Concurrent
413
435
414
436
it 'is shutdown? after shutdown completes' do
415
437
subject . shutdown
416
- sleep ( 0. 1)
438
+ subject . wait_for_termination ( 1 )
417
439
expect ( subject ) . not_to be_running
418
440
expect ( subject ) . to be_shutdown
419
441
end
420
442
421
443
it 'is shutdown? after being killed' do
422
444
subject . kill
423
- sleep ( 0. 1)
445
+ subject . wait_for_termination ( 1 )
424
446
expect ( subject ) . not_to be_running
425
447
expect ( subject ) . to be_shutdown
426
448
end
0 commit comments