-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Active Job test helpers #33635
Improve Active Job test helpers #33635
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic work @bogdanvlviv.
activejob/CHANGELOG.md
Outdated
``` | ||
|
||
*bogdanvlviv* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's combine into one changelog entry that heralds these together, that :queue
may be used with job assertions & helpers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in c6f4ddf.
activejob/CHANGELOG.md
Outdated
@@ -11,7 +65,7 @@ | |||
|
|||
*Zach Kemp* | |||
|
|||
* Allow `queue` option to `assert_no_enqueued_jobs`. | |||
* Allow `:queue` option to `assert_no_enqueued_jobs`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Array(reject).include?(job.class) | ||
Array(reject).include?(job.class) || (queue && job.queue_name != queue.to_s) | ||
elsif queue | ||
job.queue_name != queue.to_s | ||
else | ||
false | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can hoist up the queue
conditional since it applies to every clause here:
def filtered?(job)
filtered_queue?(job) || filtered_job_class?(job)
end
def filtered_queue?(job)
if queue
job.queue_name != queue.to_s
end
end
def filtered_job_class?(job)
if filter
!Array(filter).include?(job.class)
elsif reject
Array(reject).include?(job.class)
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍 Thanks for the tip. I like this simplified format. I applied this suggestion in ec2e8f6 and added you to authors of this commit.
@@ -52,7 +52,7 @@ def after_teardown # :nodoc: | |||
queue_adapter_changed_jobs.each { |klass| klass.disable_test_adapter } | |||
end | |||
|
|||
# Specifies the queue adapter to use with all active job test helpers. | |||
# Specifies the queue adapter to use with all Active Job test helpers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅 love these edits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool edit
end | ||
assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice style change. To confirm, I'm not seeing any implementation change here?
# | ||
# perform_enqueued_jobs | ||
# | ||
# assert_performed_jobs 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're showing two API calling styles here. Let's provide some guidance about when you'd use one vs. the other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. Then we should provide guidance about the one vs the other to every helper/assertion.
I believe that this difference is explicit from the current explanations like
"If a block is passed, that block performs all of the jobs that were enqueued throughout the duration of the block and asserts that the job has been performed with the given arguments in the block."
and examples.
Let's go with as it is for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed 👍
@bogdanvlviv Do you think 2b471e1 is safe to backport to 5-2-stable without breaking compatibility? (As far as I can see, it's a bugfix.) |
If the `:queue` option is specified, then only the job(s) enqueued to a specific queue will be performed. Example: ``` def test_perform_enqueued_jobs_with_queue perform_enqueued_jobs queue: :some_queue do MyJob.set(queue: :some_queue).perform_later(1, 2, 3) # will be performed HelloJob.set(queue: :other_queue).perform_later(1, 2, 3) # will not be performed end assert_performed_jobs 1 end ``` Follow up rails#33265 [bogdanvlviv & Jeremy Daer]
20555dc
to
ebb637c
Compare
@jeremy We shouldn't backport e0cf042 to The current implementation of |
How about an alternative of b43d3c07b5eeff5c0569b901933d17071e387b36 to touch few code like here? diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb
index b45cc57fb0..b130b9604b 100644
--- a/activejob/lib/active_job/test_helper.rb
+++ b/activejob/lib/active_job/test_helper.rb
@@ -404,6 +404,8 @@ def assert_performed_with(job: nil, args: nil, at: nil, queue: nil)
# end
#
def perform_enqueued_jobs(only: nil, except: nil)
+ return flush_enqueued_jobs(only: only, except: except) unless block_given?
+
validate_option(only: only, except: except)
old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs
old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs
@@ -416,7 +418,7 @@ def perform_enqueued_jobs(only: nil, except: nil)
queue_adapter.filter = only
queue_adapter.reject = except
- block_given? ? yield : flush_enqueued_jobs(only: only, except: except)
+ yield
ensure
queue_adapter.perform_enqueued_jobs = old_perform_enqueued_jobs
queue_adapter.perform_enqueued_at_jobs = old_perform_enqueued_at_jobs |
I want to move
to Also we should execute Do you want me to redo b43d3c07b5eeff5c0569b901933d17071e387b36? |
rails/activejob/lib/active_job/test_helper.rb Lines 446 to 447 in 265eeb2
Is there any reason to do Personally I feel the remaining part in the commit is like a cosmetic (moving a code chunk, changing indentation, for the purpose of the commit). |
Set ```` queue_adapter.perform_enqueued_jobs = true queue_adapter.perform_enqueued_at_jobs = true queue_adapter.filter = only queue_adapter.reject = except queue_adapter.queue = queue ``` if block given. Execution of `flush_enqueued_jobs` doesn't require that.
If the `:queue` option is specified, then only the job(s) enqueued to a specific queue will be performed. Example: ``` def test_assert_performed_jobs_with_queue_option assert_performed_jobs 1, queue: :some_queue do HelloJob.set(queue: :some_queue).perform_later("jeremy") HelloJob.set(queue: :other_queue).perform_later("bogdan") end end ```
If the `:queue` option is specified, then only the job(s) enqueued to a specific queue will not be performed. Example: ``` def test_assert_no_performed_jobs_with_queue_option assert_no_performed_jobs queue: :some_queue do HelloJob.set(queue: :other_queue).perform_later("jeremy") end end ```
…d helpers Note that it removes changelog entry of rails#33265 since the entry in this commits includes that too.
Execution of `assert_performed_jobs`, and `assert_no_performed_jobs` without a block should respect passed `:except`, `:only`, and `:queue` options.
Example: ``` def test_assert_performed_with MyJob.perform_later(1,2,3) perform_enqueued_jobs assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high') end ``` Follow up rails#33626.
ebb637c
to
b857642
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏
Those are really great addition to AJ test helper, thanks ❤️ ! |
Thanks all 🙇♂️ ! |
This patch corrects a duplicate method name introduced in rails#33635. Also fixes typo in method names.
This patch corrects a duplicate method name introduced in #33635. Also fixes typo in method names.
ec2e8f6
Allow
:queue
option toperform_enqueued_jobs
.If the
:queue
option is specified, then only the job(s) enqueued toa specific queue will be performed.
Example:
Follow up Allow
queue
option toassert_no_enqueued_jobs
#33265[bogdanvlviv & Jeremy Daer]
e0cf042
Fix
perform_enqueued_jobs
Set
d50fb21
Allow
:queue
option toassert_performed_jobs
.If the
:queue
option is specified, then only the job(s) enqueued to a specificqueue will be performed.
Example:
de4420d
Allow
:queue
option toassert_no_performed_jobs
.If the
:queue
option is specified, then only the job(s) enqueued to a specificqueue will not be performed.
Example:
2bf8b4e
Add changelog entry about adding
:queue
option to job assertions and helpersNote that it removes changelog entry of Allow
queue
option toassert_no_enqueued_jobs
#33265 since the entry in this commitsincludes that too.
11634e8
Fix
assert_performed_jobs
andassert_no_performed_jobs
Execution of
assert_performed_jobs
, andassert_no_performed_jobs
without a block should respect passed
:except
,:only
, and:queue
options.2ec60fb
Allow
assert_performed_with
to be called without a block.Example:
Follow up Allow
perform_enqueued_jobs
to be called without a block. #33626.b7beb5d
Fix formatting of
ActiveJob::TestHelper
api docsb857642
DRY in
assert_enqueued_jobs
Note that these changes keep backward compatibility, so It shouldn't break users' tests during an upgrade to 6.0.