Skip to content

Commit 1d3e005

Browse files
committed
Merge pull request #212 from rkday/executor_ut_speedup
Remove loads of sleep statements in UTs
2 parents aaeef49 + 6861667 commit 1d3e005

File tree

1 file changed

+55
-30
lines changed

1 file changed

+55
-30
lines changed

spec/concurrent/executor/thread_pool_executor_shared.rb

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

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

1212
context '#initialize defaults' do
@@ -92,14 +92,14 @@
9292
end
9393

9494
it 'returns the set value when running' do
95-
5.times{ subject.post{ sleep(0.1) } }
96-
sleep(0.1)
95+
trigger = Concurrent::Event.new
96+
5.times{ subject.post{ trigger.wait } }
9797
expect(subject.max_queue).to eq expected_max
98+
trigger.set
9899
end
99100

100101
it 'returns the set value after stopping' do
101-
5.times{ subject.post{ sleep(0.1) } }
102-
sleep(0.1)
102+
5.times{ subject.post{ nil } }
103103
subject.shutdown
104104
subject.wait_for_termination(1)
105105
expect(subject.max_queue).to eq expected_max
@@ -123,29 +123,33 @@
123123
end
124124

125125
it 'returns zero when there are no enqueued tasks' do
126-
5.times{ subject.post{ nil } }
127-
sleep(0.1)
126+
latch = Concurrent::CountDownLatch.new(5)
127+
5.times{ subject.post{ latch.count_down } }
128+
latch.wait(0.1)
128129
expect(subject.queue_length).to eq 0
129130
end
130131

131132
it 'returns the size of the queue when tasks are enqueued' do
132-
100.times{ subject.post{ sleep(0.5) } }
133-
sleep(0.1)
133+
trigger = Concurrent::Event.new
134+
20.times{ subject.post{ trigger.wait } }
134135
expect(subject.queue_length).to be > 0
136+
trigger.set
135137
end
136138

137139
it 'returns zero when stopped' do
138-
100.times{ subject.post{ sleep(0.5) } }
139-
sleep(0.1)
140+
trigger = Concurrent::Event.new
141+
20.times{ subject.post{ trigger.wait } }
140142
subject.shutdown
143+
trigger.set
141144
subject.wait_for_termination(1)
142145
expect(subject.queue_length).to eq 0
143146
end
144147

145148
it 'can never be greater than :max_queue' do
146-
100.times{ subject.post{ sleep(0.5) } }
147-
sleep(0.1)
149+
trigger = Concurrent::Event.new
150+
20.times{ subject.post{ trigger.wait } }
148151
expect(subject.queue_length).to be <= expected_max
152+
trigger.set
149153
end
150154
end
151155

@@ -165,7 +169,6 @@
165169

166170
it 'returns :max_length when stopped' do
167171
100.times{ subject.post{ nil } }
168-
sleep(0.1)
169172
subject.shutdown
170173
subject.wait_for_termination(1)
171174
expect(subject.remaining_capacity).to eq expected_max
@@ -192,27 +195,35 @@
192195
end
193196

194197
specify '#post raises an error when the queue is at capacity' do
198+
trigger = Concurrent::Event.new
195199
expect {
196-
100.times{ subject.post{ sleep(1) } }
200+
20.times{ subject.post{ trigger.wait } }
197201
}.to raise_error(Concurrent::RejectedExecutionError)
202+
trigger.set
198203
end
199204

200205
specify '#<< raises an error when the queue is at capacity' do
206+
trigger = Concurrent::Event.new
201207
expect {
202-
100.times{ subject << proc { sleep(1) } }
208+
20.times{ subject << proc { trigger.wait } }
203209
}.to raise_error(Concurrent::RejectedExecutionError)
210+
trigger.set
204211
end
205212

