Skip to content
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

Lazily deserialize job arguments in test helper #39607

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions activejob/lib/active_job/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,18 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil, &block)
end

matching_job = jobs.find do |enqueued_job|
if expected_args[:job].present?
if expected_args[:job].respond_to?(:call)
if !expected_args[:job].call(enqueued_job[:job])
potential_matches << enqueued_job
next
end
elsif expected_args[:job] != enqueued_job[:job]
potential_matches << enqueued_job
next
end
end
Comment on lines +405 to +415
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this doesn't solve the problem in the general case, since an ActiveJob::DeserializationError can still blow up the assertion if :job isn't specified, no?

Also, I think assert_performed_with needs similar protection. Something like the following would cause the same problem:

model = Model.create!
ModelJob.perform_later(model)
perform_enqueued_jobs
model.destroy
assert_performed_with(job: ModelJob)

Perhaps we could rescue ActiveJob::DeserializationError in deserialize_args_for_assertion? Although I'm not clear on whether we should attempt to match such jobs afterwards or not... 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - this definitely isn't a "fix all scenarios" patch, just make it less likely to fail 😓

The new scenario you raise is a super interesting one (and solving that might make this a more robust solution). I'll pick this back up later in the week and see what I can come up with.

Thanks for taking a look and giving some feedback, @jonathanhefner!


deserialized_job = deserialize_args_for_assertion(enqueued_job)
potential_matches << deserialized_job

Expand Down
44 changes: 44 additions & 0 deletions activejob/test/cases/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,41 @@
require "models/person"

class EnqueuedJobsTest < ActiveJob::TestCase
class ExplodingSerializer < ActiveJob::Serializers::ObjectSerializer
def serialize(object)
super({ "value" => object.value })
end

def deserialize(hash)
fail "bang"
end

private
def klass
DummyValueObject
end
end

class DummyValueObject
attr_accessor :value

def initialize(value)
@value = value
end

def ==(other)
self.value == other.value
end
end

setup do
@original_serializers = ActiveJob::Serializers.serializers
end

teardown do
ActiveJob::Serializers._additional_serializers = @original_serializers
end

def test_assert_enqueued_jobs
assert_nothing_raised do
assert_enqueued_jobs 1 do
Expand Down Expand Up @@ -703,6 +738,15 @@ def test_assert_enqueued_with_with_no_block_does_not_change_jobs_count
assert_equal 2, queue_adapter.enqueued_jobs.count
end

def test_assert_enqueued_with_with_earlier_deserializable_args
ActiveJob::Serializers.add_serializers ExplodingSerializer

assert_enqueued_with(job: HelloJob, args: ["Owl"]) do
LoggingJob.perform_later(DummyValueObject.new("Test")) # ExplodingSerializer will cause deserialization to fail
HelloJob.perform_later("Owl")
end
end

def test_assert_enqueued_jobs_with_performed
assert_enqueued_with(job: LoggingJob) do
perform_enqueued_jobs(only: HelloJob) do
Expand Down