Skip to content

Commit b041082

Browse files
committed
Updated brittle Supervisor #count#stopped spec.
1 parent 6a470cf commit b041082

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

spec/concurrent/supervisor_spec.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ def run() super(); sleep; end
2323

2424
let(:stopper_class) do
2525
Class.new(worker_class) {
26-
def initialize(sleep_time = 0.2) @sleep_time = sleep_time; end;
27-
def run() super(); sleep(@sleep_time); end
26+
attr_reader :latch
27+
def initialize(sleep_time = 0.2)
28+
@sleep_time = sleep_time
29+
@latch = Concurrent::CountDownLatch.new(1)
30+
end
31+
def run
32+
super
33+
sleep(@sleep_time)
34+
@latch.count_down
35+
end
2836
}
2937
end
3038

@@ -326,13 +334,18 @@ def mock_thread(status = 'run')
326334

327335
context '#count' do
328336

337+
let(:stoppers){ Array.new }
338+
329339
let(:busy_supervisor) do
330340
supervisor = Supervisor.new(monitor_interval: 60)
331341
3.times do
332342
supervisor.add_worker(sleeper_class.new)
333-
supervisor.add_worker(stopper_class.new)
334343
supervisor.add_worker(error_class.new)
335344
supervisor.add_worker(runner_class.new)
345+
346+
stopper = stopper_class.new
347+
stoppers << stopper
348+
supervisor.add_worker(stopper)
336349
end
337350
supervisor
338351
end
@@ -426,7 +439,7 @@ def mock_thread(status = 'run')
426439
it 'returns the count of all stopped workers as #stopped' do
427440
busy_supervisor.count.stopped.should eq total_count
428441
busy_supervisor.run!
429-
sleep(0.5)
442+
stoppers.each{|stopper| stopper.latch.wait(1) }
430443

431444
busy_supervisor.count.stopped.should eq stopped_count
432445
end

0 commit comments

Comments
 (0)