206213
specify '#post raises an error when the executor is shutting down' do
214+
trigger = Concurrent::Event.new
207215
expect {
208-
subject.shutdown; subject.post{ sleep(1) }
216+
subject.shutdown; subject.post{ trigger.wait }
209217
}.to raise_error(Concurrent::RejectedExecutionError)
218+
trigger.set
210219
end
211220

212221
specify '#<< raises an error when the executor is shutting down' do
222+
trigger = Concurrent::Event.new
213223
expect {
214-
subject.shutdown; subject << proc { sleep(1) }
224+
subject.shutdown; subject << proc { trigger.wait }
215225
}.to raise_error(Concurrent::RejectedExecutionError)
226+
trigger.set
216227
end
217228

218229
specify 'a #post task is never executed when the queue is at capacity' do
@@ -239,8 +250,8 @@
239250
subject.post{ all_tasks_posted.wait; initial_executed.increment; }
240251
end
241252

242-
# Inject 100 more tasks, which should throw an exception
243-
100.times do
253+
# Inject 20 more tasks, which should throw an exception
254+
20.times do
244255
expect {
245256
subject.post { subsequent_executed.increment; }
246257
}.to raise_error(Concurrent::RejectedExecutionError)
@@ -286,8 +297,8 @@
286297
subject << proc { all_tasks_posted.wait; initial_executed.increment; }
287298
end
288299

289-
# Inject 100 more tasks, which should throw an exeption
290-
100.times do
300+
# Inject 20 more tasks, which should throw an exeption
301+
20.times do
291302
expect {
292303
subject << proc { subsequent_executed.increment; }
293304
}.to raise_error(Concurrent::RejectedExecutionError)
@@ -346,8 +357,8 @@
346357
subject.post{ all_tasks_posted.wait; initial_executed.increment; }
347358
end
348359

349-
# Inject 100 more tasks, which should be dropped without an exception
350-
100.times do
360+
# Inject 20 more tasks, which should be dropped without an exception
361+
20.times do
351362
subject.post{ subsequent_executed.increment; }
352363
end
353364

@@ -391,8 +402,8 @@
391402
subject << proc { all_tasks_posted.wait; initial_executed.increment; }
392403
end
393404

394-
# Inject 100 more tasks, which should be dropped without an exception
395-
100.times do
405+
# Inject 20 more tasks, which should be dropped without an exception
406+
20.times do
396407
subject << proc { subsequent_executed.increment; }
397408
end
398409

@@ -456,23 +467,37 @@
456467
end
457468

458469
specify '#post does not create any new threads when the queue is at capacity' do
470+
trigger = Concurrent::Event.new
459471
initial = Thread.list.length
460-
5.times{ subject.post{ sleep(0.1) } }
461-
expect(Thread.list.length).to be < initial + 5
472+
473+
# Post several tasks to the executor. Has to be a new thread,
474+
# because it will start blocking once the queue fills up.
475+
Thread.new do
476+
5.times{ subject.post{ trigger.wait } }
477+
end
478+
479+
expect(Thread.list.length).to be < initial + 1 + 5
480+
481+
# Let the executor tasks complete.
482+
trigger.set
462483
end
463484

464485
specify '#<< executes the task on the current thread when the queue is at capacity' do
486+
trigger = Concurrent::Event.new
465487
latch = Concurrent::CountDownLatch.new(5)
466-
subject.post{ sleep(1) }
488+
subject.post{ trigger.wait }
467489
5.times{|i| subject << proc { latch.count_down } }
468490
latch.wait(0.1)
491+
trigger.set
469492
end
470493

471494
specify '#post executes the task on the current thread when the queue is at capacity' do
495+
trigger = Concurrent::Event.new
472496
latch = Concurrent::CountDownLatch.new(5)
473-
subject.post{ sleep(1) }
497+
subject.post{ trigger.wait }
474498
5.times{|i| subject.post{ latch.count_down } }
475499
latch.wait(0.1)
500+
trigger.set
476501
end
477502

478503
specify '#post executes the task on the current thread when the executor is shutting down' do

0 commit comments

Comments
 (0)