trident 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -178,7 +178,7 @@ class Trident::CLITest < MiniTest::Should::TestCase
178
178
 
179
179
  assert_equal 5, pools['pool1'].size
180
180
  assert_equal @handlers['handler1'], pools['pool1'].handler
181
- assert_equal({"foo" => "bar"}, pools['pool1'].options)
181
+ assert_equal({"options" => {"foo" => "bar"}, "handler" => "handler1"}, pools['pool1'].options)
182
182
  end
183
183
 
184
184
  should "filter pools if given" do
@@ -1,7 +1,6 @@
1
1
  require_relative '../../test_helper'
2
2
 
3
3
  class Trident::PoolManagerTest < MiniTest::Should::TestCase
4
-
5
4
  setup do
6
5
  SignalHandler.stubs(:reset_for_fork)
7
6
 
@@ -27,12 +26,11 @@ class Trident::PoolManagerTest < MiniTest::Should::TestCase
27
26
  signal_mappings = {'stop_forcefully' => 'KILL', 'stop_gracefully' => 'TERM'}
28
27
  @handler1 = PoolHandler.new("foo", "TestPoolWorker", env, signal_mappings, {})
29
28
  @handler2 = PoolHandler.new("bar", "TestPoolWorker", env, signal_mappings, {})
30
- @pool1 = Pool.new("foo", @handler1, 2, 'sleep' => 0.1)
31
- @pool2 = Pool.new("bar", @handler2, 3, 'sleep' => 0.1)
29
+ @pool1 = Pool.new("foo", @handler1, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
30
+ @pool2 = Pool.new("bar", @handler2, 'size' => 3, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
32
31
  end
33
32
 
34
33
  context "#start" do
35
-
36
34
  should "start workers for each pool" do
37
35
  manager = PoolManager.new("mymanager", [@pool1, @pool2], false)
38
36
  manager.expects(:load_handlers).never
@@ -53,11 +51,9 @@ class Trident::PoolManagerTest < MiniTest::Should::TestCase
53
51
  # once for each worker plus once for each handler
54
52
  assert_equal 7, $counter.read
55
53
  end
56
-
57
54
  end
58
55
 
59
56
  context "#stop" do
60
-
61
57
  should "stop workers for each pool" do
62
58
  manager = PoolManager.new("mymanager", [@pool1, @pool2], false)
63
59
  manager.start
@@ -86,11 +82,9 @@ class Trident::PoolManagerTest < MiniTest::Should::TestCase
86
82
  manager.expects(:stop).with("stop_gracefully")
87
83
  manager.stop_gracefully
88
84
  end
89
-
90
85
  end
91
86
 
92
87
  context "#wait" do
93
-
94
88
  should "wait for processes to exit" do
95
89
  manager = PoolManager.new("mymanager", [@pool1, @pool2], false)
96
90
  manager.start
@@ -106,11 +100,9 @@ class Trident::PoolManagerTest < MiniTest::Should::TestCase
106
100
  assert_empty @pool1.workers
107
101
  assert_empty @pool2.workers
108
102
  end
109
-
110
103
  end
111
104
 
112
105
  context "#update" do
113
-
114
106
  should "update status of all pools" do
115
107
  manager = PoolManager.new("mymanager", [@pool1, @pool2], false)
116
108
  manager.start
@@ -125,7 +117,5 @@ class Trident::PoolManagerTest < MiniTest::Should::TestCase
125
117
  refute_equal orig_pool1, @pool1.workers
126
118
  refute_equal orig_pool2, @pool2.workers
127
119
  end
128
-
129
120
  end
130
-
131
121
  end
@@ -1,6 +1,12 @@
1
1
  require_relative '../../test_helper'
2
+ require 'pry'
2
3
 
3
4
  class Trident::PoolTest < MiniTest::Should::TestCase
5
+ class DeadbeatPool < Pool
6
+ def stop
7
+ @size = 0
8
+ end
9
+ end
4
10
 
5
11
  setup do
6
12
  SignalHandler.stubs(:reset_for_fork)
@@ -15,6 +21,7 @@ class Trident::PoolTest < MiniTest::Should::TestCase
15
21
  end
16
22
  def start
17
23
  sleep(@o['sleep']) if @o['sleep']
24
+ exit!(0)
18
25
  end
19
26
  end
20
27
  EOS
@@ -23,56 +30,66 @@ class Trident::PoolTest < MiniTest::Should::TestCase
23
30
  end
24
31
 
25
32
  context "#spawn_worker" do
26
-
27
33
  should "fork a worker" do
28
- pool = Pool.new("foo", @handler, 1, 'sleep' => 0.1)
34
+ pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
29
35
  assert_empty pool.workers
30
36
  pool.send(:spawn_worker)
31
37
  assert_equal 1, pool.workers.size
32
- Process.waitpid(pool.workers.first)
38
+ Process.waitpid(pool.workers.first.pid)
33
39
  assert $?.success?
34
40
  end
35
-
36
41
  end
37
42
 
38
43
  context "#kill_worker" do
39
-
40
44
  should "kill a worker" do
41
- pool = Pool.new("foo", @handler, 1, 'sleep' => 1)
45
+ pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => Dir.mktmpdir, 'sleep' => 1)
42
46
  pool.send(:spawn_worker)
43
- pid = pool.workers.first
47
+ worker = pool.workers.first
44
48
 
45
- pool.send(:kill_worker, pid, 'stop_forcefully')
46
- Process.waitpid(pid)
49
+ pool.send(:kill_worker, worker, 'stop_forcefully')
50
+ Process.waitpid(worker.pid)
47
51
  assert ! $?.success?
48
52
  assert_empty pool.workers
49
53
  end
50
54
 
51
55
  should "kill a worker with specific signal" do
52
- pool = Pool.new("foo", @handler, 1, 'sleep' => 1)
56
+ pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => Dir.mktmpdir, 'sleep' => 1)
53
57
  pool.send(:spawn_worker)
