Skip to content

Commit 35b1c52

Browse files
committed
Clean up and add to thread pool specs
1 parent f6c9e70 commit 35b1c52

11 files changed

+148
-59
lines changed

spec/concurrent/executor/fixed_thread_pool_shared.rb

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'spec_helper'
22
require_relative 'thread_pool_shared'
3+
require 'thread'
34

45
share_examples_for :fixed_thread_pool do
56

@@ -13,13 +14,64 @@
1314

1415
it_should_behave_like :thread_pool
1516

16-
context '#initialize' do
17+
context '#initialize default values' do
18+
19+
subject { described_class.new(5) }
20+
21+
it 'defaults :min_length correctly' do
22+
subject.min_length.should eq 5
23+
end
24+
25+
it 'defaults :max_length correctly' do
26+
subject.max_length.should eq 5
27+
end
28+
29+
it 'defaults :overflow_policy to :abort' do
30+
subject.overflow_policy.should eq :abort
31+
end
32+
33+
34+
it 'defaults :idletime correctly' do
35+
subject.idletime.should eq subject.class.const_get(:DEFAULT_THREAD_IDLETIMEOUT)
36+
end
37+
38+
it 'defaults default :max_queue to zero' do
39+
subject.max_queue.should eq 0
40+
end
41+
42+
end
43+
44+
context '#initialize explicit values' do
1745

1846
it 'raises an exception when the pool length is less than one' do
1947
lambda {
2048
described_class.new(0)
2149
}.should raise_error(ArgumentError)
2250
end
51+
52+
53+
it 'sets explicit :max_queue correctly' do
54+
subject = described_class.new(5, :max_queue => 10)
55+
subject.max_queue.should eq 10
56+
end
57+
58+
it 'correctly sets valid :overflow_policy' do
59+
subject = described_class.new(5, :overflow_policy => :caller_runs)
60+
subject.overflow_policy.should eq :caller_runs
61+
end
62+
63+
it "correctly sets valid :idletime" do
64+
subject = described_class.new(5, :idletime => 10)
65+
subject.idletime.should eq 10
66+
end
67+
68+
it 'raises an exception if given an invalid :overflow_policy' do
69+
expect {
70+
described_class.new(5, overflow_policy: :bogus)
71+
}.to raise_error(ArgumentError)
72+
end
73+
74+
2375
end
2476

2577
context '#min_length' do
@@ -102,12 +154,6 @@
102154
end
103155
end
104156

105-
context '#idletime' do
106-
107-
it 'returns zero' do
108-
subject.idletime.should eq 0
109-
end
110-
end
111157

112158
context '#kill' do
113159

@@ -133,4 +179,54 @@
133179
pool.kill
134180
end
135181
end
182+
183+
context 'overflow policy' do
184+
185+
before do
186+
@queue = Queue.new
187+
end
188+
189+
# On abort, it should raise an error
190+
it "raises an error when overflow on abort" do
191+
subject = described_class.new(2, :max_queue => 2, :overflow_policy => :abort)
192+
expect {
193+
5.times do |i|
194+
subject.post { sleep 0.1; @queue << i}
195+
end
196+
subject.shutdown
197+
subject.wait_for_termination(1)
198+
}.to raise_error
199+
end
200+
201+
# On discard, we'd expect no error, but also not all five results
202+
it 'discards when overflow is :discard' do
203+
subject = described_class.new(2, :max_queue => 2, :overflow_policy => :discard)
204+
5.times do |i|
205+
subject.post { sleep 0.1; @queue << i}
206+
end
207+
subject.shutdown
208+
subject.wait_for_termination(1)
209+
@queue.length.should be < 5
210+
end
211+
212+
# To check for caller_runs, we'll check how many unique threads
213+
# actually ran the block
214+
215+
it 'uses the calling thread for overflow under caller_runs' do
216+
subject = described_class.new(2, :max_queue => 2, :overflow_policy => :caller_runs)
217+
5.times do |i|
218+
subject.post { sleep 0.1; @queue << Thread.current}
219+
end
220+
subject.shutdown
221+
subject.wait_for_termination(1)
222+
# Turn the queue into an array
223+
a = []
224+
a << @queue.shift until @queue.empty?
225+
226+
a.size.should eq 5 # one for each run of the block
227+
a.uniq.size.should eq 3 # one for each of teh two threads, plus the caller
228+
end
229+
end
230+
231+
136232
end

spec/concurrent/executor/java_cached_thread_pool_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module Concurrent
88

9-
describe JavaCachedThreadPool do
9+
describe JavaCachedThreadPool, :type=>:jruby do
1010

1111
subject { described_class.new(overflow_policy: :discard) }
1212

spec/concurrent/executor/java_fixed_thread_pool_spec.rb

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module Concurrent
88

9-
describe JavaFixedThreadPool do
9+
describe JavaFixedThreadPool, :type=>:jruby do
1010

1111
subject { described_class.new(5, overflow_policy: :discard) }
1212

@@ -19,45 +19,16 @@ module Concurrent
1919

2020
context '#initialize' do
2121

22-
it 'sets :min_length correctly' do
23-
subject = JavaFixedThreadPool.new(10)
24-
subject.min_length.should eq 10
25-
end
26-
27-
it 'sets :max_length correctly' do
28-
subject = JavaFixedThreadPool.new(5)
29-
subject.max_length.should eq 5
30-
end
31-
32-
it 'sets :idletime correctly' do
33-
subject = JavaFixedThreadPool.new(5)
34-
subject.idletime.should eq 0
35-
end
36-
37-
it 'sets :max_queue correctly' do
38-
subject = JavaFixedThreadPool.new(5)
39-
subject.max_queue.should eq 0
40-
end
4122

