Skip to content

Commit e207fa6

Browse files
committed
Merge pull request #88 from jdantonio/refactor/brittle-specs
Refactor brittle specs
2 parents d1a5954 + 5b8e99b commit e207fa6

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

spec/concurrent/exchanger_spec.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,15 @@ module Concurrent
5050
context 'with timeout' do
5151
it 'should block until timeout' do
5252

53-
latch = Concurrent::CountDownLatch.new(1)
5453
value = 0
5554
start = Time.now.to_f
5655

57-
t = Thread.new do
58-
value = exchanger.exchange(2, 0.2)
59-
latch.count_down
56+
future = Concurrent::Future.execute do
57+
exchanger.exchange(2, 0.2)
6058
end
61-
62-
latch.wait(1)
63-
59+
60+
future.value.should be_nil
6461
(Time.now.to_f - start).should >= 0.2
65-
t.status.should be_false
66-
value.should be_nil
6762
end
6863
end
6964
end

spec/concurrent/observable_shared.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def notify(*args)
135135
subject.add_observer(obs, observer_func)
136136
end
137137
trigger_observable(subject)
138+
latch.wait(1)
138139
latch.count.should eq 0
139140
end
140141

@@ -144,6 +145,7 @@ def notify(*args)
144145
subject.add_observer{ latch.count_down }
145146
end
146147
trigger_observable(subject)
148+
latch.wait(1)
147149
latch.count.should eq 0
148150
end
149151

@@ -155,6 +157,7 @@ def notify(*args)
155157
subject.delete_observer(obs)
156158

157159
trigger_observable(subject)
160+
latch.wait(1)
158161
latch.count.should eq 5
159162
end
160163

@@ -167,6 +170,7 @@ def notify(*args)
167170
subject.delete_observers
168171

169172
trigger_observable(subject)
173+
latch.wait(1)
170174
latch.count.should eq 5
171175
end
172176
end

spec/concurrent/supervisor_spec.rb

Lines changed: 25 additions & 11 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,15 +439,16 @@ 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
433446

434447
it 'returns the count of all workers terminated by exception as #abend' do
435448
busy_supervisor.count.abend.should eq 0
436449
busy_supervisor.run!
437-
sleep(0.5)
450+
stoppers.each{|stopper| stopper.latch.wait(1) }
451+
sleep(0.1)
438452

439453
busy_supervisor.count.abend.should eq abend_count
440454
end
@@ -832,8 +846,8 @@ def mock_thread(status = 'run')
832846
monitor_interval: 0.1)
833847
supervisor.add_worker(error_class.new)
834848
supervisor.should_receive(:exceeded_max_restart_frequency?).once.and_return(true)
835-
supervisor.run!
836-
sleep(0.2)
849+
future = Concurrent::Future.execute{ supervisor.run }
850+
future.value(1)
837851
supervisor.should_not be_running
838852
end
839853

@@ -842,8 +856,8 @@ def mock_thread(status = 'run')
842856
monitor_interval: 0.1)
843857
supervisor.add_worker(error_class.new)
844858
supervisor.should_receive(:exceeded_max_restart_frequency?).once.and_return(true)
845-
supervisor.run!
846-
sleep(0.2)
859+
future = Concurrent::Future.execute{ supervisor.run }
860+
future.value(1)
847861
supervisor.should_not be_running
848862
end
849863

@@ -852,8 +866,8 @@ def mock_thread(status = 'run')
852866
monitor_interval: 0.1)
853867
supervisor.add_worker(error_class.new)
854868
supervisor.should_receive(:exceeded_max_restart_frequency?).once.and_return(true)
855-
supervisor.run!
856-
sleep(0.2)
869+
future = Concurrent::Future.execute{ supervisor.run }
870+
future.value(1)
857871
supervisor.should_not be_running
858872
end
859873
end

spec/concurrent/timer_task_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,23 @@ def trigger_observable(observable)
171171
it 'runs the block immediately when the :run_now option is true' do
172172
latch = CountDownLatch.new(1)
173173
subject = TimerTask.execute(execution: 500, now: true){ latch.count_down }
174-
latch.wait(0.1).should be_true
174+
latch.wait(1).should be_true
175175
subject.kill
176176
end
177177

178178
it 'waits for :execution_interval seconds when the :run_now option is false' do
179179
latch = CountDownLatch.new(1)
180180
subject = TimerTask.execute(execution: 0.1, now: false){ latch.count_down }
181181
latch.count.should eq 1
182-
latch.wait(0.2).should be_true
182+
latch.wait(1).should be_true
183183
subject.kill
184184
end
185185

186186
it 'waits for :execution_interval seconds when the :run_now option is not given' do
187187
latch = CountDownLatch.new(1)
188188
subject = TimerTask.execute(execution: 0.1, now: false){ latch.count_down }
189189
latch.count.should eq 1
190-
latch.wait(0.2).should be_true
190+
latch.wait(1).should be_true
191191
subject.kill
192192
end
193193

@@ -199,7 +199,7 @@ def trigger_observable(observable)
199199
latch.sount_down
200200
end
201201
subject.execute
202-
latch.wait(0.2)
202+
latch.wait(1)
203203
expected.should eq subject
204204
subject.kill
205205
end

0 commit comments

Comments
 (0)