Skip to content

Commit a7d609b

Browse files
committed
Align Java Executor Service behavior for shuttingdown? shutdown? and kill
1 parent dadc2ad commit a7d609b

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

lib/concurrent-ruby/concurrent/executor/java_executor_service.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def shutdown
4646
def kill
4747
synchronize do
4848
@executor.shutdownNow
49+
ok = @executor.awaitTermination(60, java.util.concurrent.TimeUnit::SECONDS) until ok
4950
nil
5051
end
5152
end
@@ -57,15 +58,11 @@ def ns_running?
5758
end
5859

5960
def ns_shuttingdown?
60-
if @executor.respond_to? :isTerminating
61-
@executor.isTerminating
62-
else
63-
false
64-
end
61+
@executor.isShutdown && !@executor.isTerminated
6562
end
6663

6764
def ns_shutdown?
68-
@executor.isShutdown || @executor.isTerminated
65+
@executor.isTerminated
6966
end
7067

7168
class Job

spec/concurrent/executor/executor_service_shared.rb

+55-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'concurrent/atomic/atomic_fixnum'
44
require 'timeout'
55

6-
RSpec.shared_examples :executor_service do
6+
RSpec.shared_examples :executor_service do |immediate_type: false|
77

88
after(:each) do
99
subject.shutdown
@@ -84,6 +84,60 @@
8484
end
8585
end
8686

87+
context '#shuttingdown?' do
88+
it 'returns false when the thread pool is running' do
89+
expect(subject).not_to be_shuttingdown
90+
end
91+
92+
it 'returns true when the thread pool is shutting down' do
93+
skip "will never be in shuttingdown? state" if immediate_type
94+
95+
subject.post{ sleep(0.5) }
96+
subject.shutdown
97+
expect(subject).to be_shuttingdown
98+
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
99+
end
100+
101+
it 'returns false when the thread pool is shutdown' do
102+
subject.shutdown
103+
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
104+
expect(subject).not_to be_shuttingdown
105+
end
106+
107+
it 'returns false when the thread pool is killed' do
108+
subject.kill
109+
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
110+
expect(subject).not_to be_shuttingdown
111+
end
112+
end
113+
114+
context '#shutdown?' do
115+
it 'returns false when the thread pool is running' do
116+
expect(subject).not_to be_shutdown
117+
end
118+
119+
it 'returns false when the thread pool is shutting down' do
120+
skip "will never be in shuttingdown? state" if immediate_type
121+
122+
subject.post{ sleep(0.5) }
123+
subject.shutdown
124+
expect(subject).not_to be_shutdown
125+
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
126+
end
127+
128+
it 'returns true when the thread pool is shutdown' do
129+
subject.shutdown
130+
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
131+
expect(subject).to be_shutdown
132+
end
133+
134+
it 'returns false when the thread pool is killed' do
135+
subject.kill
136+
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
137+
expect(subject).to be_shutdown
138+
end
139+
end
140+
87141
context '#shutdown' do
88142

89143
it 'stops accepting new tasks' do

spec/concurrent/executor/immediate_executor_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ module Concurrent
77

88
subject { ImmediateExecutor.new }
99

10-
it_should_behave_like :executor_service
10+
it_should_behave_like :executor_service, immediate_type: true
1111
end
1212
end

spec/concurrent/executor/indirect_immediate_executor_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Concurrent
77

88
subject { IndirectImmediateExecutor.new }
99

10-
it_should_behave_like :executor_service
10+
it_should_behave_like :executor_service, immediate_type: true
1111

1212
it "runs its tasks synchronously" do
1313
start = Time.now

spec/concurrent/executor/serialized_execution_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ module Concurrent
88

99
subject { SerializedExecutionDelegator.new(ImmediateExecutor.new) }
1010

11-
it_should_behave_like :executor_service
11+
it_should_behave_like :executor_service, immediate_type: true
1212
end
1313
end

0 commit comments

Comments
 (0)