Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite flaky tests: don't allow nil timeout for restart #872

Merged
merged 2 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def self.shutdown?
# When forking processes you should shut down these background threads before forking, and restart them after forking.
# For example, you should use +shutdown+ and +restart+ when using async execution mode with Puma.
# See the {file:README.md#executing-jobs-async--in-process} for more explanation and examples.
# @param timeout [Numeric, nil] Seconds to wait for active threads to finish.
# @param timeout [Numeric] Seconds to wait for active threads to finish.
# @return [void]
def self.restart(timeout: -1)
_shutdown_all(Capsule.instances, :restart, timeout: timeout)
Expand Down
4 changes: 3 additions & 1 deletion lib/good_job/capsule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ def shutdown(timeout: :default)
end

# Shutdown and then start the capsule again.
# @param timeout [nil, Numeric, Symbol] Seconds to wait for active threads.
# @param timeout [Numeric, Symbol] Seconds to wait for active threads.
# @return [void]
def restart(timeout: :default)
raise ArgumentError, "Capsule#restart cannot be called with a timeout of nil" if timeout.nil?

shutdown(timeout: timeout)
start
end
Expand Down
6 changes: 4 additions & 2 deletions lib/good_job/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ def shutdown(timeout: -1)

# Restart the Scheduler.
# When shutdown, start; or shutdown and start.
# @param timeout [nil, Numeric] Seconds to wait for actively executing jobs to finish; shares same values as {#shutdown}.
# @param timeout [Numeric] Seconds to wait for actively executing jobs to finish; shares same values as {#shutdown}.
# @return [void]
def restart(timeout: -1)
raise ArgumentError, "Scheduler#restart cannot be called with a timeout of nil" if timeout.nil?

instrument("scheduler_restart_pools") do
shutdown(timeout: timeout) if running?
shutdown(timeout: timeout)
create_executor
warm_cache
end
Expand Down
19 changes: 14 additions & 5 deletions spec/lib/good_job/scheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@
expect { scheduler.restart }
.to change(scheduler, :running?).from(false).to(true)
end

it 'can be called multiple times' do
scheduler = described_class.new(performer)
scheduler.shutdown
expect do
scheduler.restart
scheduler.restart
scheduler.restart
end.not_to raise_error
scheduler.shutdown
end
end

describe '#create_thread' do
Expand Down Expand Up @@ -144,13 +155,11 @@
it 'uses state[:count] to create multiple threads' do
job_performer = instance_double(GoodJob::JobPerformer, next: nil, next?: true, name: '', next_at: [], cleanup: nil)
scheduler = described_class.new(job_performer, max_threads: 1)
allow(scheduler).to receive(:create_task)

result = scheduler.create_thread({ count: 10 })
if result
expect(job_performer).to have_received(:next).exactly(10).times
else
expect(job_performer).to have_received(:next).at_most(9).times
end
expect(result).to be true
expect(scheduler).to have_received(:create_task).exactly(10).times
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/good_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

describe '.shutdown?' do
it 'shuts down all scheduler and notifier instances' do
it 'returns whether any capsules are running' do
expect do
capsule = GoodJob::Capsule.new(configuration: configuration)
capsule.start
Expand Down