-
Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathcached_thread_pool_spec.rb
244 lines (195 loc) · 7.61 KB
/
cached_thread_pool_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
require 'concurrent/executor/cached_thread_pool'
require_relative 'thread_pool_shared'
module Concurrent
RSpec.describe CachedThreadPool do
subject do
described_class.new(fallback_policy: :discard)
end
after(:each) do
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
end
it_should_behave_like :thread_pool
let(:latch) { Concurrent::CountDownLatch.new }
context '#initialize' do
subject { described_class.new }
it 'sets :max_length to DEFAULT_MAX_POOL_SIZE' do
expect(subject.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
end
it 'sets :min_length to DEFAULT_MIN_POOL_SIZE' do
expect(subject.min_length).to eq described_class::DEFAULT_MIN_POOL_SIZE
end
it 'sets :idletime to DEFAULT_THREAD_IDLETIMEOUT' do
expect(subject.idletime).to eq described_class::DEFAULT_THREAD_IDLETIMEOUT
end
it 'sets :max_queue to DEFAULT_MAX_QUEUE_SIZE' do
expect(subject.max_queue).to eq described_class::DEFAULT_MAX_QUEUE_SIZE
end
end
context '#min_length' do
it 'returns zero on creation' do
expect(subject.min_length).to eq 0
end
it 'returns zero while running' do
10.times { subject.post { nil } }
subject.post { latch.count_down }
latch.wait(0.1)
expect(subject.min_length).to eq 0
end
it 'returns zero once shutdown' do
10.times { subject.post { nil } }
subject.post { latch.count_down }
latch.wait(0.1)
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject.min_length).to eq 0
end
end
context '#max_length' do
it 'returns :max_length on creation' do
expect(subject.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
end
it 'returns :max_length while running' do
10.times { subject.post { nil } }
subject.post { latch.count_down }
latch.wait(0.1)
expect(subject.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
end
it 'returns :max_length once shutdown' do
10.times { subject.post { nil } }
subject.post { latch.count_down }
latch.wait(0.1)
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
end
end
context '#largest_length' do
it 'returns zero on creation' do
expect(subject.largest_length).to eq 0
end
it 'returns a non-zero number once tasks have been received' do
10.times { subject.post { sleep(0.1) } }
subject.post { latch.count_down }
latch.wait(0.1)
expect(subject.largest_length).to be > 0
end
it 'returns a non-zero number after shutdown if tasks have been received' do
10.times { subject.post { sleep(0.1) } }
subject.post { latch.count_down }
latch.wait(0.1)
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject.largest_length).to be > 0
end
end
context '#idletime' do
subject { described_class.new(idletime: 42) }
it 'returns the thread idletime' do
expect(subject.idletime).to eq 42
end
end
context 'runtime-specific implementation' do
if Concurrent.on_jruby?
context '#initialize' do
it 'sets :fallback_policy correctly' do
clazz = java.util.concurrent.ThreadPoolExecutor::DiscardPolicy
policy = clazz.new
expect(clazz).to receive(:new).at_least(:once).with(any_args).and_return(policy)
subject = CachedThreadPool.new(fallback_policy: :discard)
expect(subject.fallback_policy).to eq :discard
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
end
it 'defaults :fallback_policy to :abort' do
subject = CachedThreadPool.new
expect(subject.fallback_policy).to eq :abort
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
end
it 'raises an exception if given an invalid :fallback_policy' do
expect {
CachedThreadPool.new(fallback_policy: :bogus)
}.to raise_error(ArgumentError)
end
end
else
context 'garbage collection' do
subject { described_class.new(idletime: 0.1, max_threads: 2) }
it 'removes from pool any thread that has been idle too long' do
latch = Concurrent::CountDownLatch.new(4)
4.times { subject.post { sleep 0.1; latch.count_down } }
sleep 0.4
expect(latch.wait(1)).to be true
expect(subject.length).to be < 4
end
it 'deals with dead threads' do
expect(subject).to receive(:ns_worker_died).exactly(5).times.and_call_original
dead_threads_queue = Queue.new
5.times { subject.post { sleep 0.1; dead_threads_queue.push Thread.current; raise Exception } }
sleep(0.2)
latch = Concurrent::CountDownLatch.new(5)
5.times { subject.post { sleep 0.1; latch.count_down } }
expect(latch.wait(1)).to be true
dead_threads = []
dead_threads << dead_threads_queue.pop until dead_threads_queue.empty?
expect(dead_threads.all? { |t| !t.alive? }).to be true
end
end
context 'worker creation and caching' do
subject { described_class.new(idletime: 1, max_threads: 5) }
it 'creates new workers when there are none available' do
expect(subject.length).to eq 0
5.times { sleep(0.1); subject << proc { sleep(1) } }
sleep(1)
expect(subject.length).to eq 5
end
it 'uses existing idle threads' do
5.times { subject << proc { sleep(0.1) } }
sleep(1)
expect(subject.length).to be >= 5
3.times { subject << proc { sleep(1) } }
sleep(0.1)
expect(subject.length).to be >= 3
end
end
end
context 'stress' do
configurations = [
{
min_threads: 2,
max_threads: ThreadPoolExecutor::DEFAULT_MAX_POOL_SIZE,
idletime: 60, # 1 minute
max_queue: 0, # unlimited
fallback_policy: :caller_runs, # shouldn't matter -- 0 max queue
},
{
min_threads: 2,
max_threads: 4,
idletime: 60, # 1 minute
max_queue: 0, # unlimited
fallback_policy: :caller_runs, # shouldn't matter -- 0 max queue
}
]
configurations.each do |config|
specify do
pool = RubyThreadPoolExecutor.new(config)
10.times do
count = Concurrent::CountDownLatch.new(100)
100.times do
pool.post { count.count_down }
end
count.wait
sleep 0.01 # let the tasks end after count_down
expect(pool.length).to be <= [200, config[:max_threads]].min
if pool.length > [110, config[:max_threads]].min
puts "ERRORSIZE #{pool.length} max #{config[:max_threads]}"
end
end
pool.shutdown
end
end
end
end
end
end