Skip to content

Instantly share code, notes, and snippets.

@Aethelflaed
Created January 15, 2015 14:21
Show Gist options
  • Save Aethelflaed/3f5ea91b0ba9314cab24 to your computer and use it in GitHub Desktop.
Save Aethelflaed/3f5ea91b0ba9314cab24 to your computer and use it in GitHub Desktop.
ActiveJob's assert_enqueued_with at comparison
I use this method instead of ActiveJob's original version to assert for enqueued job with given parameters / options.
The main point of this patch is to fix the float to float comparison with the at attribute by using an epsilon which can also be provided.
With the original version, it is impossible to test if a job is enqueued at a specified time.
I also added the wait attribute, which correspond more to my use of ActiveJob.
def assert_enqueued_with(args = {}, &block)
original_enqueued_jobs = enqueued_jobs.dup
clear_enqueued_jobs
args[:at] = args.delete(:wait).seconds.from_now.to_f if args.has_key?(:wait)
at = args.delete(:at)
epislon = args.delete(:epislon) || 0.1
args.assert_valid_keys(:job, :args, :queue)
yield
matching_job = enqueued_jobs.any? do |job|
matching = args.all? { |key, value| value == job[key] }
if at
matching &= job.has_key?(:at) && (job[:at] - at).abs < epislon
end
matching
end
assert matching_job, "No enqueued job found with #{args}"
ensure
queue_adapter.enqueued_jobs = original_enqueued_jobs + enqueued_jobs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment