@@ -21,27 +21,38 @@ module Concurrent
21
21
22
22
context 'garbage collection' do
23
23
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 ) }
25
25
26
26
it 'removes from pool any thread that has been idle too long' do
27
27
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
31
33
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
35
40
end
36
41
37
42
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
41
49
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
45
56
end
46
57
end
47
58
0 commit comments