Skip to content

Commit 63e6eea

Browse files
committed
Replaced sleep statements in many tests.
1 parent cdb4011 commit 63e6eea

10 files changed

+63
-40
lines changed

spec/concurrent/atomic/event_spec.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ module Concurrent
2727

2828
it 'triggers the event' do
2929
latch = CountDownLatch.new(1)
30-
Thread.new{ subject.wait.tap{ latch.count_down } }
31-
sleep(0.1)
30+
t = Thread.new{ subject.wait.tap{ latch.count_down } }
31+
t.join(0.1)
3232
subject.set
3333
expect(latch.wait(1)).to be true
3434
end
@@ -156,21 +156,19 @@ def subject.simulate_spurious_wake_up
156156

157157
it 'should resist to spurious wake ups without timeout' do
158158
latch = CountDownLatch.new(1)
159-
Thread.new{ subject.wait.tap{ latch.count_down } }
159+
t = Thread.new{ subject.wait.tap{ latch.count_down } }
160+
t.join(0.1)
160161

161-
sleep(0.1)
162162
subject.simulate_spurious_wake_up
163-
164163
expect(latch.wait(0.1)).to be false
165164
end
166165

167166
it 'should resist to spurious wake ups with timeout' do
168167
latch = CountDownLatch.new(1)
169-
Thread.new{ subject.wait(0.3); latch.count_down }
168+
t = Thread.new{ subject.wait(0.3); latch.count_down }
169+
t.join(0.1)
170170

171-
sleep(0.1)
172171
subject.simulate_spurious_wake_up
173-
174172
expect(latch.wait(0.1)).to be false
175173
expect(latch.wait(1)).to be true
176174
end

spec/concurrent/dataflow_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,11 @@ def fib_with_dot(n)
223223
else
224224
n1 = fib_with_dot(n - 1)
225225
n2 = fib_with_dot(n - 2)
226-
Concurrent.dataflow_with(root_executor, n1, n2) { n1.value + n2.value }
226+
Concurrent.dataflow_with(ImmediateExecutor.new, n1, n2) { n1.value + n2.value }
227227
end
228228
end
229229

230230
expected = fib_with_dot(7)
231-
sleep(0.1)
232231
expect(expected.value).to eq 13
233232
end
234233

spec/concurrent/executor/cached_thread_pool_spec.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ module Concurrent
44

55
describe CachedThreadPool do
66

7+
let(:latch) { Concurrent::CountDownLatch.new }
8+
79
subject do
810
described_class.new(fallback_policy: :discard)
911
end
1012

1113
after(:each) do
1214
subject.kill
13-
sleep(0.1)
15+
subject.wait_for_termination(1)
1416
end
1517

1618
it_should_behave_like :thread_pool
@@ -42,13 +44,15 @@ module Concurrent
4244

4345
it 'returns zero while running' do
4446
10.times{ subject.post{ nil } }
45-
sleep(0.1)
47+
subject.post { latch.count_down }
48+
latch.wait(0.1)
4649
expect(subject.min_length).to eq 0
4750
end
4851

4952
it 'returns zero once shutdown' do
5053
10.times{ subject.post{ nil } }
51-
sleep(0.1)
54+
subject.post { latch.count_down }
55+
latch.wait(0.1)
5256
subject.shutdown
5357
subject.wait_for_termination(1)
5458
expect(subject.min_length).to eq 0
@@ -63,13 +67,15 @@ module Concurrent
6367

