aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/test/cases/queue_naming_test.rb
blob: 4b43c7c3c52ec9619c334af093083fd4d261db76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true

require "helper"
require "jobs/hello_job"
require "jobs/logging_job"
require "jobs/nested_job"

class QueueNamingTest < ActiveSupport::TestCase
  test "name derived from base" do
    assert_equal "default", HelloJob.new.queue_name
  end

  test "uses given queue name job" do
    original_queue_name = HelloJob.queue_name

    begin
      HelloJob.queue_as :greetings
      assert_equal "greetings", HelloJob.new.queue_name
    ensure
      HelloJob.queue_name = original_queue_name
    end
  end

  test "allows a blank queue name" do
    original_queue_name = HelloJob.queue_name

    begin
      HelloJob.queue_as ""
      assert_equal "", HelloJob.new.queue_name
    ensure
      HelloJob.queue_name = original_queue_name
    end
  end

  test "does not use a nil queue name" do
    original_queue_name = HelloJob.queue_name

    begin
      HelloJob.queue_as nil
      assert_equal "default", HelloJob.new.queue_name
    ensure
      HelloJob.queue_name = original_queue_name
    end
  end

  test "evals block given to queue_as to determine queue" do
    original_queue_name = HelloJob.queue_name

    begin
      HelloJob.queue_as { :another }
      assert_equal "another", HelloJob.new.queue_name
    ensure
      HelloJob.queue_name = original_queue_name
    end
  end

  test "can use arguments to determine queue_name in queue_as block" do
    original_queue_name = HelloJob.queue_name

    begin
      HelloJob.queue_as { arguments.first == "1" ? :one : :two }
      assert_equal "one", HelloJob.new("1").queue_name
      assert_equal "two", HelloJob.new("3").queue_name
    ensure
      HelloJob.queue_name = original_queue_name
    end
  end

  test "queue_name_prefix prepended to the queue name with default delimiter" do
    original_queue_name_prefix = ActiveJob::Base.queue_name_prefix
    original_queue_name = HelloJob.queue_name

    begin
      ActiveJob::Base.queue_name_prefix = "aj"
      HelloJob.queue_as :low
      assert_equal "aj_low", HelloJob.queue_name
    ensure
      ActiveJob::Base.queue_name_prefix = original_queue_name_prefix
      HelloJob.queue_name = original_queue_name
    end
  end

  test "queue_name_prefix prepended to the queue name with custom delimiter" do
    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
      ActiveJob::Base.queue_name_delimiter = "."
      ActiveJob::Base.queue_name_prefix = "aj"
      HelloJob.queue_as :low
      assert_equal "aj.low", HelloJob.queue_name
    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

  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

  test "uses queue passed to #set" do
    job = HelloJob.set(queue: :some_queue).perform_later
    assert_equal "some_queue", job.queue_name
  end
end