-
Notifications
You must be signed in to change notification settings - Fork 416
/
thread_pool_shared.rb
269 lines (223 loc) · 6.5 KB
/
thread_pool_shared.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
require 'spec_helper'
require_relative 'global_thread_pool_shared'
share_examples_for :executor_service do
after(:each) do
subject.kill
sleep(0.1)
end
it_should_behave_like :global_thread_pool
context '#post' do
it 'rejects the block while shutting down' do
latch = Concurrent::CountDownLatch.new(1)
subject.post{ sleep(1) }
subject.shutdown
subject.post{ latch.count_down }
latch.wait(0.1).should be_false
end
it 'returns false while shutting down' do
subject.post{ sleep(1) }
subject.shutdown
subject.post{ nil }.should be_false
end
it 'rejects the block once shutdown' do
subject.shutdown
latch = Concurrent::CountDownLatch.new(1)
subject.post{ sleep(1) }
subject.post{ latch.count_down }
latch.wait(0.1).should be_false
end
it 'returns false once shutdown' do
subject.post{ nil }
subject.shutdown
sleep(0.1)
subject.post{ nil }.should be_false
end
end
context '#running?' do
it 'returns true when the thread pool is running' do
subject.should be_running
end
it 'returns false when the thread pool is shutting down' do
subject.post{ sleep(1) }
subject.shutdown
subject.wait_for_termination(1)
subject.should_not be_running
end
it 'returns false when the thread pool is shutdown' do
subject.shutdown
subject.wait_for_termination(1)
subject.should_not be_running
end
it 'returns false when the thread pool is killed' do
subject.kill
subject.wait_for_termination(1)
subject.should_not be_running
end
end
context '#shutdown' do
it 'stops accepting new tasks' do
subject.post{ sleep(1) }
sleep(0.1)
subject.shutdown
@expected = false
subject.post{ @expected = true }.should be_false
sleep(1)
@expected.should be_false
end
it 'allows in-progress tasks to complete' do
@expected = false
subject.post{ @expected = true }
sleep(0.1)
subject.shutdown
sleep(1)
@expected.should be_true
end
it 'allows pending tasks to complete' do
@expected = false
subject.post{ sleep(0.2) }
subject.post{ sleep(0.2); @expected = true }
sleep(0.1)
subject.shutdown
sleep(1)
@expected.should be_true
end
end
context '#shutdown followed by #wait_for_termination' do
it "allows in-progress tasks to complete" do
@expected = false
subject.post{ sleep 0.1; @expected = true }
subject.shutdown
subject.wait_for_termination(1)
@expected.should be_true
end
it 'allows pending tasks to complete' do
@expected = false
subject.post{ sleep(0.1) }
subject.post{ sleep(0.1); @expected = true }
subject.shutdown
subject.wait_for_termination(1)
@expected.should be_true
end
it "stops accepting/running new tasks" do
@expected = :start
subject.post{ sleep(0.1) }
subject.post{ sleep(0.1); @expected = :should_be_run }
subject.shutdown
subject.post{ @expected = :should_not_be_run }
subject.wait_for_termination(1)
@expected.should == :should_be_run
end
end
context '#kill' do
it 'stops accepting new tasks' do
subject.post{ sleep(1) }
sleep(0.1)
subject.kill
@expected = false
subject.post{ @expected = true }.should be_false
sleep(1)
@expected.should be_false
end
it 'rejects all pending tasks' do
subject.post{ sleep(1) }
sleep(0.1)
subject.kill
sleep(0.1)
subject.post{ nil }.should be_false
end
end
context '#wait_for_termination' do
it 'immediately returns true when no operations are pending' do
subject.shutdown
subject.wait_for_termination(0).should be_true
end
it 'returns true after shutdown has complete' do
10.times { subject << proc{ nil } }
sleep(0.1)
subject.shutdown
subject.wait_for_termination(1).should be_true
end
it 'returns true when shutdown sucessfully completes before timeout' do
subject.post{ sleep(0.5) }
sleep(0.1)
subject.shutdown
subject.wait_for_termination(1).should be_true
end
it 'returns false when shutdown fails to complete before timeout' do
100.times{ subject.post{ sleep(1) } }
sleep(0.1)
subject.shutdown
subject.wait_for_termination(0).should be_false
end
end
end
share_examples_for :thread_pool do
after(:each) do
subject.kill
sleep(0.1)
end
it_should_behave_like :executor_service
context '#length' do
it 'returns zero on creation' do
subject.length.should eq 0
end
it 'returns zero once shut down' do
5.times{ subject.post{ sleep(0.1) } }
sleep(0.1)
subject.shutdown
subject.wait_for_termination(1)
subject.length.should eq 0
end
it 'aliased as #current_length' do
5.times{ subject.post{ sleep(0.1) } }
sleep(0.1)
subject.current_length.should eq subject.length
end
end
context '#scheduled_task_count' do
it 'returns zero on creation' do
subject.scheduled_task_count.should eq 0
end
it 'returns the approximate number of tasks that have been post thus far' do
10.times{ subject.post{ nil } }
sleep(0.1)
subject.scheduled_task_count.should > 0
end
it 'returns the approximate number of tasks that were post' do
10.times{ subject.post{ nil } }
sleep(0.1)
subject.shutdown
subject.wait_for_termination(1)
subject.scheduled_task_count.should > 0
end
end
context '#completed_task_count' do
it 'returns zero on creation' do
subject.completed_task_count.should eq 0
end
it 'returns the approximate number of tasks that have been completed thus far' do
5.times{ subject.post{ raise StandardError } }
5.times{ subject.post{ nil } }
sleep(0.1)
subject.completed_task_count.should > 0
end
it 'returns the approximate number of tasks that were completed' do
5.times{ subject.post{ raise StandardError } }
5.times{ subject.post{ nil } }
sleep(0.1)
subject.shutdown
subject.wait_for_termination(1)
subject.completed_task_count.should > 0
end
end
context '#shutdown' do
it 'allows threads to exit normally' do
10.times{ subject << proc{ nil } }
subject.length.should > 0
sleep(0.1)
subject.shutdown
sleep(1)
subject.length.should == 0
end
end
end