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`
  • Loading branch information
bensheldon committed Mar 1, 2024
1 parent dadc2ad commit f256ae1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def shutdown
def kill
synchronize do
@executor.shutdownNow
ok = @executor.awaitTermination(60, java.util.concurrent.TimeUnit::SECONDS) until ok
nil
end
end
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: 54 additions & 0 deletions spec/concurrent/executor/executor_service_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,60 @@
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 subject.is_a?(ImmediateExecutor)

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.wait_for_termination(pool_termination_timeout)).to eq true
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 subject.is_a?(ImmediateExecutor)

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 false when the thread pool is killed' do
subject.kill
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject).to be_shutdown
end
end

context '#shutdown' do

it 'stops accepting new tasks' do
Expand Down

0 comments on commit f256ae1

Please sign in to comment.