2014-12-30 10:53:42 -05:00
|
|
|
require 'active_support/core_ext/hash/keys'
|
|
|
|
|
2014-08-26 16:08:49 -04:00
|
|
|
module ActiveJob
|
|
|
|
# Provides helper methods for testing Active Job
|
|
|
|
module TestHelper
|
2014-08-29 16:11:17 -04:00
|
|
|
extend ActiveSupport::Concern
|
2014-08-26 16:08:49 -04:00
|
|
|
|
2014-08-29 16:11:17 -04:00
|
|
|
included do
|
2014-09-02 10:06:19 -04:00
|
|
|
def before_setup
|
2014-09-24 00:11:54 -04:00
|
|
|
@old_queue_adapter = queue_adapter
|
2014-09-02 10:06:19 -04:00
|
|
|
ActiveJob::Base.queue_adapter = :test
|
|
|
|
clear_enqueued_jobs
|
|
|
|
clear_performed_jobs
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_teardown
|
|
|
|
super
|
|
|
|
ActiveJob::Base.queue_adapter = @old_queue_adapter
|
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
|
2014-08-29 16:11:17 -04:00
|
|
|
# Asserts that the number of enqueued jobs matches the given number.
|
|
|
|
#
|
|
|
|
# def test_jobs
|
|
|
|
# assert_enqueued_jobs 0
|
2014-09-05 02:35:19 -04:00
|
|
|
# HelloJob.perform_later('david')
|
2014-08-29 16:11:17 -04:00
|
|
|
# assert_enqueued_jobs 1
|
2014-09-05 02:35:19 -04:00
|
|
|
# HelloJob.perform_later('abdelkader')
|
2014-08-29 16:11:17 -04:00
|
|
|
# assert_enqueued_jobs 2
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If a block is passed, that block should cause the specified number of
|
|
|
|
# jobs to be enqueued.
|
|
|
|
#
|
|
|
|
# def test_jobs_again
|
|
|
|
# assert_enqueued_jobs 1 do
|
2014-09-05 02:35:19 -04:00
|
|
|
# HelloJob.perform_later('cristian')
|
2014-08-29 16:11:17 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# assert_enqueued_jobs 2 do
|
2014-09-05 02:35:19 -04:00
|
|
|
# HelloJob.perform_later('aaron')
|
|
|
|
# HelloJob.perform_later('rafael')
|
2014-08-29 16:11:17 -04:00
|
|
|
# end
|
|
|
|
# end
|
2015-01-07 23:33:01 -05:00
|
|
|
#
|
|
|
|
# The number of times a specific job is enqueued can be asserted.
|
|
|
|
#
|
|
|
|
# def test_logging_job
|
|
|
|
# assert_enqueued_jobs 2, only: LoggingJob do
|
|
|
|
# LoggingJob.perform_later
|
|
|
|
# HelloJob.perform_later('jeremy')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def assert_enqueued_jobs(number, only: nil)
|
2014-08-29 16:11:17 -04:00
|
|
|
if block_given?
|
2015-01-07 23:33:01 -05:00
|
|
|
original_count = enqueued_jobs_size(only: only)
|
2014-08-29 16:11:17 -04:00
|
|
|
yield
|
2015-01-07 23:33:01 -05:00
|
|
|
new_count = enqueued_jobs_size(only: only)
|
|
|
|
assert_equal original_count + number, new_count, "#{number} jobs expected, but #{new_count - original_count} were enqueued"
|
2014-08-29 16:11:17 -04:00
|
|
|
else
|
2015-01-07 23:33:01 -05:00
|
|
|
actual_count = enqueued_jobs_size(only: only)
|
|
|
|
assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued"
|
2014-08-29 16:11:17 -04:00
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
|
2014-11-01 14:27:18 -04:00
|
|
|
# Asserts that no jobs have been enqueued.
|
2014-08-29 16:11:17 -04:00
|
|
|
#
|
|
|
|
# def test_jobs
|
|
|
|
# assert_no_enqueued_jobs
|
2014-09-05 02:35:19 -04:00
|
|
|
# HelloJob.perform_later('jeremy')
|
2014-08-29 16:11:17 -04:00
|
|
|
# assert_enqueued_jobs 1
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If a block is passed, that block should not cause any job to be enqueued.
|
|
|
|
#
|
|
|
|
# def test_jobs_again
|
|
|
|
# assert_no_enqueued_jobs do
|
|
|
|
# # No job should be enqueued from this block
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2015-01-08 09:39:01 -05:00
|
|
|
# It can be asserted that no jobs of a specific kind are enqueued:
|
|
|
|
#
|
|
|
|
# def test_no_logging
|
|
|
|
# assert_no_enqueued_jobs only: LoggingJob do
|
|
|
|
# HelloJob.perform_later('jeremy')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2014-08-29 16:11:17 -04:00
|
|
|
# Note: This assertion is simply a shortcut for:
|
|
|
|
#
|
2014-10-20 15:10:59 -04:00
|
|
|
# assert_enqueued_jobs 0, &block
|
2015-01-08 09:39:01 -05:00
|
|
|
def assert_no_enqueued_jobs(only: nil, &block)
|
|
|
|
assert_enqueued_jobs 0, only: only, &block
|
2014-08-29 16:11:17 -04:00
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
|
2014-08-29 16:11:17 -04:00
|
|
|
# Asserts that the number of performed jobs matches the given number.
|
2014-11-20 14:23:03 -05:00
|
|
|
# If no block is passed, <tt>perform_enqueued_jobs</tt>
|
|
|
|
# must be called around the job call.
|
2014-08-29 16:11:17 -04:00
|
|
|
#
|
|
|
|
# def test_jobs
|
|
|
|
# assert_performed_jobs 0
|
2014-11-20 14:23:03 -05:00
|
|
|
#
|
|
|
|
# perform_enqueued_jobs do
|
|
|
|
# HelloJob.perform_later('xavier')
|
|
|
|
# end
|
2014-08-29 16:11:17 -04:00
|
|
|
# assert_performed_jobs 1
|
2014-11-20 14:23:03 -05:00
|
|
|
#
|
|
|
|
# perform_enqueued_jobs do
|
|
|
|
# HelloJob.perform_later('yves')
|
|
|
|
# assert_performed_jobs 2
|
|
|
|
# end
|
2014-08-29 16:11:17 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If a block is passed, that block should cause the specified number of
|
|
|
|
# jobs to be performed.
|
|
|
|
#
|
|
|
|
# def test_jobs_again
|
|
|
|
# assert_performed_jobs 1 do
|
2014-09-05 02:35:19 -04:00
|
|
|
# HelloJob.perform_later('robin')
|
2014-08-29 16:11:17 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# assert_performed_jobs 2 do
|
2014-09-05 02:35:19 -04:00
|
|
|
# HelloJob.perform_later('carlos')
|
|
|
|
# HelloJob.perform_later('sean')
|
2014-08-29 16:11:17 -04:00
|
|
|
# end
|
|
|
|
# end
|
2015-02-06 13:05:28 -05:00
|
|
|
#
|
|
|
|
# The block form supports filtering. If the :only option is specified,
|
|
|
|
# then only the listed job(s) will be performed.
|
|
|
|
#
|
|
|
|
# def test_hello_job
|
|
|
|
# assert_performed_jobs 1, only: HelloJob do
|
|
|
|
# HelloJob.perform_later('jeremy')
|
|
|
|
# LoggingJob.perform_later
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# An array may also be specified, to support testing multiple jobs.
|
|
|
|
#
|
|
|
|
# def test_hello_and_logging_jobs
|
|
|
|
# assert_nothing_raised do
|
|
|
|
# assert_performed_jobs 2, only: [HelloJob, LoggingJob] do
|
|
|
|
# HelloJob.perform_later('jeremy')
|
|
|
|
# LoggingJob.perform_later('stewie')
|
|
|
|
# RescueJob.perform_later('david')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def assert_performed_jobs(number, only: nil)
|
2014-08-29 16:11:17 -04:00
|
|
|
if block_given?
|
|
|
|
original_count = performed_jobs.size
|
2015-02-06 13:05:28 -05:00
|
|
|
perform_enqueued_jobs(only: only) { yield }
|
2014-08-29 16:11:17 -04:00
|
|
|
new_count = performed_jobs.size
|
|
|
|
assert_equal original_count + number, new_count,
|
|
|
|
"#{number} jobs expected, but #{new_count - original_count} were performed"
|
|
|
|
else
|
2014-09-02 10:06:19 -04:00
|
|
|
performed_jobs_size = performed_jobs.size
|
|
|
|
assert_equal number, performed_jobs_size, "#{number} jobs expected, but #{performed_jobs_size} were performed"
|
2014-08-29 16:11:17 -04:00
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
|
2014-08-29 16:11:17 -04:00
|
|
|
# Asserts that no jobs have been performed.
|
|
|
|
#
|
|
|
|
# def test_jobs
|
|
|
|
# assert_no_performed_jobs
|
2014-11-20 14:23:03 -05:00
|
|
|
#
|
|
|
|
# perform_enqueued_jobs do
|
|
|
|
# HelloJob.perform_later('matthew')
|
|
|
|
# assert_performed_jobs 1
|
|
|
|
# end
|
2014-08-29 16:11:17 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If a block is passed, that block should not cause any job to be performed.
|
|
|
|
#
|
|
|
|
# def test_jobs_again
|
|
|
|
# assert_no_performed_jobs do
|
|
|
|
# # No job should be performed from this block
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2015-02-06 13:05:28 -05:00
|
|
|
# The block form supports filtering. If the :only option is specified,
|
|
|
|
# then only the listed job(s) will be performed.
|
|
|
|
#
|
|
|
|
# def test_hello_job
|
|
|
|
# assert_performed_jobs 1, only: HelloJob do
|
|
|
|
# HelloJob.perform_later('jeremy')
|
|
|
|
# LoggingJob.perform_later
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# An array may also be specified, to support testing multiple jobs.
|
|
|
|
#
|
|
|
|
# def test_hello_and_logging_jobs
|
|
|
|
# assert_nothing_raised do
|
|
|
|
# assert_performed_jobs 2, only: [HelloJob, LoggingJob] do
|
|
|
|
# HelloJob.perform_later('jeremy')
|
|
|
|
# LoggingJob.perform_later('stewie')
|
|
|
|
# RescueJob.perform_later('david')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2014-08-29 16:11:17 -04:00
|
|
|
# Note: This assertion is simply a shortcut for:
|
|
|
|
#
|
2014-10-20 15:10:59 -04:00
|
|
|
# assert_performed_jobs 0, &block
|
2015-02-06 13:05:28 -05:00
|
|
|
def assert_no_performed_jobs(only: nil, &block)
|
|
|
|
assert_performed_jobs 0, only: only, &block
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
|
2014-08-29 16:11:17 -04:00
|
|
|
# Asserts that the job passed in the block has been enqueued with the given arguments.
|
|
|
|
#
|
2014-11-01 14:27:18 -04:00
|
|
|
# def test_assert_enqueued_with
|
2014-08-29 16:11:17 -04:00
|
|
|
# assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low') do
|
2014-09-05 02:35:19 -04:00
|
|
|
# MyJob.perform_later(1,2,3)
|
2014-08-29 16:11:17 -04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def assert_enqueued_with(args = {}, &_block)
|
|
|
|
original_enqueued_jobs = enqueued_jobs.dup
|
|
|
|
clear_enqueued_jobs
|
|
|
|
args.assert_valid_keys(:job, :args, :at, :queue)
|
2015-01-23 06:01:25 -05:00
|
|
|
serialized_args = serialize_args_for_assertion(args)
|
2014-08-29 16:11:17 -04:00
|
|
|
yield
|
|
|
|
matching_job = enqueued_jobs.any? do |job|
|
2015-01-23 06:01:25 -05:00
|
|
|
serialized_args.all? { |key, value| value == job[key] }
|
2014-08-29 16:11:17 -04:00
|
|
|
end
|
2014-09-22 10:40:41 -04:00
|
|
|
assert matching_job, "No enqueued job found with #{args}"
|
2014-08-29 16:11:17 -04:00
|
|
|
ensure
|
|
|
|
queue_adapter.enqueued_jobs = original_enqueued_jobs + enqueued_jobs
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
|
2014-08-29 16:11:17 -04:00
|
|
|
# Asserts that the job passed in the block has been performed with the given arguments.
|
|
|
|
#
|
|
|
|
# def test_assert_performed_with
|
|
|
|
# assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high') do
|
2014-09-05 02:35:19 -04:00
|
|
|
# MyJob.perform_later(1,2,3)
|
2014-08-29 16:11:17 -04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def assert_performed_with(args = {}, &_block)
|
|
|
|
original_performed_jobs = performed_jobs.dup
|
|
|
|
clear_performed_jobs
|
|
|
|
args.assert_valid_keys(:job, :args, :at, :queue)
|
2015-01-23 06:01:25 -05:00
|
|
|
serialized_args = serialize_args_for_assertion(args)
|
2014-11-20 14:23:03 -05:00
|
|
|
perform_enqueued_jobs { yield }
|
2014-08-29 16:11:17 -04:00
|
|
|
matching_job = performed_jobs.any? do |job|
|
2015-01-23 06:01:25 -05:00
|
|
|
serialized_args.all? { |key, value| value == job[key] }
|
2014-08-29 16:11:17 -04:00
|
|
|
end
|
|
|
|
assert matching_job, "No performed job found with #{args}"
|
|
|
|
ensure
|
|
|
|
queue_adapter.performed_jobs = original_performed_jobs + performed_jobs
|
|
|
|
end
|
|
|
|
|
2015-02-06 13:05:28 -05:00
|
|
|
def perform_enqueued_jobs(only: nil)
|
2015-03-06 12:41:46 -05:00
|
|
|
old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs
|
|
|
|
old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs
|
|
|
|
old_filter = queue_adapter.filter
|
|
|
|
|
|
|
|
begin
|
|
|
|
queue_adapter.perform_enqueued_jobs = true
|
|
|
|
queue_adapter.perform_enqueued_at_jobs = true
|
|
|
|
queue_adapter.filter = only
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
queue_adapter.perform_enqueued_jobs = old_perform_enqueued_jobs
|
|
|
|
queue_adapter.perform_enqueued_at_jobs = old_perform_enqueued_at_jobs
|
|
|
|
queue_adapter.filter = old_filter
|
|
|
|
end
|
2014-11-20 14:23:03 -05:00
|
|
|
end
|
|
|
|
|
2014-08-29 16:11:17 -04:00
|
|
|
def queue_adapter
|
|
|
|
ActiveJob::Base.queue_adapter
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
2014-08-29 16:11:17 -04:00
|
|
|
|
|
|
|
delegate :enqueued_jobs, :enqueued_jobs=,
|
|
|
|
:performed_jobs, :performed_jobs=,
|
|
|
|
to: :queue_adapter
|
|
|
|
|
|
|
|
private
|
|
|
|
def clear_enqueued_jobs
|
|
|
|
enqueued_jobs.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_performed_jobs
|
|
|
|
performed_jobs.clear
|
|
|
|
end
|
2015-01-07 23:33:01 -05:00
|
|
|
|
|
|
|
def enqueued_jobs_size(only: nil)
|
|
|
|
if only
|
2014-09-24 00:11:54 -04:00
|
|
|
enqueued_jobs.select { |job| job.fetch(:job) == only }.size
|
2015-01-07 23:33:01 -05:00
|
|
|
else
|
|
|
|
enqueued_jobs.size
|
|
|
|
end
|
|
|
|
end
|
2015-01-23 06:01:25 -05:00
|
|
|
|
|
|
|
def serialize_args_for_assertion(args)
|
|
|
|
serialized_args = args.dup
|
|
|
|
if job_args = serialized_args.delete(:args)
|
|
|
|
serialized_args[:args] = ActiveJob::Arguments.serialize(job_args)
|
|
|
|
end
|
|
|
|
serialized_args
|
|
|
|
end
|
2014-08-29 16:11:17 -04:00
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|