mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
correctly set test adapter when configure the queue adapter on a per job (#26690)
The `ActiveJob::TestHelper` replace the adapter to test adapter in `before_setup`. It gets the target class using the `descendants`, but if the test target job class is not loaded, will not be a replacement of the adapter. Therefore, instead of replacing with `before_setup`, modified to replace when setting adapter. Fixes #26360
This commit is contained in:
parent
a57b5292b0
commit
80dc309821
5 changed files with 62 additions and 10 deletions
|
@ -1,3 +1,9 @@
|
|||
* Correctly set test adapter when configure the queue adapter on a per job.
|
||||
|
||||
Fixes #26360.
|
||||
|
||||
*Yuji Yaginuma*
|
||||
|
||||
* Push skipped jobs to `enqueued_jobs` when using `perform_enqueued_jobs` with a `only` filter in tests
|
||||
|
||||
*Alexander Pauly*
|
||||
|
|
|
@ -8,16 +8,35 @@ module ActiveJob
|
|||
:performed_jobs, :performed_jobs=,
|
||||
to: :queue_adapter
|
||||
|
||||
module TestQueueAdapter
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
class_attribute :_test_adapter, instance_accessor: false, instance_predicate: false
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def queue_adapter
|
||||
self._test_adapter.nil? ? super : self._test_adapter
|
||||
end
|
||||
|
||||
def disable_test_adapter
|
||||
self._test_adapter = nil
|
||||
end
|
||||
|
||||
def enable_test_adapter(test_adapter)
|
||||
self._test_adapter = test_adapter
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ActiveJob::Base.include(TestQueueAdapter)
|
||||
|
||||
def before_setup # :nodoc:
|
||||
test_adapter = queue_adapter_for_test
|
||||
|
||||
@old_queue_adapters = (ActiveJob::Base.descendants << ActiveJob::Base).select do |klass|
|
||||
# only override explicitly set adapters, a quirk of `class_attribute`
|
||||
klass.singleton_class.public_instance_methods(false).include?(:_queue_adapter)
|
||||
end.map do |klass|
|
||||
[klass, klass.queue_adapter].tap do
|
||||
klass.queue_adapter = test_adapter
|
||||
end
|
||||
queue_adapter_changed_jobs.each do |klass|
|
||||
klass.enable_test_adapter(test_adapter)
|
||||
end
|
||||
|
||||
clear_enqueued_jobs
|
||||
|
@ -27,9 +46,8 @@ module ActiveJob
|
|||
|
||||
def after_teardown # :nodoc:
|
||||
super
|
||||
@old_queue_adapters.each do |(klass, adapter)|
|
||||
klass.queue_adapter = adapter
|
||||
end
|
||||
|
||||
queue_adapter_changed_jobs.each { |klass| klass.disable_test_adapter }
|
||||
end
|
||||
|
||||
# Specifies the queue adapter to use with all active job test helpers.
|
||||
|
@ -358,5 +376,12 @@ module ActiveJob
|
|||
job.queue_name = payload[:queue]
|
||||
job
|
||||
end
|
||||
|
||||
def queue_adapter_changed_jobs
|
||||
(ActiveJob::Base.descendants << ActiveJob::Base).select do |klass|
|
||||
# only override explicitly set adapters, a quirk of `class_attribute`
|
||||
klass.singleton_class.public_instance_methods(false).include?(:_queue_adapter)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,6 +21,7 @@ class QueueAdapterTest < ActiveJob::TestCase
|
|||
end
|
||||
|
||||
test "should allow overriding the queue_adapter at the child class level without affecting the parent or its sibling" do
|
||||
ActiveJob::Base.disable_test_adapter
|
||||
base_queue_adapter = ActiveJob::Base.queue_adapter
|
||||
|
||||
child_job_one = Class.new(ActiveJob::Base)
|
||||
|
|
|
@ -560,3 +560,20 @@ class InheritedJobTest < ActiveJob::TestCase
|
|||
assert_instance_of ActiveJob::QueueAdapters::TestAdapter, InheritedJob.queue_adapter
|
||||
end
|
||||
end
|
||||
|
||||
class QueueAdapterJobTest < ActiveJob::TestCase
|
||||
def before_setup
|
||||
@original_autoload_paths = ActiveSupport::Dependencies.autoload_paths
|
||||
ActiveSupport::Dependencies.autoload_paths = %w(test/jobs)
|
||||
super
|
||||
end
|
||||
|
||||
def after_teardown
|
||||
ActiveSupport::Dependencies.autoload_paths = @original_autoload_paths
|
||||
super
|
||||
end
|
||||
|
||||
def test_queue_adapter_is_test_adapter
|
||||
assert_instance_of ActiveJob::QueueAdapters::TestAdapter, QueueAdapterJob.queue_adapter
|
||||
end
|
||||
end
|
||||
|
|
3
activejob/test/jobs/queue_adapter_job.rb
Normal file
3
activejob/test/jobs/queue_adapter_job.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class QueueAdapterJob < ActiveJob::Base
|
||||
self.queue_adapter = :inline
|
||||
end
|
Loading…
Reference in a new issue