Skip to content

Commit d222830

Browse files
committed
Moved thread pool tests from shared to ruby-specific.
The specs for the Java-specific thread pools are mainly intended to 1) ensure consistent APIs and 2) verify that the Ruby thread pools have matching behavior. Some of the more complex behaviors (such as thread creation and garbage collection) are difficult to test for the Java thread pools. In these cases we'll just trust that Java's thread pools behave as documented and just test the Ruby versions.
1 parent 099d9ec commit d222830

File tree

4 files changed

+68
-60
lines changed

4 files changed

+68
-60
lines changed

spec/concurrent/cached_thread_pool_shared.rb

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -104,51 +104,10 @@
104104
end
105105
end
106106

107-
context 'garbage collection' do
108-
109-
subject{ described_class.new(idletime: 1, max_threads: 5, gc_interval: 0) }
110-
111-
it 'removes from pool any thread that has been idle too long' do
112-
3.times { subject << proc{ sleep(0.1) } }
113-
sleep(0.1)
114-
subject.length.should eq 3
115-
sleep(2)
116-
subject << proc{ nil }
117-
sleep(0.1)
118-
subject.length.should < 3
119-
end
120-
121-
it 'removes from pool any dead thread' do
122-
3.times { subject << proc{ sleep(0.1); raise Exception } }
123-
sleep(0.1)
124-
subject.length.should eq 3
125-
sleep(2)
126-
subject << proc{ nil }
127-
sleep(0.1)
128-
subject.length.should < 3
129-
end
130-
end
131-
132107
context 'worker creation and caching' do
133108

134109
subject{ described_class.new(idletime: 1, max_threads: 5) }
135110

136-
it 'creates new workers when there are none available' do
137-
subject.length.should eq 0
138-
5.times{ sleep(0.1); subject << proc{ sleep(1) } }
139-
sleep(1)
140-
subject.length.should eq 5
141-
end
142-
143-
it 'uses existing idle threads' do
144-
5.times{ subject << proc{ sleep(0.1) } }
145-
sleep(1)
146-
subject.length.should >= 5
147-
3.times{ subject << proc{ sleep(1) } }
148-
sleep(0.1)
149-
subject.length.should >= 5
150-
end
151-
152111
it 'never creates more than :max_threads threads' do
153112
pool = described_class.new(max_threads: 5)
154113
100.times{ pool << proc{ sleep(1) } }

spec/concurrent/fixed_thread_pool_shared.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,8 @@
115115
end
116116
end
117117

118-
context 'exception handling' do
119-
120-
it 'restarts threads that experience exception' do
121-
count = subject.length
122-
count.times{ subject << proc{ raise StandardError } }
123-
sleep(1)
124-
subject.length.should eq count
125-
end
126-
end
127-
128118
context 'worker creation and caching' do
129119

130-
it 'creates new workers when there are none available' do
131-
pool = described_class.new(5)
132-
pool.current_length.should eq 0
133-
5.times{ pool << proc{ sleep(1) } }
134-
sleep(0.1)
135-
pool.current_length.should eq 5
136-
pool.kill
137-
end
138-
139120
it 'never creates more than :num_threads threads' do
140121
pool = described_class.new(5)
141122
100.times{ pool << proc{ sleep(1) } }

spec/concurrent/ruby_cached_thread_pool_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,51 @@ module Concurrent
1313
end
1414

1515
it_should_behave_like :cached_thread_pool
16+
17+
context 'garbage collection' do
18+
19+
subject{ described_class.new(idletime: 1, max_threads: 5, gc_interval: 0) }
20+
21+
it 'removes from pool any thread that has been idle too long' do
22+
3.times { subject << proc{ sleep(0.1) } }
23+
sleep(0.1)
24+
subject.length.should eq 3
25+
sleep(2)
26+
subject << proc{ nil }
27+
sleep(0.1)
28+
subject.length.should < 3
29+
end
30+
31+
it 'removes from pool any dead thread' do
32+
3.times { subject << proc{ sleep(0.1); raise Exception } }
33+
sleep(0.1)
34+
subject.length.should eq 3
35+
sleep(2)
36+
subject << proc{ nil }
37+
sleep(0.1)
38+
subject.length.should < 3
39+
end
40+
end
41+
42+
context 'worker creation and caching' do
43+
44+
subject{ described_class.new(idletime: 1, max_threads: 5) }
45+
46+
it 'creates new workers when there are none available' do
47+
subject.length.should eq 0
48+
5.times{ sleep(0.1); subject << proc{ sleep(1) } }
49+
sleep(1)
50+
subject.length.should eq 5
51+
end
52+
53+
it 'uses existing idle threads' do
54+
5.times{ subject << proc{ sleep(0.1) } }
55+
sleep(1)
56+
subject.length.should >= 5
57+
3.times{ subject << proc{ sleep(1) } }
58+
sleep(0.1)
59+
subject.length.should >= 5
60+
end
61+
end
1662
end
1763
end

spec/concurrent/ruby_fixed_thread_pool_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,27 @@ module Concurrent
1313
end
1414

1515
it_should_behave_like :fixed_thread_pool
16+
17+
context 'exception handling' do
18+
19+
it 'restarts threads that experience exception' do
20+
count = subject.length
21+
count.times{ subject << proc{ raise StandardError } }
22+
sleep(1)
23+
subject.length.should eq count
24+
end
25+
end
26+
27+
context 'worker creation and caching' do
28+
29+
it 'creates new workers when there are none available' do
30+
pool = described_class.new(5)
31+
pool.current_length.should eq 0
32+
5.times{ pool << proc{ sleep(1) } }
33+
sleep(0.1)
34+
pool.current_length.should eq 5
35+
pool.kill
36+
end
37+
end
1638
end
1739
end

0 commit comments

Comments
 (0)