Skip to content

Commit cf2ca8c

Browse files
committed
Refactored more difficult thread pool tests.
1 parent 2a62578 commit cf2ca8c

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

spec/concurrent/executor/ruby_cached_thread_pool_spec.rb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,38 @@ module Concurrent
2121

2222
context 'garbage collection' do
2323

24-
subject{ described_class.new(idletime: 1, max_threads: 5, gc_interval: 0) }
24+
subject{ described_class.new(idletime: 1, max_threads: 2, gc_interval: 0) }
2525

2626
it 'removes from pool any thread that has been idle too long' do
2727
subject.instance_variable_set(:@idletime, 1)
28-
3.times { subject << proc{ sleep(0.1) } }
29-
sleep(0.1)
30-
expect(subject.length).to eq 3
28+
latch = Concurrent::CountDownLatch.new(3)
29+
3.times { subject << proc{ sleep(0.1); latch.count_down } }
30+
expect(latch.wait(1)).to be true
31+
32+
max_threads = subject.length
3133
sleep(2)
32-
subject << proc{ nil }
33-
sleep(0.1)
34-
expect(subject.length).to be < 3
34+
35+
latch = Concurrent::CountDownLatch.new(1)
36+
subject << proc{ latch.count_down }
37+
expect(latch.wait(1)).to be true
38+
39+
expect(subject.length).to be < max_threads
3540
end
3641

3742
it 'removes from pool any dead thread' do
38-
3.times { subject << proc{ sleep(0.1); raise Exception } }
39-
sleep(0.1)
40-
expect(subject.length).to eq 3
43+
subject.instance_variable_set(:@idletime, 1)
44+
latch = Concurrent::CountDownLatch.new(3)
45+
3.times { subject << proc{ sleep(0.1); latch.count_down; raise Exception } }
46+
expect(latch.wait(1)).to be true
47+
48+
max_threads = subject.length
4149
sleep(2)
42-
subject << proc{ nil }
43-
sleep(0.1)
44-
expect(subject.length).to be < 3
50+
51+
latch = Concurrent::CountDownLatch.new(1)
52+
subject << proc{ latch.count_down }
53+
expect(latch.wait(1)).to be true
54+
55+
expect(subject.length).to be < max_threads
4556
end
4657
end
4758

0 commit comments

Comments
 (0)