54
- pid = pool.workers.first
58
+ worker = pool.workers.first
55
59
 
56
- Process.expects(:kill).with("TERM", pid)
57
- pool.send(:kill_worker, pid, 'stop_gracefully')
60
+ Process.expects(:kill).with("TERM", worker.pid)
61
+ pool.send(:kill_worker, worker, 'stop_gracefully')
58
62
  end
59
-
60
63
  end
61
64
 
62
65
  context "#spawn_workers" do
63
-
64
66
  should "start multiple workers" do
65
- pool = Pool.new("foo", @handler, 4, 'sleep' => 1)
67
+ pool = Pool.new("foo", @handler, 'size' => 4, 'pids_dir' => Dir.mktmpdir, 'sleep' => 1)
66
68
  pool.send(:spawn_workers, 4)
67
69
  assert_equal 4, pool.workers.size
68
70
  end
69
71
 
72
+ context "forked process" do
73
+ should "clean up its pid file when complete" do
74
+ pool = Pool.new("foo", @handler, 'size' => 4, 'pids_dir' => Dir.mktmpdir, 'sleep' => 1)
75
+ pool.send(:spawn_workers, 1)
76
+
77
+ assert_equal 1, pool.workers.size
78
+
79
+ worker = pool.workers.first
80
+ assert File.exists?(File.join(pool.orphans_dir, "#{worker.pid}.pid"))
81
+
82
+ pool.send(:kill_worker, worker, 'stop_gracefully')
83
+ Process.waitpid(worker.pid)
84
+
85
+ refute File.exists?(File.join(pool.orphans_dir, "#{worker.pid}.pid"))
86
+ end
87
+ end
70
88
  end
71
89
 
72
90
  context "#kill_workers" do
73
-
74
91
  should "kill multiple workers, most recent first" do
