From 8b9b0da4a37585ce5eb71516aca55e93bde39115 Mon Sep 17 00:00:00 2001 From: "Ben Sheldon [he/him]" Date: Thu, 29 Feb 2024 21:15:25 -0800 Subject: [PATCH] Align Java Executor Service behavior for `shuttingdown?`, `shutdown?` Co-authored-by: Benoit Daloze --- .../executor/java_executor_service.rb | 8 +--- .../executor/executor_service_shared.rb | 44 ++++++++++++++++++- .../executor/immediate_executor_spec.rb | 2 +- .../indirect_immediate_executor_spec.rb | 2 +- .../executor/serialized_execution_spec.rb | 2 +- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/concurrent-ruby/concurrent/executor/java_executor_service.rb b/lib/concurrent-ruby/concurrent/executor/java_executor_service.rb index 7c9ab178e..b2bc69a6e 100644 --- a/lib/concurrent-ruby/concurrent/executor/java_executor_service.rb +++ b/lib/concurrent-ruby/concurrent/executor/java_executor_service.rb @@ -57,15 +57,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 diff --git a/spec/concurrent/executor/executor_service_shared.rb b/spec/concurrent/executor/executor_service_shared.rb index e5ffa367c..7b87922d5 100644 --- a/spec/concurrent/executor/executor_service_shared.rb +++ b/spec/concurrent/executor/executor_service_shared.rb @@ -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 @@ -84,6 +84,48 @@ 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 + 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 + end + context '#shutdown' do it 'stops accepting new tasks' do diff --git a/spec/concurrent/executor/immediate_executor_spec.rb b/spec/concurrent/executor/immediate_executor_spec.rb index c53efd72e..53f2748ab 100644 --- a/spec/concurrent/executor/immediate_executor_spec.rb +++ b/spec/concurrent/executor/immediate_executor_spec.rb @@ -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 diff --git a/spec/concurrent/executor/indirect_immediate_executor_spec.rb b/spec/concurrent/executor/indirect_immediate_executor_spec.rb index f40364f28..c3c043d73 100644 --- a/spec/concurrent/executor/indirect_immediate_executor_spec.rb +++ b/spec/concurrent/executor/indirect_immediate_executor_spec.rb @@ -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 diff --git a/spec/concurrent/executor/serialized_execution_spec.rb b/spec/concurrent/executor/serialized_execution_spec.rb index 5f0cb4172..11b9bc72c 100644 --- a/spec/concurrent/executor/serialized_execution_spec.rb +++ b/spec/concurrent/executor/serialized_execution_spec.rb @@ -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