Skip to content

Commit 6bc3163

Browse files
committed
Better global thread pool configuration during tests.
1 parent 53b5607 commit 6bc3163

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

lib/concurrent/configuration.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,30 @@ class Configuration
1515
attr_accessor :global_operation_pool
1616

1717
def initialize
18-
task_pool_config = {
18+
end
19+
20+
def cores
21+
@cores ||= Concurrent::processor_count
22+
end
23+
24+
def global_task_pool
25+
@global_task_pool ||= Concurrent::ThreadPoolExecutor.new(
1926
min_threads: [2, cores].max,
2027
max_threads: [20, cores * 15].max,
2128
idletime: 2 * 60, # 2 minutes
2229
max_queue: 0, # unlimited
2330
overflow_policy: :abort # raise an exception
24-
}
31+
)
32+
end
2533

26-
operation_pool_config = {
34+
def global_operation_pool
35+
@global_operation_pool = Concurrent::ThreadPoolExecutor.new(
2736
min_threads: [2, cores].max,
2837
max_threads: [2, cores].max,
2938
idletime: 10 * 60, # 10 minutes
3039
max_queue: [20, cores * 15].max,
3140
overflow_policy: :abort # raise an exception
32-
}
33-
34-
@global_task_pool = Concurrent::ThreadPoolExecutor.new(task_pool_config)
35-
@global_operation_pool = Concurrent::ThreadPoolExecutor.new(operation_pool_config)
36-
end
37-
38-
def cores
39-
@cores ||= Concurrent::processor_count
41+
)
4042
end
4143

4244
def global_task_pool=(executor)

spec/concurrent/configuration_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ module Concurrent
2222

2323
describe Configuration do
2424

25+
context 'configure' do
26+
27+
it 'raises an exception if called twice'
28+
29+
it 'raises an exception if called after tasks post to the thread pool'
30+
31+
it 'raises an exception if called after operations post to the thread pool'
32+
33+
it 'allows reconfiguration if set to :test mode'
34+
end
35+
2536
context '#global_task_pool' do
2637
pending
2738
end

spec/concurrent/dataflow_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ module Concurrent
66

77
let(:executor) { ImmediateExecutor.new }
88

9+
before(:each) do
10+
Concurrent.configure do |config|
11+
config.global_operation_pool = Concurrent::ImmediateExecutor.new
12+
end
13+
end
14+
915
it 'raises an exception when no block given' do
1016
expect { Concurrent::dataflow }.to raise_error(ArgumentError)
1117
end

spec/spec_helper.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@
2727
end
2828

2929
config.before(:each) do
30-
Concurrent.configure do |config|
31-
config.global_task_pool = Concurrent::FixedThreadPool.new(5)
32-
config.global_operation_pool = Concurrent::FixedThreadPool.new(5)
33-
end
3430
end
3531

3632
config.after(:each) do
3733
Thread.list.each do |thread|
3834
thread.kill unless thread == Thread.current
3935
end
36+
37+
Concurrent.configure do |config|
38+
config.global_task_pool = nil
39+
config.global_operation_pool = nil
40+
end
4041
end
4142

4243
config.after(:suite) do

0 commit comments

Comments
 (0)