75
- pool = Pool.new("foo", @handler, 4, 'sleep' => 1)
92
+ pool = Pool.new("foo", @handler, 'size' => 4, 'pids_dir' => Dir.mktmpdir, 'sleep' => 1)
76
93
  pool.send(:spawn_workers, 4)
77
94
  orig_workers = pool.workers.dup
78
95
  assert_equal 4, orig_workers.size
@@ -81,13 +98,11 @@ class Trident::PoolTest < MiniTest::Should::TestCase
81
98
  assert_equal 1, pool.workers.size
82
99
  assert_equal orig_workers.first, pool.workers.first
83
100
  end
84
-
85
101
  end
86
102
 
87
103
  context "#cleanup_dead_workers" do
88
-
89
104
  should "stop tracking workers that have died" do
90
- pool = Pool.new("foo", @handler, 4, 'sleep' => 0)
105
+ pool = Pool.new("foo", @handler, 'size' => 4, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0)
91
106
  pool.send(:spawn_workers, 4)
92
107
 
93
108
  sleep 0.1
@@ -97,7 +112,7 @@ class Trident::PoolTest < MiniTest::Should::TestCase
97
112
  end
98
113
 
99
114
  should "block waiting for workers that have died when blocking" do
100
- pool = Pool.new("foo", @handler, 1, 'sleep' => 0.2)
115
+ pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.2)
101
116
  pool.send(:spawn_workers, 1)
102
117
  assert_equal 1, pool.workers.size
103
118
 
@@ -109,7 +124,7 @@ class Trident::PoolTest < MiniTest::Should::TestCase
109
124
  end
110
125
 
111
126
  should "not block waiting for workers that have died when not-blocking" do
112
- pool = Pool.new("foo", @handler, 1, 'sleep' => 0.1)
127
+ pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
113
128
  pool.send(:spawn_workers, 1)
114
129
  assert_equal 1, pool.workers.size
115
130
 
@@ -118,7 +133,7 @@ class Trident::PoolTest < MiniTest::Should::TestCase
118
133
  end
119
134
 
120
135
  should "cleanup workers that have died even if already waited on" do
121
- pool = Pool.new("foo", @handler, 4, 'sleep' => 0)
136
+ pool = Pool.new("foo", @handler, 'size' => 4, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0)
122
137
  pool.send(:spawn_workers, 4)
123
138
 
124
139
  # Calling process.wait on a pid that was already waited on throws a ECHLD
@@ -128,14 +143,11 @@ class Trident::PoolTest < MiniTest::Should::TestCase
128
143
 
129
144
  assert_equal 0, pool.workers.size
130
145
  end
131
-
132
-
133
146
  end
134
147
 
135
148
  context "#maintain_worker_count" do
136
-
137
149
  should "spawn workers when count is low" do
138
- pool = Pool.new("foo", @handler, 2, 'sleep' => 0.1)
150
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
139
151
  assert_empty pool.workers
140
152
 
141
153
  pool.send(:maintain_worker_count, 'stop_gracefully')
@@ -143,7 +155,7 @@ class Trident::PoolTest < MiniTest::Should::TestCase
143
155
  end
144
156
 
145
157
  should "kill workers when count is high" do
146
- pool = Pool.new("foo", @handler, 2, 'sleep' => 0.1)
158
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
147
159
  pool.send(:spawn_workers, 4)
148
160
  assert_equal 4, pool.workers.size
149
161
 
@@ -152,23 +164,58 @@ class Trident::PoolTest < MiniTest::Should::TestCase
152
164
  end
153
165
 
154
166
  should "kill workers with given action when count is high" do
155
- pool = Pool.new("foo", @handler, 2, 'sleep' => 0.1)
167
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
156
168
  pool.send(:spawn_workers, 4)
157
169
  assert_equal 4, pool.workers.size
158
170
 
159
- Process.expects(:kill).with("KILL", pool.workers.to_a[-1])
160
- Process.expects(:kill).with("KILL", pool.workers.to_a[-2])
171
+ Process.expects(:kill).with("KILL", pool.workers.to_a[-1].pid)
172
+ Process.expects(:kill).with("KILL", pool.workers.to_a[-2].pid)
161
173
  pool.send(:maintain_worker_count, 'stop_forcefully')
