2016-08-06 12:41:18 -04:00
|
|
|
require "helper"
|
|
|
|
require "jobs/logging_job"
|
|
|
|
require "jobs/hello_job"
|
|
|
|
require "jobs/provider_jid_job"
|
|
|
|
require "active_support/core_ext/numeric/time"
|
2014-08-18 03:19:41 -04:00
|
|
|
|
|
|
|
class QueuingTest < ActiveSupport::TestCase
|
2016-08-06 12:41:18 -04:00
|
|
|
test "should run jobs enqueued on a listening queue" do
|
2014-08-18 03:19:41 -04:00
|
|
|
TestJob.perform_later @id
|
|
|
|
wait_for_jobs_to_finish_for(5.seconds)
|
|
|
|
assert job_executed
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "should not run jobs queued on a non-listening queue" do
|
2015-08-15 23:41:33 -04:00
|
|
|
skip if adapter_is?(:inline, :async, :sucker_punch, :que)
|
2014-10-10 02:53:22 -04:00
|
|
|
old_queue = TestJob.queue_name
|
|
|
|
|
2014-08-18 03:19:41 -04:00
|
|
|
begin
|
|
|
|
TestJob.queue_as :some_other_queue
|
|
|
|
TestJob.perform_later @id
|
|
|
|
wait_for_jobs_to_finish_for(2.seconds)
|
|
|
|
assert_not job_executed
|
|
|
|
ensure
|
|
|
|
TestJob.queue_name = old_queue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "should supply a wrapped class name to Sidekiq" do
|
2015-03-23 18:16:07 -04:00
|
|
|
skip unless adapter_is?(:sidekiq)
|
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
::HelloJob.perform_later
|
|
|
|
hash = ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper.jobs.first
|
2016-08-06 12:41:18 -04:00
|
|
|
assert_equal "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper", hash["class"]
|
|
|
|
assert_equal "HelloJob", hash["wrapped"]
|
2015-03-23 18:16:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "should access provider_job_id inside Sidekiq job" do
|
2016-07-26 17:46:03 -04:00
|
|
|
skip unless adapter_is?(:sidekiq)
|
|
|
|
Sidekiq::Testing.inline! do
|
|
|
|
job = ::ProviderJidJob.perform_later
|
|
|
|
assert_equal "Provider Job ID: #{job.provider_job_id}", JobBuffer.last_value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-18 03:44:28 -04:00
|
|
|
test "resque JobWrapper should have instance variable queue" do
|
|
|
|
skip unless adapter_is?(:resque)
|
|
|
|
job = ::HelloJob.set(wait: 5.seconds).perform_later
|
|
|
|
hash = Resque.decode(Resque.find_delayed_selection { true }[0])
|
|
|
|
assert_equal hash["queue"], job.queue_name
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "should not run job enqueued in the future" do
|
2014-08-18 03:19:41 -04:00
|
|
|
begin
|
|
|
|
TestJob.set(wait: 10.minutes).perform_later @id
|
|
|
|
wait_for_jobs_to_finish_for(5.seconds)
|
|
|
|
assert_not job_executed
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "should run job enqueued in the future at the specified time" do
|
2014-08-18 03:19:41 -04:00
|
|
|
begin
|
2015-04-22 16:06:58 -04:00
|
|
|
TestJob.set(wait: 5.seconds).perform_later @id
|
2014-08-18 03:19:41 -04:00
|
|
|
wait_for_jobs_to_finish_for(2.seconds)
|
|
|
|
assert_not job_executed
|
|
|
|
wait_for_jobs_to_finish_for(10.seconds)
|
|
|
|
assert job_executed
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip
|
|
|
|
end
|
|
|
|
end
|
2015-04-26 13:05:08 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "should supply a provider_job_id when available for immediate jobs" do
|
2016-02-28 13:45:16 -05:00
|
|
|
skip unless adapter_is?(:async, :delayed_job, :sidekiq, :qu, :que, :queue_classic)
|
2015-04-26 13:05:08 -04:00
|
|
|
test_job = TestJob.perform_later @id
|
2016-08-06 12:41:18 -04:00
|
|
|
assert test_job.provider_job_id, "Provider job id should be set by provider"
|
2015-05-07 18:32:15 -04:00
|
|
|
end
|
2015-05-07 09:57:33 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "should supply a provider_job_id when available for delayed jobs" do
|
2016-02-28 13:45:16 -05:00
|
|
|
skip unless adapter_is?(:async, :delayed_job, :sidekiq, :que, :queue_classic)
|
2015-05-07 09:57:33 -04:00
|
|
|
delayed_test_job = TestJob.set(wait: 1.minute).perform_later @id
|
2016-08-06 12:41:18 -04:00
|
|
|
assert delayed_test_job.provider_job_id, "Provider job id should by set for delayed jobs by provider"
|
2015-05-07 09:57:33 -04:00
|
|
|
end
|
2015-07-07 15:52:28 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "current locale is kept while running perform_later" do
|
2015-07-07 15:52:28 -04:00
|
|
|
skip if adapter_is?(:inline)
|
|
|
|
|
|
|
|
begin
|
|
|
|
I18n.available_locales = [:en, :de]
|
|
|
|
I18n.locale = :de
|
|
|
|
|
|
|
|
TestJob.perform_later @id
|
|
|
|
wait_for_jobs_to_finish_for(5.seconds)
|
|
|
|
assert job_executed
|
2016-08-06 12:41:18 -04:00
|
|
|
assert_equal "de", job_executed_in_locale
|
2015-07-07 15:52:28 -04:00
|
|
|
ensure
|
|
|
|
I18n.available_locales = [:en]
|
|
|
|
I18n.locale = :en
|
|
|
|
end
|
|
|
|
end
|
2015-03-18 05:48:26 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "should run job with higher priority first" do
|
2015-03-18 05:48:26 -04:00
|
|
|
skip unless adapter_is?(:delayed_job, :que)
|
|
|
|
|
|
|
|
wait_until = Time.now + 3.seconds
|
|
|
|
TestJob.set(wait_until: wait_until, priority: 20).perform_later "#{@id}.1"
|
|
|
|
TestJob.set(wait_until: wait_until, priority: 10).perform_later "#{@id}.2"
|
|
|
|
wait_for_jobs_to_finish_for(10.seconds)
|
|
|
|
assert job_executed "#{@id}.1"
|
|
|
|
assert job_executed "#{@id}.2"
|
|
|
|
assert job_executed_at("#{@id}.2") < job_executed_at("#{@id}.1")
|
|
|
|
end
|
2014-08-18 03:19:41 -04:00
|
|
|
end
|