4223
it 'sets :overflow_policy correctly' do
43-
clazz = java.util.concurrent.ThreadPoolExecutor::DiscardPolicy
24+
clazz = java.util.concurrent.ThreadPoolExecutor::DiscardPolicy
4425
policy = clazz.new
4526
clazz.should_receive(:new).at_least(:once).with(any_args).and_return(policy)
4627

4728
subject = JavaFixedThreadPool.new(5, overflow_policy: :discard)
4829
subject.overflow_policy.should eq :discard
4930
end
5031

51-
it 'defaults :overflow_policy to :abort' do
52-
subject = JavaFixedThreadPool.new(5)
53-
subject.overflow_policy.should eq :abort
54-
end
55-
56-
it 'raises an exception if given an invalid :overflow_policy' do
57-
expect {
58-
JavaFixedThreadPool.new(5, overflow_policy: :bogus)
59-
}.to raise_error(ArgumentError)
60-
end
6132
end
6233
end
6334
end

spec/concurrent/executor/java_single_thread_executor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module Concurrent
88

9-
describe JavaSingleThreadExecutor do
9+
describe JavaSingleThreadExecutor, :type=>:jruby do
1010

1111
after(:each) do
1212
subject.kill

spec/concurrent/executor/java_thread_pool_executor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
module Concurrent
88

9-
describe JavaThreadPoolExecutor do
9+
describe JavaThreadPoolExecutor, :type => :jruby do
1010

1111
after(:each) do
1212
subject.kill

spec/concurrent/executor/ruby_cached_thread_pool_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Concurrent
55

6-
describe RubyCachedThreadPool do
6+
describe RubyCachedThreadPool, :type=>:mrirbx do
77

88
subject do
99
described_class.new(

spec/concurrent/executor/ruby_fixed_thread_pool_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Concurrent
55

6-
describe RubyFixedThreadPool do
6+
describe RubyFixedThreadPool, :type=> :mrirbx do
77

88
subject { described_class.new(5, overflow_policy: :discard) }
99

spec/concurrent/executor/ruby_single_thread_executor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module Concurrent
66

7-
describe RubySingleThreadExecutor do
7+
describe RubySingleThreadExecutor, :type=>:mrirbx do
88

99
after(:each) do
1010
subject.kill

spec/concurrent/executor/ruby_thread_pool_executor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Concurrent
55

6-
describe RubyThreadPoolExecutor do
6+
describe RubyThreadPoolExecutor, :type=>:mrirbx do
77

88
after(:each) do
99
subject.kill

spec/concurrent/executor/thread_pool_executor_shared.rb

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,65 @@
33

44
share_examples_for :thread_pool_executor do
55

6+
67
after(:each) do
78
subject.kill
89
sleep(0.1)
910
end
1011

11-
it_should_behave_like :thread_pool
12+
context '#initialize defaults' do
1213

13-
context '#initialize' do
14+
subject { described_class.new }
1415

1516
it 'defaults :min_length to DEFAULT_MIN_POOL_SIZE' do
16-
subject = described_class.new
1717
subject.min_length.should eq described_class::DEFAULT_MIN_POOL_SIZE
1818
end
1919

20+
2021
it 'defaults :max_length to DEFAULT_MAX_POOL_SIZE' do
21-
subject = described_class.new
2222
subject.max_length.should eq described_class::DEFAULT_MAX_POOL_SIZE
2323
end
2424

2525
it 'defaults :idletime to DEFAULT_THREAD_IDLETIMEOUT' do
26-
subject = described_class.new
2726
subject.idletime.should eq described_class::DEFAULT_THREAD_IDLETIMEOUT
2827
end
2928

3029
it 'defaults :max_queue to DEFAULT_MAX_QUEUE_SIZE' do
31-
subject = described_class.new
3230
subject.max_queue.should eq described_class::DEFAULT_MAX_QUEUE_SIZE
3331
end
3432

33+
it 'defaults :overflow_policy to :abort' do
34+
subject.overflow_policy.should eq :abort
35+
end
36+
end
37+
38+
context "#initialize explicit values" do
39+
40+
it "sets :min_threads" do
41+
described_class.new(min_threads: 2).min_length.should eq 2
42+
end
43+
44+
it "sets :max_threads" do
45+
described_class.new(max_threads: 2).max_length.should eq 2
46+
end
47+
48+
it "sets :idletime" do
49+
described_class.new(idletime: 2).idletime.should eq 2
50+
end
51+
52+
it "doesn't allow max_threads < min_threads" do
53+
expect {
54+
described_class.new(min_threads: 2, max_threads: 1)
55+
}.to raise_error(ArgumentError)
56+
end
57+
3558
it 'accepts all valid overflow policies' do
3659
Concurrent::RubyThreadPoolExecutor::OVERFLOW_POLICIES.each do |policy|
3760
subject = described_class.new(overflow_policy: policy)
3861
subject.overflow_policy.should eq policy
3962
end
4063
end
4164

42-
it 'defaults :overflow_policy to :abort' do
43-
subject = described_class.new
44-
subject.overflow_policy.should eq :abort
45-
end
4665

4766
it 'raises an exception if :min_threads is less than zero' do
4867
expect {

0 commit comments

Comments
 (0)