162
174
 
163
175
  pool.send(:spawn_workers, 2)
164
- Process.expects(:kill).with("TERM", pool.workers.to_a[-1])
165
- Process.expects(:kill).with("TERM", pool.workers.to_a[-2])
176
+ Process.expects(:kill).with("TERM", pool.workers.to_a[-1].pid)
177
+ Process.expects(:kill).with("TERM", pool.workers.to_a[-2].pid)
178
+
179
+ pool.send(:maintain_worker_count, 'stop_gracefully')
180
+ end
181
+
182
+ should "do nothing when orphan count is high and no workers are present" do
183
+ dir = Dir.mktmpdir
184
+ pool = DeadbeatPool.new("foo", @handler, 'size' => 4, 'pids_dir' => dir, 'sleep' => 0.1)
185
+ pool.start
186
+ pool.stop
187
+
188
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => dir, 'sleep' => 0.1)
189
+ pool.send(:maintain_worker_count, 'stop_gracefully')
190
+
191
+ assert_equal 0, pool.workers.size
192
+ assert_equal 4, pool.orphans.size
193
+ end
194
+
195
+ should "kill workers when orphan count is high and workers are present" do
196
+ dir = Dir.mktmpdir
197
+ pool = DeadbeatPool.new("foo", @handler, 'size' => 4, 'pids_dir' => dir, 'sleep' => 0.1)
198
+ pool.start
199
+ pool.stop
200
+
201
+ new_pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => dir, 'sleep' => 0.1)
202
+ assert_equal 4, new_pool.orphans.size
203
+
204
+ new_pool.send(:spawn_workers, 4)
205
+ assert_equal 4, pool.workers.size
206
+
166
207
  pool.send(:maintain_worker_count, 'stop_gracefully')
208
+
209
+ pool.workers.each do |worker|
210
+ Process.waitpid(worker.pid)
211
+ end
212
+
213
+ assert_equal 0, pool.workers.size
167
214
  end
168
215
 
169
216
  should "do nothing when count is correct" do
170
217
  Process.expects(:kill).never
171
- pool = Pool.new("foo", @handler, 2, 'sleep' => 0.1)
218
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
172
219
  pool.send(:spawn_workers, 2)
173
220
  orig_workers = pool.workers.dup
174
221
  assert_equal 2, orig_workers.size
@@ -177,57 +224,251 @@ class Trident::PoolTest < MiniTest::Should::TestCase
177
224
  assert_equal 2, pool.workers.size
178
225
  assert_equal orig_workers, pool.workers
179
226
  end
180
-
181
227
  end
182
228
 
183
229
  context "#start" do
184
-
185
230
  should "start up the workers" do
186
- pool = Pool.new("foo", @handler, 2, 'sleep' => 0.1)
231
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
187
232
  pool.start
188
233
  assert_equal 2, pool.workers.size
189
234
  end
190
-
191
235
  end
192
236
 
193
237
  context "#stop" do
194
-
195
238
  should "stop the workers" do
196
- pool = Pool.new("foo", @handler, 2, 'sleep' => 0.1)
239
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
197
240
  pool.start
198
241
  assert_equal 2, pool.workers.size
199
242
  pool.stop
200
243
  assert_empty pool.workers
201
244
  end
202
-
203
245
  end
204
246
 
205
247
  context "#wait" do
206
-
207
248
  should "block till all workers complete" do
208
- pool = Pool.new("foo", @handler, 2, 'sleep' => 0.1)
249
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
209
250
  pool.start
210
251
  assert_equal 2, pool.workers.size
211
252
  pool.wait
212
253
  assert_empty pool.workers
213
254
  end
214
-
215
255
  end
216
256
 
217
257
  context "#update" do
218
-
219
258
  should "update monitored workers" do
