2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
require "helper"
|
|
|
|
require "jobs/hello_job"
|
2019-09-11 20:20:09 -04:00
|
|
|
require "jobs/prefixed_job"
|
2016-08-06 12:41:18 -04:00
|
|
|
require "jobs/logging_job"
|
|
|
|
require "jobs/nested_job"
|
2014-05-18 10:24:30 -04:00
|
|
|
|
|
|
|
class QueueNamingTest < ActiveSupport::TestCase
|
2016-08-06 12:41:18 -04:00
|
|
|
test "name derived from base" do
|
2018-11-03 22:39:18 -04:00
|
|
|
assert_equal "default", HelloJob.new.queue_name
|
2014-05-18 10:24:30 -04:00
|
|
|
end
|
2014-08-12 06:53:46 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "uses given queue name job" do
|
2014-10-10 02:53:22 -04:00
|
|
|
original_queue_name = HelloJob.queue_name
|
|
|
|
|
2014-05-18 10:24:30 -04:00
|
|
|
begin
|
|
|
|
HelloJob.queue_as :greetings
|
2014-08-25 10:34:50 -04:00
|
|
|
assert_equal "greetings", HelloJob.new.queue_name
|
|
|
|
ensure
|
|
|
|
HelloJob.queue_name = original_queue_name
|
|
|
|
end
|
|
|
|
end
|
2014-06-09 12:49:14 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "allows a blank queue name" do
|
2014-10-10 02:53:22 -04:00
|
|
|
original_queue_name = HelloJob.queue_name
|
|
|
|
|
2014-10-09 13:33:55 -04:00
|
|
|
begin
|
|
|
|
HelloJob.queue_as ""
|
|
|
|
assert_equal "", HelloJob.new.queue_name
|
|
|
|
ensure
|
|
|
|
HelloJob.queue_name = original_queue_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "does not use a nil queue name" do
|
2014-10-10 02:53:22 -04:00
|
|
|
original_queue_name = HelloJob.queue_name
|
|
|
|
|
2014-10-09 13:33:55 -04:00
|
|
|
begin
|
|
|
|
HelloJob.queue_as nil
|
|
|
|
assert_equal "default", HelloJob.new.queue_name
|
|
|
|
ensure
|
|
|
|
HelloJob.queue_name = original_queue_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "evals block given to queue_as to determine queue" do
|
2014-10-10 02:53:22 -04:00
|
|
|
original_queue_name = HelloJob.queue_name
|
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
begin
|
|
|
|
HelloJob.queue_as { :another }
|
|
|
|
assert_equal "another", HelloJob.new.queue_name
|
2014-05-18 10:24:30 -04:00
|
|
|
ensure
|
2014-08-25 10:34:50 -04:00
|
|
|
HelloJob.queue_name = original_queue_name
|
2014-05-18 10:24:30 -04:00
|
|
|
end
|
|
|
|
end
|
2014-08-18 04:17:36 -04:00
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "can use arguments to determine queue_name in queue_as block" do
|
2014-10-10 02:53:22 -04:00
|
|
|
original_queue_name = HelloJob.queue_name
|
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
begin
|
2017-01-05 03:20:57 -05:00
|
|
|
HelloJob.queue_as { arguments.first == "1" ? :one : :two }
|
2016-08-06 12:41:18 -04:00
|
|
|
assert_equal "one", HelloJob.new("1").queue_name
|
|
|
|
assert_equal "two", HelloJob.new("3").queue_name
|
2014-08-25 10:34:50 -04:00
|
|
|
ensure
|
|
|
|
HelloJob.queue_name = original_queue_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "queue_name_prefix prepended to the queue name with default delimiter" do
|
2014-10-10 02:53:22 -04:00
|
|
|
original_queue_name_prefix = ActiveJob::Base.queue_name_prefix
|
|
|
|
original_queue_name = HelloJob.queue_name
|
2014-08-18 04:17:36 -04:00
|
|
|
|
2014-10-10 02:53:22 -04:00
|
|
|
begin
|
2016-08-06 12:41:18 -04:00
|
|
|
ActiveJob::Base.queue_name_prefix = "aj"
|
2014-08-18 04:17:36 -04:00
|
|
|
HelloJob.queue_as :low
|
2016-08-06 12:41:18 -04:00
|
|
|
assert_equal "aj_low", HelloJob.queue_name
|
2014-08-18 04:17:36 -04:00
|
|
|
ensure
|
|
|
|
ActiveJob::Base.queue_name_prefix = original_queue_name_prefix
|
|
|
|
HelloJob.queue_name = original_queue_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "queue_name_prefix prepended to the queue name with custom delimiter" do
|
2014-09-23 16:51:44 -04:00
|
|
|
original_queue_name_prefix = ActiveJob::Base.queue_name_prefix
|
|
|
|
original_queue_name_delimiter = ActiveJob::Base.queue_name_delimiter
|
|
|
|
original_queue_name = HelloJob.queue_name
|
|
|
|
|
|
|
|
begin
|
2016-08-06 12:41:18 -04:00
|
|
|
ActiveJob::Base.queue_name_delimiter = "."
|
|
|
|
ActiveJob::Base.queue_name_prefix = "aj"
|
2014-09-23 16:51:44 -04:00
|
|
|
HelloJob.queue_as :low
|
2016-08-06 12:41:18 -04:00
|
|
|
assert_equal "aj.low", HelloJob.queue_name
|
2014-09-23 16:51:44 -04:00
|
|
|
ensure
|
|
|
|
ActiveJob::Base.queue_name_prefix = original_queue_name_prefix
|
|
|
|
ActiveJob::Base.queue_name_delimiter = original_queue_name_delimiter
|
|
|
|
HelloJob.queue_name = original_queue_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-03 22:39:18 -04:00
|
|
|
test "using a custom default_queue_name" do
|
|
|
|
original_default_queue_name = ActiveJob::Base.default_queue_name
|
|
|
|
|
|
|
|
begin
|
|
|
|
ActiveJob::Base.default_queue_name = "default_queue_name"
|
|
|
|
|
|
|
|
assert_equal "default_queue_name", HelloJob.new.queue_name
|
|
|
|
ensure
|
|
|
|
ActiveJob::Base.default_queue_name = original_default_queue_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "queue_name_prefix prepended to the default_queue_name" do
|
|
|
|
original_queue_name_prefix = ActiveJob::Base.queue_name_prefix
|
|
|
|
original_default_queue_name = ActiveJob::Base.default_queue_name
|
|
|
|
|
|
|
|
begin
|
|
|
|
ActiveJob::Base.queue_name_prefix = "prefix"
|
|
|
|
ActiveJob::Base.default_queue_name = "default_queue_name"
|
|
|
|
|
|
|
|
assert_equal "prefix_default_queue_name", HelloJob.new.queue_name
|
|
|
|
ensure
|
|
|
|
ActiveJob::Base.queue_name_prefix = original_queue_name_prefix
|
|
|
|
ActiveJob::Base.default_queue_name = original_default_queue_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-11 20:20:09 -04:00
|
|
|
test "can change queue_name_prefix in a job class definition without affecting other jobs" do
|
|
|
|
assert_equal "production", PrefixedJob.queue_name_prefix
|
|
|
|
assert_nil HelloJob.queue_name_prefix
|
|
|
|
end
|
|
|
|
|
|
|
|
test "can change queue_name_prefix in a job class without affecting other jobs" do
|
|
|
|
original_prefix = PrefixedJob.queue_name_prefix
|
|
|
|
|
|
|
|
begin
|
|
|
|
PrefixedJob.queue_name_prefix = "staging"
|
|
|
|
|
|
|
|
assert_equal "staging", PrefixedJob.queue_name_prefix
|
|
|
|
assert_nil HelloJob.queue_name_prefix
|
|
|
|
ensure
|
|
|
|
PrefixedJob.queue_name_prefix = original_prefix
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:41:18 -04:00
|
|
|
test "uses queue passed to #set" do
|
2014-08-25 10:34:50 -04:00
|
|
|
job = HelloJob.set(queue: :some_queue).perform_later
|
|
|
|
assert_equal "some_queue", job.queue_name
|
|
|
|
end
|
2014-05-18 10:24:30 -04:00
|
|
|
end
|