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

Lazily deserialize job arguments in test helper #39607

wants to merge 1 commit into from

Conversation

BrentWheeldon
Copy link
Contributor

Summary

If there is a job in enqueued_jobs whose arguments are not deserializable for some reason (for example, a deleted ActiveRecord model) assert_enqueued_with will raise an error even if the job we're looking for is in the enqueued_jobs array.

This solution isn't as tidy as I was hoping for - definitely open to suggestions on other approaches!

Other Information

Fixes #39606

@rails-bot rails-bot bot added the activejob label Jun 12, 2020
If there is a job in `enqueued_jobs` whose arguments are not
deserializable for some reason (for example, a deleted ActiveRecord
model) `assert_enqueued_with` will raise an error even if the job we're
looking for is in the `enqueued_jobs` array.
Comment on lines +405 to +415
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
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!

@rails-bot
Copy link

rails-bot bot commented Sep 13, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Sep 13, 2020
@rails-bot rails-bot bot closed this Sep 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

assert_enqueued_with somewhat unexpectedly raises ActiveJob::DeserializationError
2 participants