Skip to content

Commit

Permalink
Fix capsule test setup to make sure there is always a GoodJob.capsule…
Browse files Browse the repository at this point in the history
… present
  • Loading branch information
bensheldon committed Apr 4, 2023
1 parent 92c09be commit 76f1a61
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/good_job/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ def start
set_up_application!
GoodJob.configuration.options.merge!(options.symbolize_keys)
configuration = GoodJob.configuration
capsule = GoodJob.capsule

Daemon.new(pidfile: configuration.pidfile).daemonize if configuration.daemonize?

capsule = GoodJob::Capsule.new
capsule.start

if configuration.probe_port
Expand Down
2 changes: 1 addition & 1 deletion lib/good_job/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def initialize(performer, max_threads: nil, max_cache: nil, warm_cache_on_initia
@id = SecureRandom.uuid

source = ActiveSupport::BacktraceCleaner.new.clean(caller.lazy.reject { |line| line.include? File.expand_path(File.dirname(__FILE__)) }).first
puts "Created GoodJob::Scheduler `#{@id}`"
puts "\nCreated GoodJob::Scheduler `#{@id}`"
puts "↳ #{source}"

@performer = performer
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/good_job/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
before do
stub_const 'GoodJob::CLI::RAILS_ENVIRONMENT_RB', File.expand_path("spec/test_app/config/environment.rb")
allow(GoodJob).to receive(:configuration).and_return(GoodJob::Configuration.new({}))
allow(GoodJob::Capsule).to receive(:new).and_return capsule_mock
allow(GoodJob).to receive(:capsule).and_return capsule_mock
end

describe '#start' do
it 'initializes a capsule' do
it 'starts and stops a capsule' do
allow(Kernel).to receive(:loop)

cli = described_class.new([], {}, {})
cli.start

expect(GoodJob::Capsule).to have_received(:new)
expect(capsule_mock).to have_received(:start)
expect(capsule_mock).to have_received(:shutdown)
end

it 'can gracefully shut down on INT signal' do
Expand All @@ -30,7 +31,6 @@

sleep_until { cli_thread.fulfilled? }

expect(GoodJob::Capsule).to have_received(:new)
expect(capsule_mock).to have_received(:shutdown)
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 @@ -27,7 +27,7 @@

describe '.restart' do
it 'does nothing when there are no capsule instances' do
expect(GoodJob::Capsule.instances).to be_empty
GoodJob::Capsule.instances.clear
expect { described_class.restart }.not_to change(described_class, :shutdown?).from(true)
end

Expand Down
4 changes: 4 additions & 0 deletions spec/support/reset_good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
expect(GoodJob::Capsule.instances).to all be_shutdown
GoodJob::Capsule.instances.clear

# always make sure there is a capsule; unstub it first if necessary
RSpec::Mocks.space.proxy_for(GoodJob::Capsule).reset
GoodJob.capsule = GoodJob::Capsule.new

expect(PgLock.current_database.advisory_lock.owns.count).to eq(0), "Existing owned advisory locks AFTER test run"

other_locks = PgLock.current_database.advisory_lock.others
Expand Down

0 comments on commit 76f1a61

Please sign in to comment.