220
- pool = Pool.new("foo", @handler, 2, 'sleep' => 0.2)
259
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.2)
221
260
  pool.start
222
261
  orig_workers = pool.workers.dup
223
262
  assert_equal 2, orig_workers.size
224
- Process.kill("KILL", orig_workers.first)
263
+ Process.kill("KILL", orig_workers.first.pid)
225
264
  sleep(0.1)
226
265
  assert_equal orig_workers, pool.workers
227
266
  pool.update
228
267
  refute_equal orig_workers, pool.workers
229
268
  end
269
+ end
270
+
271
+ context "#load_orphans" do
272
+ should "load orphaned workers from the pool's pid directory" do
273
+ dir = Dir.mktmpdir
274
+ pool = DeadbeatPool.new("foo", @handler, 'size' => 1, 'pids_dir' => dir, 'sleep' => 0.1)
275
+ pool.start
276
+ pid = pool.workers.first.pid
277
+ pool.stop
278
+
279
+ new_pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => dir, 'sleep' => 0.1)
280
+ assert_equal 1, new_pool.orphans.size
281
+ assert_equal pid, new_pool.orphans.first.pid
282
+ end
283
+
284
+ should "not load dead orphan workers" do
285
+ tmp = Dir.mktmpdir
286
+ File.open(File.join(tmp, "99999.pid"), 'w') do |f|
287
+ f << '99999'
288
+ end
230
289
 
290
+ pool = Pool.new("foo", @handler, 'pids_dir' => tmp , 'size' => 2, 'sleep' => 0.1)
291
+ assert 0, pool.orphans.size
292
+ end
231
293
  end
232
294
 
