Skip to content

Commit 3c7fcbe

Browse files
committed
Ensure all tested thread pools terminate
1 parent 88c33cd commit 3c7fcbe

13 files changed

+133
-75
lines changed

spec/concurrent/agent_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,9 @@ def update(time, old_value, new_value)
860860

861861
subject.await_for(5)
862862
expect(bucket).to eq [1, 2, 3]
863+
864+
executor.kill
865+
expect(executor.wait_for_termination(1)).to eq true
863866
end
864867
end
865868

spec/concurrent/executor/cached_thread_pool_spec.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module Concurrent
99
end
1010

1111
after(:each) do
12-
subject.kill
13-
subject.wait_for_termination(1)
12+
subject.shutdown
13+
expect(subject.wait_for_termination(1)).to eq true
1414
end
1515

1616
it_should_behave_like :thread_pool
@@ -19,20 +19,22 @@ module Concurrent
1919

2020
context '#initialize' do
2121

22+
subject { described_class.new }
23+
2224
it 'sets :max_length to DEFAULT_MAX_POOL_SIZE' do
23-
expect(described_class.new.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
25+
expect(subject.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
2426
end
2527

2628
it 'sets :min_length to DEFAULT_MIN_POOL_SIZE' do
27-
subject = expect(described_class.new.min_length).to eq described_class::DEFAULT_MIN_POOL_SIZE
29+
expect(subject.min_length).to eq described_class::DEFAULT_MIN_POOL_SIZE
2830
end
2931

3032
it 'sets :idletime to DEFAULT_THREAD_IDLETIMEOUT' do
31-
subject = expect(described_class.new.idletime).to eq described_class::DEFAULT_THREAD_IDLETIMEOUT
33+
expect(subject.idletime).to eq described_class::DEFAULT_THREAD_IDLETIMEOUT
3234
end
3335

3436
it 'sets :max_queue to DEFAULT_MAX_QUEUE_SIZE' do
35-
subject = expect(described_class.new.max_queue).to eq described_class::DEFAULT_MAX_QUEUE_SIZE
37+
expect(subject.max_queue).to eq described_class::DEFAULT_MAX_QUEUE_SIZE
3638
end
3739
end
3840

@@ -54,7 +56,7 @@ module Concurrent
5456
subject.post { latch.count_down }
5557
latch.wait(0.1)
5658
subject.shutdown
57-
subject.wait_for_termination(1)
59+
expect(subject.wait_for_termination(1)).to eq true
5860
expect(subject.min_length).to eq 0
5961
end
6062
end
@@ -77,7 +79,7 @@ module Concurrent
7779
subject.post { latch.count_down }
7880
latch.wait(0.1)
7981
subject.shutdown
80-
subject.wait_for_termination(1)
82+
expect(subject.wait_for_termination(1)).to eq true
8183
expect(subject.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
8284
end
8385
end
@@ -100,7 +102,7 @@ module Concurrent
100102
subject.post { latch.count_down }
101103
latch.wait(0.1)
102104
subject.shutdown
103-
subject.wait_for_termination(1)
105+
expect(subject.wait_for_termination(1)).to eq true
104106
expect(subject.largest_length).to be > 0
105107
end
106108
end
@@ -127,11 +129,15 @@ module Concurrent
127129

128130
subject = CachedThreadPool.new(fallback_policy: :discard)
129131
expect(subject.fallback_policy).to eq :discard
132+
subject.shutdown
133+
expect(subject.wait_for_termination(1)).to eq true
130134
end
131135

132136
it 'defaults :fallback_policy to :abort' do
133137
subject = CachedThreadPool.new
134138
expect(subject.fallback_policy).to eq :abort
139+
subject.shutdown
140+
expect(subject.wait_for_termination(1)).to eq true
135141
end
136142

137143
it 'raises an exception if given an invalid :fallback_policy' do

spec/concurrent/executor/executor_service_shared.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
RSpec.shared_examples :executor_service do
44

55
after(:each) do
6-
subject.kill
7-
subject.wait_for_termination(0.1)
6+
subject.shutdown
7+
expect(subject.wait_for_termination(1)).to eq true
88
end
99

1010
it_should_behave_like :global_thread_pool
@@ -40,21 +40,21 @@
4040
end
4141

4242
it 'returns false when the thread pool is shutting down' do
43-
subject.post{ sleep(1) }
43+
subject.post{ sleep(0.5) }
4444
subject.shutdown
45-
subject.wait_for_termination(1)
4645
expect(subject).not_to be_running
46+
expect(subject.wait_for_termination(1)).to eq true
4747
end
4848

4949
it 'returns false when the thread pool is shutdown' do
5050
subject.shutdown
51-
subject.wait_for_termination(1)
51+
expect(subject.wait_for_termination(1)).to eq true
5252
expect(subject).not_to be_running
5353
end
5454

5555
it 'returns false when the thread pool is killed' do
5656
subject.kill
57-
subject.wait_for_termination(1)
57+
expect(subject.wait_for_termination(1)).to eq true
5858
expect(subject).not_to be_running
5959
end
6060
end
@@ -67,7 +67,7 @@
6767
subject.post{ sleep(0.1); latch1.count_down }
6868
latch1.wait(1)
6969
subject.shutdown
70-
subject.wait_for_termination
70+
expect(subject.wait_for_termination(1)).to eq true
7171
begin
7272
subject.post{ latch2.count_down }
7373
rescue Concurrent::RejectedExecutionError
@@ -97,7 +97,7 @@
9797
latch = Concurrent::CountDownLatch.new(1)
9898
subject.post{ sleep(0.1); latch.count_down }
9999
subject.shutdown
100-
subject.wait_for_termination(1)
100+
expect(subject.wait_for_termination(1)).to eq true
101101
expect(latch.wait(1)).to be_truthy
102102
end
103103

@@ -107,7 +107,7 @@
107107
subject.post { sleep 0.1; q << i }
108108
end
109109
subject.shutdown
110-
subject.wait_for_termination(1)
110+
expect(subject.wait_for_termination(1)).to eq true
111111
expect(q.length).to eq 5
112112
end
113113

@@ -120,7 +120,7 @@
120120
subject.post{ expected.increment }
121121
rescue Concurrent::RejectedExecutionError
122122
end
123-
subject.wait_for_termination(1)
123+
expect(subject.wait_for_termination(1)).to eq true
124124
expect(expected.value).to eq(2)
125125
end
126126
end
@@ -176,10 +176,12 @@
176176

177177
it 'returns false when shutdown fails to complete before timeout' do
178178
unless subject.serialized?
179-
100.times{ subject.post{ sleep(1) } }
179+
latch = Concurrent::CountDownLatch.new 1
180+
100.times{ subject.post{ latch.wait } }
180181
sleep(0.1)
181182
subject.shutdown
182-
expect(subject.wait_for_termination(0)).to be_falsey
183+
expect(subject.wait_for_termination(0.01)).to be_falsey
184+
latch.count_down
183185
end
184186
end
185187

spec/concurrent/executor/fixed_thread_pool_spec.rb

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module Concurrent
88
subject { described_class.new(num_threads) }
99

1010
after(:each) do
11-
subject.kill
12-
subject.wait_for_termination(0.1)
11+
subject.shutdown
12+
expect(subject.wait_for_termination(1)).to eq true
1313
end
1414

1515
it_should_behave_like :thread_pool
@@ -55,25 +55,29 @@ module Concurrent
5555
it 'sets explicit :max_queue correctly' do
5656
subject = described_class.new(5, :max_queue => 10)
5757
expect(subject.max_queue).to eq 10
58+
subject.shutdown
59+
expect(subject.wait_for_termination(1)).to eq true
5860
end
5961

6062
it 'correctly sets valid :fallback_policy' do
6163
subject = described_class.new(5, :fallback_policy => :caller_runs)
6264
expect(subject.fallback_policy).to eq :caller_runs
65+
subject.shutdown
66+
expect(subject.wait_for_termination(1)).to eq true
6367
end
6468

6569
it "correctly sets valid :idletime" do
6670
subject = described_class.new(5, :idletime => 10)
6771
expect(subject.idletime).to eq 10
72+
subject.shutdown
73+
expect(subject.wait_for_termination(1)).to eq true
6874
end
6975

7076
it 'raises an exception if given an invalid :fallback_policy' do
7177
expect {
7278
described_class.new(5, fallback_policy: :bogus)
7379
}.to raise_error(ArgumentError)
7480
end
75-
76-
7781
end
7882

7983
context '#min_length' do
@@ -94,7 +98,7 @@ module Concurrent
9498
subject.post { latch.count_down }
9599
latch.wait(0.1)
96100
subject.shutdown
97-
subject.wait_for_termination(1)
101+
expect(subject.wait_for_termination(1)).to eq true
98102
expect(subject.min_length).to eq num_threads
99103
end
100104
end
@@ -117,7 +121,7 @@ module Concurrent
117121
subject.post { latch.count_down }
118122
latch.wait(0.1)
119123
subject.shutdown
120-
subject.wait_for_termination(1)
124+
expect(subject.wait_for_termination(1)).to eq true
121125
expect(subject.max_length).to eq num_threads
122126
end
123127
end
@@ -150,7 +154,7 @@ module Concurrent
150154
subject.post { latch.count_down }
151155
latch.wait(0.1)
152156
subject.shutdown
153-
subject.wait_for_termination(1)
157+
expect(subject.wait_for_termination(1)).to eq true
154158
expect(subject.largest_length).to eq num_threads
155159
end
156160
end
@@ -176,10 +180,13 @@ module Concurrent
176180

177181
it 'never creates more than :num_threads threads' do
178182
pool = described_class.new(5)
179-
100.times{ pool << proc{ sleep(1) } }
183+
latch = Concurrent::CountDownLatch.new 1
184+
100.times{ pool << proc{ latch.wait } }
180185
sleep(0.1)
181186
expect(pool.length).to eq 5
182-
pool.kill
187+
latch.count_down
188+
pool.shutdown
189+
expect(pool.wait_for_termination(1)).to eq true
183190
end
184191
end
185192

@@ -190,7 +197,7 @@ module Concurrent
190197
end
191198

192199
after(:each) do
193-
subject.kill
200+
subject.shutdown
194201
end
195202

196203
# On abort, it should raise an error
@@ -209,6 +216,8 @@ module Concurrent
209216
end
210217
latch.wait(1)
211218
}.to raise_error(RejectedExecutionError)
219+
subject.shutdown
220+
expect(subject.wait_for_termination(1)).to eq true
212221
end
213222

214223
# On discard, we'd expect no error, but also not all five results
@@ -227,6 +236,8 @@ module Concurrent
227236
latch.wait(1)
228237

229238
expect(@queue.length).to be < 5
239+
subject.shutdown
240+
expect(subject.wait_for_termination(1)).to eq true
230241
end
231242

232243
# To check for caller_runs, we'll check how many unique threads
@@ -254,6 +265,9 @@ module Concurrent
254265
#NOTE: This test is very, very difficult to setup properly. Hence the 'be_within' matcher
255266
expect(a.size).to be_within(1).of(5) # one for each run of the block
256267
expect(a.uniq.size).to be_within(1).of(3) # one for each of the two threads, plus the caller
268+
269+
subject.shutdown
270+
expect(subject.wait_for_termination(1)).to eq true
257271
end
258272
end
259273

@@ -268,6 +282,8 @@ module Concurrent
268282

269283
subject = FixedThreadPool.new(5, fallback_policy: :discard)
270284
expect(subject.fallback_policy).to eq :discard
285+
subject.shutdown
286+
expect(subject.wait_for_termination(1)).to eq true
271287
end
272288

273289
else
@@ -290,7 +306,8 @@ module Concurrent
290306
5.times{ pool << proc{ sleep(1) } }
291307
sleep(0.1)
292308
expect(pool.length).to eq 5
293-
pool.kill
309+
pool.shutdown
310+
expect(pool.wait_for_termination(1)).to eq true
294311
end
295312
end
296313
end

spec/concurrent/executor/java_single_thread_executor_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module Concurrent
77
RSpec.describe JavaSingleThreadExecutor, :type=>:jruby do
88

99
after(:each) do
10-
subject.kill
11-
subject.wait_for_termination(0.1)
10+
subject.shutdown
11+
expect(subject.wait_for_termination(1)).to eq true
1212
end
1313

1414
subject { JavaSingleThreadExecutor.new }

spec/concurrent/executor/java_thread_pool_executor_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module Concurrent
77
RSpec.describe JavaThreadPoolExecutor, :type => :jruby do
88

99
after(:each) do
10-
subject.kill
11-
subject.wait_for_termination(0.1)
10+
subject.shutdown
11+
expect(subject.wait_for_termination(1)).to eq true
1212
end
1313

1414
subject do

spec/concurrent/executor/ruby_single_thread_executor_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module Concurrent
55
RSpec.describe RubySingleThreadExecutor, :type=>:mrirbx do
66

77
after(:each) do
8-
subject.kill
9-
subject.wait_for_termination(0.1)
8+
subject.shutdown
9+
expect(subject.wait_for_termination(1)).to eq true
1010
end
1111

1212
subject { RubySingleThreadExecutor.new }

spec/concurrent/executor/ruby_thread_pool_executor_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module Concurrent
55
RSpec.describe RubyThreadPoolExecutor, :type=>:mrirbx do
66

77
after(:each) do
8-
subject.kill
9-
subject.wait_for_termination(0.1)
8+
subject.shutdown
9+
expect(subject.wait_for_termination(1)).to eq true
1010
end
1111

1212
subject do
@@ -46,10 +46,12 @@ module Concurrent
4646
end
4747

4848
it 'returns the remaining capacity when tasks are enqueued' do
49-
100.times{ subject.post{ sleep(0.5) } }
49+
block = Concurrent::CountDownLatch.new
50+
100.times{ subject.post{ block.wait } }
5051
subject.post { latch.count_down }
5152
latch.wait(0.1)
5253
expect(subject.remaining_capacity).to be < expected_max
54+
block.count_down
5355
end
5456
end
5557
end

0 commit comments

Comments
 (0)