2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-01-15 12:21:04 +00:00
|
|
|
module ZammadActiveJobHelper
|
|
|
|
delegate :enqueued_jobs, :performed_jobs, to: :queue_adapter
|
|
|
|
|
|
|
|
def queue_adapter
|
|
|
|
::ActiveJob::Base.queue_adapter
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_jobs
|
|
|
|
enqueued_jobs.clear
|
|
|
|
performed_jobs.clear
|
2019-11-13 07:03:47 +00:00
|
|
|
ActiveJobLock.destroy_all
|
2019-01-15 12:21:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-16 14:17:20 +00:00
|
|
|
module ZammadActiveJobSystemHelper
|
|
|
|
include ActiveJob::TestHelper
|
|
|
|
|
|
|
|
alias original_perform_enqueued_jobs perform_enqueued_jobs
|
|
|
|
|
|
|
|
def perform_enqueued_jobs(**kwargs, &block)
|
|
|
|
ActiveJobLock.destroy_all
|
|
|
|
original_perform_enqueued_jobs(**kwargs, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-15 12:21:04 +00:00
|
|
|
RSpec.configure do |config|
|
|
|
|
|
|
|
|
activate_for = {
|
|
|
|
type: :job, # actual Job examples
|
|
|
|
performs_jobs: true, # examples performing Jobs
|
|
|
|
}
|
|
|
|
|
2021-08-16 14:17:20 +00:00
|
|
|
config.include ZammadActiveJobSystemHelper, performs_jobs: true, type: :system
|
|
|
|
|
2019-01-15 12:21:04 +00:00
|
|
|
activate_for.each do |key, value|
|
|
|
|
config.include ZammadActiveJobHelper, key => value
|
|
|
|
config.include RSpec::Rails::JobExampleGroup, key => value
|
|
|
|
|
|
|
|
config.around(:each, key => value) do |example|
|
|
|
|
|
|
|
|
default_queue_adapter = ::ActiveJob::Base.queue_adapter
|
|
|
|
::ActiveJob::Base.queue_adapter = :test
|
|
|
|
|
|
|
|
clear_jobs
|
|
|
|
|
|
|
|
example.run
|
|
|
|
|
2019-12-04 14:29:43 +00:00
|
|
|
ensure
|
2019-01-15 12:21:04 +00:00
|
|
|
::ActiveJob::Base.queue_adapter = default_queue_adapter
|
|
|
|
end
|
|
|
|
end
|
2021-06-23 11:35:27 +00:00
|
|
|
|
|
|
|
# Workaround needed for behavior change introduced in Rails >= 5.2
|
|
|
|
# see: https://github.com/rails/rails/issues/37270
|
2021-07-19 12:24:09 +00:00
|
|
|
config.before do
|
2021-06-23 11:35:27 +00:00
|
|
|
(ActiveJob::Base.descendants << ActiveJob::Base).each(&:disable_test_adapter)
|
|
|
|
end
|
2019-01-15 12:21:04 +00:00
|
|
|
end
|