295
+ context "#cleanup_orphaned_workers" do
296
+ should "remove any orphaned workers that throw Errno::ESRCH (does not exist)" do
297
+ dir = Dir.mktmpdir
298
+ pool = DeadbeatPool.new("foo", @handler, 'size' => 1, 'pids_dir' => dir, 'sleep' => 0.1)
299
+ pool.send(:spawn_worker)
300
+ assert_equal 1, pool.workers.size
301
+
302
+ pid = pool.workers.first.pid
303
+
304
+ pool.stop
305
+
306
+ Process.kill(9, pid)
307
+ Process.waitpid(pid)
308
+
309
+ pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => dir, 'sleep' => 0.1)
310
+
311
+ assert_equal 1, pool.orphans.size
312
+
313
+ pool.send(:cleanup_orphaned_workers)
314
+
315
+ assert_equal 0, pool.orphans.size
316
+ end
317
+
318
+ should "remove any orphaned workers that throw Errno::EPERM (permission error)" do
319
+ dir = Dir.mktmpdir
320
+ pool = DeadbeatPool.new("foo", @handler, 'size' => 1, 'pids_dir' => dir, 'sleep' => 0.1)
321
+ pool.send(:spawn_worker)
322
+ assert_equal 1, pool.workers.size
323
+
324
+ pid = pool.workers.first.pid
325
+
326
+ pool.stop
327
+
328
+ Process.kill(9, pid)
329
+ Process.waitpid(pid)
330
+
331
+ pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => dir, 'sleep' => 0.1)
332
+
333
+ assert_equal 1, pool.orphans.size
334
+
335
+ Process.expects(:kill).once.with(0, pid).raises(Errno::EPERM)
336
+
337
+ pool.send(:cleanup_orphaned_workers)
338
+
339
+ assert_equal 0, pool.orphans.size
340
+ end
341
+ end
342
+
343
+ context "above_threshold?" do
344
+ should "return true if total_workers_count is above the threshold" do
345
+ pool = Pool.new("foo", @handler, 'size' => 0, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
346
+
347
+ pool.send(:spawn_worker)
348
+ assert_equal 1, pool.workers.size
349
+
350
+ # cleanup spawned worker
351
+ # in case assertion fails.
352
+ pid = pool.workers.first.pid
353
+ Process.kill(9, pid)
354
+ Process.waitpid(pid)
355
+
356
+ assert pool.above_threshold?
357
+ end
358
+
359
+ should "return false if total_workers_count is equal to the threshold" do
360
+ pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
361
+
362
+ pool.send(:spawn_worker)
363
+ assert_equal 1, pool.workers.size
364
+
365
+ # cleanup spawned worker
366
+ # in case assertion fails.
367
+ pid = pool.workers.first.pid
368
+ Process.kill(9, pid)
369
+ Process.waitpid(pid)
370
+
371
+ refute pool.above_threshold?
372
+ end
373
+
374
+ should "return false if total_workers_count is below the threshold" do
375
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
376
+
377
+ pool.send(:spawn_worker)
378
+ assert_equal 1, pool.workers.size
379
+
380
+ # cleanup spawned worker
381
+ # in case assertion fails.
382
+ pid = pool.workers.first.pid
383
+ Process.kill(9, pid)
384
+ Process.waitpid(pid)
385
+
386
+ refute pool.above_threshold?
387
+ end
388
+ end
389
+
390
+ context "at_threshold?" do
391
+ should "return false if total_workers_count is above the threshold" do
392
+ pool = Pool.new("foo", @handler, 'size' => 0, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
393
+
394
+ pool.send(:spawn_worker)
395
+ assert_equal 1, pool.workers.size
396
+
397
+ # cleanup spawned worker
398
+ # in case assertion fails.
399
+ pid = pool.workers.first.pid
400
+ Process.kill(9, pid)
401
+ Process.waitpid(pid)
402
+
403
+ refute pool.at_threshold?
404
+ end
405
+
406
+ should "return true if total_workers_count is equal to the threshold" do
407
+ pool = Pool.new("foo", @handler, 'size' => 1, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
408
+
409
+ pool.send(:spawn_worker)
410
+ assert_equal 1, pool.workers.size
411
+
412
+ # cleanup spawned worker
413
+ # in case assertion fails.
414
+ pid = pool.workers.first.pid
415
+ Process.kill(9, pid)
416
+ Process.waitpid(pid)
417
+
418
+ assert pool.at_threshold?
419
+ end
420
+
421
+ should "return false if total_workers_count is below the threshold" do
422
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
423
+
424
+ pool.send(:spawn_worker)
425
+ assert_equal 1, pool.workers.size
426
+
427
+ # cleanup spawned worker
428
+ # in case assertion fails.
429
+ pid = pool.workers.first.pid
430
+ Process.kill(9, pid)
431
+ Process.waitpid(pid)
432
+
433
+ refute pool.at_threshold?
434
+ end
435
+ end
436
+
437
+ context "has_workers?" do
438
+ should "return false if there are no workers" do
439
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
440
+ refute pool.has_workers?
441
+ end
442
+
443
+ should "return true if there are workers" do
444
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
445
+ refute pool.has_workers?
446
+
447
+ pool.send(:spawn_worker)
448
+
449
+ # cleanup spawned worker
450
+ # in case assertion fails.
451
+ pid = pool.workers.first.pid
452
+ Process.kill(9, pid)
453
+ Process.waitpid(pid)
454
+
455
+ assert pool.has_workers?
456
+ end
457
+ end
458
+
459
+ context "total_workers_count" do
460
+ should "return the sum of orphans.size + workers.size" do
461
+ pool = Pool.new("foo", @handler, 'size' => 2, 'pids_dir' => Dir.mktmpdir, 'sleep' => 0.1)
462
+ assert_equal 0, pool.total_workers_count
463
+
464
+ pool.orphans << Worker.new(1, pool)
465
+ pool.orphans << Worker.new(2, pool)
466
+ pool.orphans << Worker.new(3, pool)
467
+ pool.workers << Worker.new(4, pool)
468
+ pool.workers << Worker.new(5, pool)
469
+ pool.workers << Worker.new(6, pool)
470
+
471
+ assert_equal 6, pool.total_workers_count
472
+ end
473
+ end
233
474
  end