Skip to content

Commit

Permalink
Align Java Executor Service behavior for shuttingdown? shutdown?
Browse files Browse the repository at this point in the history
…and `kill`

Co-authored-by: Benoit Daloze <eregontp@gmail.com>
  • Loading branch information
bensheldon and eregon committed Mar 1, 2024
1 parent dadc2ad commit 93195e0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
11 changes: 4 additions & 7 deletions lib/concurrent-ruby/concurrent/executor/java_executor_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def shutdown
def kill
synchronize do
@executor.shutdownNow
nil
end
wait_for_termination
nil
end

private
Expand All @@ -57,15 +58,11 @@ def ns_running?
end

def ns_shuttingdown?
if @executor.respond_to? :isTerminating
@executor.isTerminating
else
false
end
@executor.isShutdown && !@executor.isTerminated
end

def ns_shutdown?
@executor.isShutdown || @executor.isTerminated
@executor.isTerminated
end

class Job
Expand Down
54 changes: 53 additions & 1 deletion spec/concurrent/executor/executor_service_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'concurrent/atomic/atomic_fixnum'
require 'timeout'

RSpec.shared_examples :executor_service do
RSpec.shared_examples :executor_service do |immediate_type: false|

after(:each) do
subject.shutdown
Expand Down Expand Up @@ -84,6 +84,58 @@
end
end

context '#shuttingdown?' do
it 'returns false when the thread pool is running' do
expect(subject).not_to be_shuttingdown
end

it 'returns true when the thread pool is shutting down' do
skip "will never be in shuttingdown? state" if immediate_type

subject.post{ sleep(0.5) }
subject.shutdown
expect(subject).to be_shuttingdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
end

it 'returns false when the thread pool is shutdown' do
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject).not_to be_shuttingdown
end

it 'returns false when the thread pool is killed' do
subject.kill
expect(subject).not_to be_shuttingdown
end
end

context '#shutdown?' do
it 'returns false when the thread pool is running' do
expect(subject).not_to be_shutdown
end

it 'returns false when the thread pool is shutting down' do
skip "will never be in shuttingdown? state" if immediate_type

subject.post{ sleep(0.5) }
subject.shutdown
expect(subject).not_to be_shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
end

it 'returns true when the thread pool is shutdown' do
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject).to be_shutdown
end

it 'returns true when the thread pool is killed' do
subject.kill
expect(subject).to be_shutdown
end
end

context '#shutdown' do

it 'stops accepting new tasks' do
Expand Down
2 changes: 1 addition & 1 deletion spec/concurrent/executor/immediate_executor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ module Concurrent

subject { ImmediateExecutor.new }

it_should_behave_like :executor_service
it_should_behave_like :executor_service, immediate_type: true
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Concurrent

subject { IndirectImmediateExecutor.new }

it_should_behave_like :executor_service
it_should_behave_like :executor_service, immediate_type: true

it "runs its tasks synchronously" do
start = Time.now
Expand Down
2 changes: 1 addition & 1 deletion spec/concurrent/executor/serialized_execution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ module Concurrent

subject { SerializedExecutionDelegator.new(ImmediateExecutor.new) }

it_should_behave_like :executor_service
it_should_behave_like :executor_service, immediate_type: true
end
end

0 comments on commit 93195e0

Please sign in to comment.