Skip to content

Commit

Permalink
Make perform_enqueued_jobs compatible with all Active Job adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiculescu committed Jun 28, 2023
1 parent 48e17e5 commit ba70a5b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
14 changes: 14 additions & 0 deletions actionmailer/test/test_helper_test.rb
Expand Up @@ -623,3 +623,17 @@ def test_setup_shouldnt_conflict_with_mailer_setup
assert_equal "a value", @test_var
end
end

class AdapterIsNotTestAdapterTest < ActionMailer::TestCase
def queue_adapter_for_test
ActiveJob::QueueAdapters::InlineAdapter.new
end

def test_can_send_email_using_any_active_job_adapter
assert_nothing_raised do
assert_emails 1 do
TestHelperMailer.test.deliver_now
end
end
end
end
7 changes: 7 additions & 0 deletions activejob/CHANGELOG.md
@@ -1,3 +1,10 @@
* `perform_enqueued_jobs` is now compatible with all Active Job adapters

This means that methods that depend on it, like Action Mailer's `assert_emails`,
will work correctly even if the test adapter is not used.

*Alex Ghiculescu*

* Allow queue adapters to provide a custom name by implementing `queue_adapter_name`

*Sander Verdonschot*
Expand Down
13 changes: 11 additions & 2 deletions activejob/lib/active_job/test_helper.rb
Expand Up @@ -595,9 +595,14 @@ def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, priority: ni
#
# If the +:at+ option is specified, then only run jobs enqueued to run
# immediately or before the given time
#
# If an adapter other than the test adapter is in use, this method just yields.
# See queue_adapter_for_test for more information.
def perform_enqueued_jobs(only: nil, except: nil, queue: nil, at: nil, &block)
return flush_enqueued_jobs(only: only, except: except, queue: queue, at: at) unless block_given?

return _assert_nothing_raised_or_warn("perform_enqueued_jobs", &block) unless using_test_adapter?

validate_option(only: only, except: except)

old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs
Expand Down Expand Up @@ -636,12 +641,16 @@ def queue_adapter
end

private
def using_test_adapter?
queue_adapter.is_a?(ActiveJob::QueueAdapters::TestAdapter)
end

def clear_enqueued_jobs
enqueued_jobs.clear
enqueued_jobs.clear if using_test_adapter?
end

def clear_performed_jobs
performed_jobs.clear
performed_jobs.clear if using_test_adapter?
end

def jobs_with(jobs, only: nil, except: nil, queue: nil, at: nil)
Expand Down
14 changes: 14 additions & 0 deletions activejob/test/cases/test_helper_test.rb
Expand Up @@ -2059,6 +2059,20 @@ def test_assert_performed_with_without_block_does_not_change_jobs_count
end
end

class AdapterIsNotTestAdapterTest < ActiveJob::TestCase
def queue_adapter_for_test
ActiveJob::QueueAdapters::InlineAdapter.new
end

def test_perform_enqueued_jobs_just_yields
JobBuffer.clear
perform_enqueued_jobs do
HelloJob.perform_later("kevin")
end
assert_equal(1, JobBuffer.values.size)
end
end

class OverrideQueueAdapterTest < ActiveJob::TestCase
class CustomQueueAdapter < ActiveJob::QueueAdapters::TestAdapter; end

Expand Down

0 comments on commit ba70a5b

Please sign in to comment.