2014-08-26 16:08:49 -04:00
|
|
|
require 'helper'
|
|
|
|
require 'active_support/core_ext/time'
|
|
|
|
require 'active_support/core_ext/date'
|
|
|
|
require 'jobs/hello_job'
|
|
|
|
require 'jobs/logging_job'
|
|
|
|
require 'jobs/nested_job'
|
2015-02-06 13:05:28 -05:00
|
|
|
require 'jobs/rescue_job'
|
2015-01-23 06:01:25 -05:00
|
|
|
require 'models/person'
|
2014-08-26 16:08:49 -04:00
|
|
|
|
|
|
|
class EnqueuedJobsTest < ActiveJob::TestCase
|
|
|
|
def test_assert_enqueued_jobs
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_enqueued_jobs 1 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('david')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_repeated_enqueued_jobs_calls
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_enqueued_jobs 1 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('abdelkader')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_enqueued_jobs 2 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('sean')
|
|
|
|
HelloJob.perform_later('yves')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_enqueued_jobs_with_no_block
|
|
|
|
assert_nothing_raised do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('rafael')
|
2014-08-26 16:08:49 -04:00
|
|
|
assert_enqueued_jobs 1
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('aaron')
|
|
|
|
HelloJob.perform_later('matthew')
|
2014-08-26 16:08:49 -04:00
|
|
|
assert_enqueued_jobs 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-20 14:23:03 -05:00
|
|
|
def test_assert_no_enqueued_jobs_with_no_block
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_no_enqueued_jobs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-26 16:08:49 -04:00
|
|
|
def test_assert_no_enqueued_jobs
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_no_enqueued_jobs do
|
2014-11-20 14:23:03 -05:00
|
|
|
HelloJob.perform_now
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_enqueued_jobs_too_few_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_enqueued_jobs 2 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('xavier')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/2 .* but 1/, error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_enqueued_jobs_too_many_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_enqueued_jobs 1 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('cristian')
|
|
|
|
HelloJob.perform_later('guillermo')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/1 .* but 2/, error.message)
|
|
|
|
end
|
2014-09-02 10:06:19 -04:00
|
|
|
|
2014-08-26 16:08:49 -04:00
|
|
|
def test_assert_no_enqueued_jobs_failure
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_no_enqueued_jobs do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('jeremy')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/0 .* but 1/, error.message)
|
|
|
|
end
|
|
|
|
|
2015-01-07 23:33:01 -05:00
|
|
|
def test_assert_enqueued_jobs_with_only_option
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_enqueued_jobs 1, only: HelloJob do
|
|
|
|
HelloJob.perform_later('jeremy')
|
|
|
|
LoggingJob.perform_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_enqueued_jobs_with_only_option_and_none_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_enqueued_jobs 1, only: HelloJob do
|
|
|
|
LoggingJob.perform_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/1 .* but 0/, error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_enqueued_jobs_with_only_option_and_too_few_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_enqueued_jobs 5, only: HelloJob do
|
|
|
|
HelloJob.perform_later('jeremy')
|
|
|
|
4.times { LoggingJob.perform_later }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/5 .* but 1/, error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_enqueued_jobs_with_only_option_and_too_many_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_enqueued_jobs 1, only: HelloJob do
|
|
|
|
2.times { HelloJob.perform_later('jeremy') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/1 .* but 2/, error.message)
|
|
|
|
end
|
|
|
|
|
2015-01-08 09:39:01 -05:00
|
|
|
def test_assert_no_enqueued_jobs_with_only_option
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_no_enqueued_jobs only: HelloJob do
|
|
|
|
LoggingJob.perform_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_no_enqueued_jobs_with_only_option_failure
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_no_enqueued_jobs only: HelloJob do
|
|
|
|
HelloJob.perform_later('jeremy')
|
|
|
|
LoggingJob.perform_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/0 .* but 1/, error.message)
|
|
|
|
end
|
|
|
|
|
2014-08-26 16:08:49 -04:00
|
|
|
def test_assert_enqueued_job
|
2014-08-29 16:11:17 -04:00
|
|
|
assert_enqueued_with(job: LoggingJob, queue: 'default') do
|
2014-11-20 14:23:03 -05:00
|
|
|
LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_enqueued_job_failure
|
|
|
|
assert_raise ActiveSupport::TestCase::Assertion do
|
2014-08-29 16:11:17 -04:00
|
|
|
assert_enqueued_with(job: LoggingJob, queue: 'default') do
|
2014-08-25 10:34:50 -04:00
|
|
|
NestedJob.perform_later
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-22 10:40:41 -04:00
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
2014-08-29 16:11:17 -04:00
|
|
|
assert_enqueued_with(job: NestedJob, queue: 'low') do
|
2014-08-25 10:34:50 -04:00
|
|
|
NestedJob.perform_later
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
2014-09-22 10:40:41 -04:00
|
|
|
|
2014-09-23 04:43:59 -04:00
|
|
|
assert_equal 'No enqueued job found with {:job=>NestedJob, :queue=>"low"}', error.message
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_enqueued_job_args
|
|
|
|
assert_raise ArgumentError do
|
2014-08-29 16:11:17 -04:00
|
|
|
assert_enqueued_with(class: LoggingJob) do
|
2014-08-25 10:34:50 -04:00
|
|
|
NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-01-23 06:01:25 -05:00
|
|
|
|
|
|
|
def test_assert_enqueued_job_with_global_id_args
|
|
|
|
ricardo = Person.new(9)
|
|
|
|
assert_enqueued_with(job: HelloJob, args: [ricardo]) do
|
|
|
|
HelloJob.perform_later(ricardo)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_enqueued_job_failure_with_global_id_args
|
|
|
|
ricardo = Person.new(9)
|
|
|
|
wilma = Person.new(11)
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_enqueued_with(job: HelloJob, args: [wilma]) do
|
|
|
|
HelloJob.perform_later(ricardo)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "No enqueued job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
|
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class PerformedJobsTest < ActiveJob::TestCase
|
|
|
|
def test_assert_performed_jobs
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_performed_jobs 1 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('david')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_repeated_performed_jobs_calls
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_performed_jobs 1 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('abdelkader')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_performed_jobs 2 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('sean')
|
|
|
|
HelloJob.perform_later('yves')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_performed_jobs_with_no_block
|
|
|
|
assert_nothing_raised do
|
2014-11-20 14:23:03 -05:00
|
|
|
perform_enqueued_jobs do
|
|
|
|
HelloJob.perform_later('rafael')
|
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
assert_performed_jobs 1
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
2014-11-20 14:23:03 -05:00
|
|
|
perform_enqueued_jobs do
|
|
|
|
HelloJob.perform_later('aaron')
|
|
|
|
HelloJob.perform_later('matthew')
|
|
|
|
assert_performed_jobs 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_no_performed_jobs_with_no_block
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_no_performed_jobs
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_no_performed_jobs
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_no_performed_jobs do
|
2014-11-20 14:23:03 -05:00
|
|
|
# empty block won't perform jobs
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_performed_jobs_too_few_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_performed_jobs 2 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('xavier')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/2 .* but 1/, error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_performed_jobs_too_many_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_performed_jobs 1 do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('cristian')
|
|
|
|
HelloJob.perform_later('guillermo')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/1 .* but 2/, error.message)
|
|
|
|
end
|
2014-09-02 10:06:19 -04:00
|
|
|
|
2014-08-26 16:08:49 -04:00
|
|
|
def test_assert_no_performed_jobs_failure
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_no_performed_jobs do
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.perform_later('jeremy')
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/0 .* but 1/, error.message)
|
|
|
|
end
|
|
|
|
|
2015-02-06 13:05:28 -05:00
|
|
|
def test_assert_performed_jobs_with_only_option
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_performed_jobs 1, only: HelloJob do
|
|
|
|
HelloJob.perform_later('jeremy')
|
|
|
|
LoggingJob.perform_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_performed_jobs_with_only_option_as_array
|
|
|
|
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 test_assert_performed_jobs_with_only_option_and_none_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_performed_jobs 1, only: HelloJob do
|
|
|
|
LoggingJob.perform_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/1 .* but 0/, error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_performed_jobs_with_only_option_and_too_few_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_performed_jobs 5, only: HelloJob do
|
|
|
|
HelloJob.perform_later('jeremy')
|
|
|
|
4.times { LoggingJob.perform_later }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/5 .* but 1/, error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_performed_jobs_with_only_option_and_too_many_sent
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_performed_jobs 1, only: HelloJob do
|
|
|
|
2.times { HelloJob.perform_later('jeremy') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/1 .* but 2/, error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_no_performed_jobs_with_only_option
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_no_performed_jobs only: HelloJob do
|
|
|
|
LoggingJob.perform_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_no_performed_jobs_with_only_option_as_array
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_no_performed_jobs only: [HelloJob, RescueJob] do
|
|
|
|
LoggingJob.perform_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_no_performed_jobs_with_only_option_failure
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_no_performed_jobs only: HelloJob do
|
|
|
|
HelloJob.perform_later('jeremy')
|
|
|
|
LoggingJob.perform_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/0 .* but 1/, error.message)
|
|
|
|
end
|
|
|
|
|
2014-08-26 16:08:49 -04:00
|
|
|
def test_assert_performed_job
|
2014-08-29 16:11:17 -04:00
|
|
|
assert_performed_with(job: NestedJob, queue: 'default') do
|
2014-08-25 10:34:50 -04:00
|
|
|
NestedJob.perform_later
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_performed_job_failure
|
|
|
|
assert_raise ActiveSupport::TestCase::Assertion do
|
2014-08-25 10:34:50 -04:00
|
|
|
assert_performed_with(job: LoggingJob, at: Date.tomorrow.noon, queue: 'default') do
|
|
|
|
NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise ActiveSupport::TestCase::Assertion do
|
2014-08-25 10:34:50 -04:00
|
|
|
assert_performed_with(job: NestedJob, at: Date.tomorrow.noon, queue: 'low') do
|
|
|
|
NestedJob.set(queue: 'low', wait_until: Date.tomorrow.noon).perform_later
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-01-23 06:01:25 -05:00
|
|
|
|
|
|
|
def test_assert_performed_job_with_global_id_args
|
|
|
|
ricardo = Person.new(9)
|
|
|
|
assert_performed_with(job: HelloJob, args: [ricardo]) do
|
|
|
|
HelloJob.perform_later(ricardo)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_performed_job_failure_with_global_id_args
|
|
|
|
ricardo = Person.new(9)
|
|
|
|
wilma = Person.new(11)
|
|
|
|
error = assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_performed_with(job: HelloJob, args: [wilma]) do
|
|
|
|
HelloJob.perform_later(ricardo)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "No performed job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
|
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|