6468
it 'returns :max_length while running' do
6569
10.times{ subject.post{ nil } }
66-
sleep(0.1)
70+
subject.post { latch.count_down }
71+
latch.wait(0.1)
6772
expect(subject.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
6873
end
6974

7075
it 'returns :max_length once shutdown' do
7176
10.times{ subject.post{ nil } }
72-
sleep(0.1)
77+
subject.post { latch.count_down }
78+
latch.wait(0.1)
7379
subject.shutdown
7480
subject.wait_for_termination(1)
7581
expect(subject.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
@@ -84,13 +90,15 @@ module Concurrent
8490

8591
it 'returns a non-zero number once tasks have been received' do
8692
10.times{ subject.post{ sleep(0.1) } }
87-
sleep(0.1)
93+
subject.post { latch.count_down }
94+
latch.wait(0.1)
8895
expect(subject.largest_length).to be > 0
8996
end
9097

9198
it 'returns a non-zero number after shutdown if tasks have been received' do
9299
10.times{ subject.post{ sleep(0.1) } }
93-
sleep(0.1)
100+
subject.post { latch.count_down }
101+
latch.wait(0.1)
94102
subject.shutdown
95103
subject.wait_for_termination(1)
96104
expect(subject.largest_length).to be > 0

spec/concurrent/executor/executor_service_shared.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
after(:each) do
66
subject.kill
7-
sleep(0.1)
7+
subject.wait_for_termination(0.1)
88
end
99

1010
it_should_behave_like :global_thread_pool

spec/concurrent/executor/fixed_thread_pool_spec.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ module Concurrent
44

55
describe FixedThreadPool do
66

7+
let(:latch) { Concurrent::CountDownLatch.new }
8+
79
let!(:num_threads){ 5 }
810
subject { described_class.new(num_threads) }
911

1012
after(:each) do
1113
subject.kill
12-
sleep(0.1)
14+
subject.wait_for_termination(0.1)
1315
end
1416

1517
it_should_behave_like :thread_pool
@@ -82,13 +84,15 @@ module Concurrent
8284

8385
it 'returns :num_threads while running' do
8486
10.times{ subject.post{ nil } }
85-
sleep(0.1)
87+
subject.post { latch.count_down }
88+
latch.wait(0.1)
8689
expect(subject.min_length).to eq num_threads
8790
end
8891

8992
it 'returns :num_threads once shutdown' do
9093
10.times{ subject.post{ nil } }
91-
sleep(0.1)
94+
subject.post { latch.count_down }
95+
latch.wait(0.1)
9296
subject.shutdown
9397
subject.wait_for_termination(1)
9498
expect(subject.min_length).to eq num_threads
@@ -103,13 +107,15 @@ module Concurrent
103107

104108
it 'returns :num_threads while running' do
105109
10.times{ subject.post{ nil } }
106-
sleep(0.1)
110+
subject.post { latch.count_down }
111+
latch.wait(0.1)
107112
expect(subject.max_length).to eq num_threads
108113
end
109114

110115
it 'returns :num_threads once shutdown' do
111116
10.times{ subject.post{ nil } }
112-
sleep(0.1)
117+
subject.post { latch.count_down }
118+
latch.wait(0.1)
113119
subject.shutdown
114120
subject.wait_for_termination(1)
115121
expect(subject.max_length).to eq num_threads
@@ -120,7 +126,8 @@ module Concurrent
120126

121127
it 'returns :num_threads while running' do
122128
10.times{ subject.post{ nil } }
123-
sleep(0.1)
129+
subject.post { latch.count_down }
130+
latch.wait(0.1)
124131
expect(subject.length).to eq num_threads
125132
end
126133
end
@@ -133,13 +140,15 @@ module Concurrent
133140

134141
it 'returns :num_threads while running' do
135142
10.times{ subject.post{ nil } }
136-
sleep(0.1)
143+
subject.post { latch.count_down }
144+
latch.wait(0.1)
137145
expect(subject.largest_length).to eq num_threads
138146
end
139147

140148
it 'returns :num_threads once shutdown' do
141149
10.times{ subject.post{ nil } }
142-
sleep(0.1)
150+
subject.post { latch.count_down }
151+
latch.wait(0.1)
143152
subject.shutdown
144153
subject.wait_for_termination(1)
145154
expect(subject.largest_length).to eq num_threads

spec/concurrent/executor/java_single_thread_executor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Concurrent
88

99
after(:each) do
1010
subject.kill
11-
sleep(0.1)
11+
subject.wait_for_termination(0.1)
1212
end
1313

1414
subject { JavaSingleThreadExecutor.new }

spec/concurrent/executor/java_thread_pool_executor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Concurrent
88

99
after(:each) do
1010
subject.kill
11-
sleep(0.1)
11+
subject.wait_for_termination(0.1)
1212
end
1313

1414
subject do

spec/concurrent/executor/ruby_single_thread_executor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Concurrent
66

77
after(:each) do
88
subject.kill
9-
sleep(0.1)
9+
subject.wait_for_termination(0.1)
1010
end
1111

1212
subject { RubySingleThreadExecutor.new }

spec/concurrent/executor/ruby_thread_pool_executor_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ module Concurrent
44

55
describe RubyThreadPoolExecutor, :type=>:mrirbx do
66

7+
let(:latch) { Concurrent::CountDownLatch.new }
8+
79
after(:each) do
810
subject.kill
9-
sleep(0.1)
11+
subject.wait_for_termination(0.1)
1012
end
1113

1214
subject do
@@ -39,13 +41,15 @@ module Concurrent
3941

4042
it 'returns :max_length when no tasks are enqueued' do
4143
5.times{ subject.post{ nil } }
42-
sleep(0.1)
44+
subject.post { latch.count_down }
45+
latch.wait(0.1)
4346
expect(subject.remaining_capacity).to eq expected_max
4447
end
4548

4649
it 'returns the remaining capacity when tasks are enqueued' do
4750
100.times{ subject.post{ sleep(0.5) } }
48-
sleep(0.1)
51+
subject.post { latch.count_down }
52+
latch.wait(0.1)
4953
expect(subject.remaining_capacity).to be < expected_max
5054
end
5155
end

spec/concurrent/executor/thread_pool_shared.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
shared_examples :thread_pool do
44

5+
let(:latch) { Concurrent::CountDownLatch.new }
6+
57
after(:each) do
68
subject.kill
7-
sleep(0.1)
9+
subject.wait_for_termination(0.1)
810
end
911

1012
it_should_behave_like :executor_service
@@ -42,7 +44,8 @@
4244

4345
it 'returns zero once shut down' do
4446
5.times{ subject.post{ sleep(0.1) } }
45-
sleep(0.1)
47+
subject.post { latch.count_down }
48+
latch.wait(0.1)
4649
subject.shutdown
4750
subject.wait_for_termination(1)
4851
expect(subject.length).to eq 0
@@ -57,13 +60,15 @@
5760

5861
it 'returns the approximate number of tasks that have been post thus far' do
5962
10.times{ subject.post{ nil } }
60-
sleep(0.1)
63+
subject.post { latch.count_down }
64+
latch.wait(0.1)
6165
expect(subject.scheduled_task_count).to be > 0
6266
end
6367

6468
it 'returns the approximate number of tasks that were post' do
6569
10.times{ subject.post{ nil } }
66-
sleep(0.1)
70+
subject.post { latch.count_down }
71+
latch.wait(0.1)
6772
subject.shutdown
6873
subject.wait_for_termination(1)
6974
expect(subject.scheduled_task_count).to be > 0
@@ -79,17 +84,17 @@
7984
it 'returns the approximate number of tasks that have been completed thus far' do
8085
5.times{ subject.post{ raise StandardError } }
8186
5.times{ subject.post{ nil } }
82-
sleep(0.1)
83-
expect(subject.completed_task_count).to be > 0
87+
subject.post { latch.count_down }
88+
latch.wait(1)
89+
expect(subject.completed_task_count).to be > 1
8490
end
8591

8692
it 'returns the approximate number of tasks that were completed' do
8793
5.times{ subject.post{ raise StandardError } }
8894
5.times{ subject.post{ nil } }
89-
sleep(0.1)
9095
subject.shutdown
9196
subject.wait_for_termination(1)
92-
expect(subject.completed_task_count).to be > 0
97+
expect(subject.completed_task_count).to be > 1
9398
end
9499
end
95100

0 commit comments

Comments
 (0)