-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow schedulers to be restarted; separate unit tests from integratio…
…n tests
- Loading branch information
1 parent
c254589
commit 35bf224
Showing
14 changed files
with
239 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,28 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe GoodJob::Scheduler do | ||
before do | ||
ActiveJob::Base.queue_adapter = adapter | ||
let(:performer) { instance_double(GoodJob::Performer, next: nil) } | ||
|
||
stub_const "RUN_JOBS", Concurrent::Array.new | ||
stub_const "THREAD_JOBS", Concurrent::Hash.new(Concurrent::Array.new) | ||
|
||
stub_const 'ExampleJob', (Class.new(ApplicationJob) do | ||
self.queue_name = 'test' | ||
self.priority = 50 | ||
|
||
def perform(*_args, **_kwargs) | ||
thread_name = Thread.current.name || Thread.current.object_id | ||
|
||
RUN_JOBS << provider_job_id | ||
THREAD_JOBS[thread_name] << provider_job_id | ||
end | ||
end) | ||
|
||
stub_const 'RetryableError', Class.new(StandardError) | ||
stub_const 'ErrorJob', (Class.new(ApplicationJob) do | ||
self.queue_name = 'test' | ||
self.priority = 50 | ||
retry_on(RetryableError, wait: 0, attempts: 3) do |job, error| | ||
# puts "FAILED" | ||
end | ||
|
||
def perform(*args, **kwargs) | ||
thread_name = Thread.current.name || Thread.current.object_id | ||
|
||
RUN_JOBS << { args: args, kwargs: kwargs } | ||
THREAD_JOBS[thread_name] << provider_job_id | ||
|
||
raise RetryableError | ||
end | ||
end), transfer_nested_constants: true | ||
end | ||
|
||
let(:adapter) { GoodJob::Adapter.new } | ||
|
||
context 'when there are a large number of jobs' do | ||
let(:number_of_jobs) { 250 } | ||
|
||
let!(:good_jobs) do | ||
number_of_jobs.times do |i| | ||
ExampleJob.perform_later(i) | ||
end | ||
end | ||
|
||
it 'pops items off of the queue and runs them' do | ||
performer = GoodJob::Performer.new(GoodJob::Job.all, :perform_with_advisory_lock) | ||
describe '#shutdown' do | ||
it 'shuts down the theadpools' do | ||
scheduler = described_class.new(performer) | ||
|
||
sleep_until(max: 5, increments_of: 0.5) { GoodJob::Job.count == 0 } | ||
|
||
if RUN_JOBS.size != number_of_jobs | ||
jobs = THREAD_JOBS.values.flatten | ||
|
||
jobs_tally = jobs.each_with_object(Hash.new(0)) do |job_id, hash| | ||
hash[job_id] += 1 | ||
end | ||
|
||
rerun_jobs = jobs_tally.select { |_key, value| value > 1 } | ||
|
||
rerun_jobs.each do |job_id, tally| | ||
rerun_threads = THREAD_JOBS.select { |_thread, thread_jobs| thread_jobs.include? job_id }.keys | ||
|
||
puts "Ran job id #{job_id} for #{tally} times on threads #{rerun_threads}" | ||
end | ||
end | ||
|
||
scheduler.shutdown | ||
|
||
expect(GoodJob::Job.count).to eq(0), -> { "Unworked jobs are #{GoodJob::Job.all.map(&:id)}" } | ||
expect(rerun_jobs).to be_nil | ||
expect(RUN_JOBS.size).to eq number_of_jobs | ||
expect(scheduler.instance_variable_get(:@timer).running?).to be false | ||
expect(scheduler.instance_variable_get(:@pool).running?).to be false | ||
end | ||
end | ||
|
||
context 'when job has errors' do | ||
let!(:jobs) { ErrorJob.perform_later } | ||
|
||
it "handles and retries jobs with errors" do | ||
performer = GoodJob::Performer.new(GoodJob::Job.all, :perform_with_advisory_lock) | ||
describe '#restart' do | ||
it 'restarts the threadpools' do | ||
scheduler = described_class.new(performer) | ||
scheduler.shutdown | ||
|
||
sleep_until(max: 5, increments_of: 0.5) { GoodJob::Job.count == 0 } | ||
scheduler.restart | ||
|
||
scheduler.shutdown | ||
expect(scheduler.instance_variable_get(:@timer).running?).to be true | ||
expect(scheduler.instance_variable_get(:@pool).running?).to be true | ||
end | ||
end | ||
end |
Oops, something went wrong.