Skip to content

Commit cc72cd9

Browse files
committed
Reliably testing concurrency abstractions will be the death of me.
1 parent ae66d27 commit cc72cd9

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

spec/concurrent/executor/thread_pool_executor_shared.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@
465465

466466
context ':caller_runs' do
467467

468-
subject do
468+
let(:executor) do
469469
described_class.new(
470470
min_threads: 1,
471471
max_threads: 1,
@@ -475,14 +475,20 @@
475475
)
476476
end
477477

478+
after(:each) do
479+
# need to replicate this w/i scope otherwise rspec may complain
480+
executor.kill
481+
executor.wait_for_termination(0.1)
482+
end
483+
478484
specify '#post does not create any new threads when the queue is at capacity' do
479485
trigger = Concurrent::Event.new
480486
initial = Thread.list.length
481487

482488
# Post several tasks to the executor. Has to be a new thread,
483489
# because it will start blocking once the queue fills up.
484490
Thread.new do
485-
5.times{ subject.post{ trigger.wait } }
491+
5.times{ executor.post{ trigger.wait } }
486492
end
487493

488494
expect(Thread.list.length).to be < initial + 1 + 5
@@ -494,32 +500,32 @@
494500
specify '#<< executes the task on the current thread when the queue is at capacity' do
495501
trigger = Concurrent::Event.new
496502
latch = Concurrent::CountDownLatch.new(5)
497-
subject.post{ trigger.wait }
498-
5.times{|i| subject << proc { latch.count_down } }
503+
executor.post{ trigger.wait }
504+
5.times{|i| executor << proc { latch.count_down } }
499505
latch.wait(0.1)
500506
trigger.set
501507
end
502508

503509
specify '#post executes the task on the current thread when the queue is at capacity' do
504510
trigger = Concurrent::Event.new
505511
latch = Concurrent::CountDownLatch.new(5)
506-
subject.post{ trigger.wait }
507-
5.times{|i| subject.post{ latch.count_down } }
512+
executor.post{ trigger.wait }
513+
5.times{|i| executor.post{ latch.count_down } }
508514
latch.wait(0.1)
509515
trigger.set
510516
end
511517

512518
specify '#post executes the task on the current thread when the executor is shutting down' do
513519
latch = Concurrent::CountDownLatch.new(1)
514-
subject.shutdown
515-
subject.post{ latch.count_down }
520+
executor.shutdown
521+
executor.post{ latch.count_down }
516522
latch.wait(0.1)
517523
end
518524

519525
specify '#<< executes the task on the current thread when the executor is shutting down' do
520526
latch = Concurrent::CountDownLatch.new(1)
521-
subject.shutdown
522-
subject << proc { latch.count_down }
527+
executor.shutdown
528+
executor << proc { latch.count_down }
523529
latch.wait(0.1)
524530
end
525531
end

0 commit comments

Comments
 (0)