aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/test/cases/exceptions_test.rb
blob: 840f4d40b58307a917a521b92fb8eb7b7d01c949 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# frozen_string_literal: true

require "helper"
require "jobs/retry_job"
require "models/person"

class ExceptionsTest < ActiveSupport::TestCase
  setup do
    JobBuffer.clear
    skip if adapter_skips_scheduling?(ActiveJob::Base.queue_adapter)
  end

  test "successfully retry job throwing exception against defaults" do
    RetryJob.perform_later "DefaultsError", 5

    assert_equal [
      "Raised DefaultsError for the 1st time",
      "Raised DefaultsError for the 2nd time",
      "Raised DefaultsError for the 3rd time",
      "Raised DefaultsError for the 4th time",
      "Successfully completed job" ], JobBuffer.values
  end

  test "successfully retry job throwing exception against higher limit" do
    RetryJob.perform_later "ShortWaitTenAttemptsError", 9
    assert_equal 9, JobBuffer.values.count
  end

  test "keeps the same attempts counter for several exceptions listed in the same retry_on declaration" do
    exceptions_to_raise = %w(FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo
                             SecondRetryableErrorOfTwo SecondRetryableErrorOfTwo)

    assert_raises SecondRetryableErrorOfTwo do
      RetryJob.perform_later(exceptions_to_raise, 5)

      assert_equal [
        "Raised FirstRetryableErrorOfTwo for the 1st time",
        "Raised FirstRetryableErrorOfTwo for the 2nd time",
        "Raised FirstRetryableErrorOfTwo for the 3rd time",
        "Raised SecondRetryableErrorOfTwo for the 4th time",
        "Raised SecondRetryableErrorOfTwo for the 5th time",
      ], JobBuffer.values
    end
  end

  test "keeps a separate attempts counter for each individual retry_on declaration" do
    exceptions_to_raise = %w(DefaultsError DefaultsError DefaultsError DefaultsError
                             FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo FirstRetryableErrorOfTwo)

    assert_nothing_raised do
      RetryJob.perform_later(exceptions_to_raise, 10)

      assert_equal [
        "Raised DefaultsError for the 1st time",
        "Raised DefaultsError for the 2nd time",
        "Raised DefaultsError for the 3rd time",
        "Raised DefaultsError for the 4th time",
        "Raised FirstRetryableErrorOfTwo for the 5th time",
        "Raised FirstRetryableErrorOfTwo for the 6th time",
        "Raised FirstRetryableErrorOfTwo for the 7th time",
        "Successfully completed job"
      ], JobBuffer.values
    end
  end

  test "failed retry job when exception kept occurring against defaults" do
    RetryJob.perform_later "DefaultsError", 6
    assert_equal "Raised DefaultsError for the 5th time", JobBuffer.last_value
  rescue DefaultsError
    pass
  end

  test "failed retry job when exception kept occurring against higher limit" do
    RetryJob.perform_later "ShortWaitTenAttemptsError", 11
    assert_equal "Raised ShortWaitTenAttemptsError for the 10th time", JobBuffer.last_value
  rescue ShortWaitTenAttemptsError
    pass
  end

  test "discard job" do
    RetryJob.perform_later "DiscardableError", 2
    assert_equal "Raised DiscardableError for the 1st time", JobBuffer.last_value
  end

  test "custom handling of discarded job" do
    RetryJob.perform_later "CustomDiscardableError", 2
    assert_equal "Dealt with a job that was discarded in a custom way. Message: CustomDiscardableError", JobBuffer.last_value
  end

  test "custom handling of job that exceeds retry attempts" do
    RetryJob.perform_later "CustomCatchError", 6
    assert_equal "Dealt with a job that failed to retry in a custom way after 6 attempts. Message: CustomCatchError", JobBuffer.last_value
  end

  test "long wait job" do
    travel_to Time.now

    RetryJob.perform_later "LongWaitError", 2, :log_scheduled_at

    assert_equal [
      "Raised LongWaitError for the 1st time",
      "Next execution scheduled at #{(Time.now + 3600.seconds).to_f}",
      "Successfully completed job"
    ], JobBuffer.values
  end

  test "exponentially retrying job" do
    travel_to Time.now

    RetryJob.perform_later "ExponentialWaitTenAttemptsError", 5, :log_scheduled_at

    assert_equal [
      "Raised ExponentialWaitTenAttemptsError for the 1st time",
      "Next execution scheduled at #{(Time.now + 3.seconds).to_f}",
      "Raised ExponentialWaitTenAttemptsError for the 2nd time",
      "Next execution scheduled at #{(Time.now + 18.seconds).to_f}",
      "Raised ExponentialWaitTenAttemptsError for the 3rd time",
      "Next execution scheduled at #{(Time.now + 83.seconds).to_f}",
      "Raised ExponentialWaitTenAttemptsError for the 4th time",
      "Next execution scheduled at #{(Time.now + 258.seconds).to_f}",
      "Successfully completed job"
    ], JobBuffer.values
  end

  test "custom wait retrying job" do
    travel_to Time.now

    RetryJob.perform_later "CustomWaitTenAttemptsError", 5, :log_scheduled_at

    assert_equal [
      "Raised CustomWaitTenAttemptsError for the 1st time",
      "Next execution scheduled at #{(Time.now + 2.seconds).to_f}",
      "Raised CustomWaitTenAttemptsError for the 2nd time",
      "Next execution scheduled at #{(Time.now + 4.seconds).to_f}",
      "Raised CustomWaitTenAttemptsError for the 3rd time",
      "Next execution scheduled at #{(Time.now + 6.seconds).to_f}",
      "Raised CustomWaitTenAttemptsError for the 4th time",
      "Next execution scheduled at #{(Time.now + 8.seconds).to_f}",
      "Successfully completed job"
    ], JobBuffer.values
  end

  test "use individual execution timers when calculating retry delay" do
    travel_to Time.now

    exceptions_to_raise = %w(ExponentialWaitTenAttemptsError CustomWaitTenAttemptsError ExponentialWaitTenAttemptsError CustomWaitTenAttemptsError)

    RetryJob.perform_later exceptions_to_raise, 5, :log_scheduled_at

    assert_equal [
      "Raised ExponentialWaitTenAttemptsError for the 1st time",
      "Next execution scheduled at #{(Time.now + 3.seconds).to_f}",
      "Raised CustomWaitTenAttemptsError for the 2nd time",
      "Next execution scheduled at #{(Time.now + 2.seconds).to_f}",
      "Raised ExponentialWaitTenAttemptsError for the 3rd time",
      "Next execution scheduled at #{(Time.now + 18.seconds).to_f}",
      "Raised CustomWaitTenAttemptsError for the 4th time",
      "Next execution scheduled at #{(Time.now + 4.seconds).to_f}",
      "Successfully completed job"
    ], JobBuffer.values
  end

  test "successfully retry job throwing one of two retryable exceptions" do
    RetryJob.perform_later "SecondRetryableErrorOfTwo", 3

    assert_equal [
      "Raised SecondRetryableErrorOfTwo for the 1st time",
      "Raised SecondRetryableErrorOfTwo for the 2nd time",
      "Successfully completed job" ], JobBuffer.values
  end

  test "discard job throwing one of two discardable exceptions" do
    RetryJob.perform_later "SecondDiscardableErrorOfTwo", 2
    assert_equal [ "Raised SecondDiscardableErrorOfTwo for the 1st time" ], JobBuffer.values
  end

  test "successfully retry job throwing DeserializationError" do
    RetryJob.perform_later Person.new(404), 5
    assert_equal ["Raised ActiveJob::DeserializationError for the 5 time"], JobBuffer.values
  end

  private
    def adapter_skips_scheduling?(queue_adapter)
      [
        ActiveJob::QueueAdapters::InlineAdapter,
        ActiveJob::QueueAdapters::AsyncAdapter,
        ActiveJob::QueueAdapters::SneakersAdapter
      ].include?(queue_adapter.class)
    end
end