File: exceptions_test.rb

package info (click to toggle)
rails 2%3A7.2.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,348 kB
  • sloc: ruby: 349,797; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (383 lines) | stat: -rw-r--r-- 15,155 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# frozen_string_literal: true

require "helper"
require "jobs/retry_job"
require "jobs/after_discard_retry_job"
require "models/person"
require "minitest/mock"

class ExceptionsTest < ActiveSupport::TestCase
  class << self
    def adapter_skips_scheduling?(queue_adapter)
      [
        ActiveJob::QueueAdapters::InlineAdapter,
        ActiveJob::QueueAdapters::AsyncAdapter
#        ActiveJob::QueueAdapters::SneakersAdapter
      ].include?(queue_adapter.class)
    end
  end

  unless adapter_skips_scheduling?(ActiveJob::Base.queue_adapter)
    setup do
      JobBuffer.clear
    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)
      end

      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"
      ], JobBuffer.values
    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
      random_amount = 1
      delay_for_jitter = random_amount * 3600 * ActiveJob::Base.retry_jitter

      Kernel.stub(:rand, random_amount) do
        RetryJob.perform_later "LongWaitError", 2, :log_scheduled_at
        assert_equal [
          "Raised LongWaitError for the 1st time",
          "Next execution scheduled at #{(Time.now + 3600.seconds + delay_for_jitter).to_f}",
          "Successfully completed job"
        ], JobBuffer.values
      end
    end

    test "polynomially retrying job includes jitter" do
      travel_to Time.now

      random_amount = 2
      delay_for_jitter = -> (delay) { random_amount * delay * ActiveJob::Base.retry_jitter }

      Kernel.stub(:rand, random_amount) do
        RetryJob.perform_later "PolynomialWaitTenAttemptsError", 5, :log_scheduled_at

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

    test "retry jitter uses value from ActiveJob::Base.retry_jitter by default" do
      old_jitter = ActiveJob::Base.retry_jitter
      ActiveJob::Base.retry_jitter = 4.0

      travel_to Time.now

      random_amount = 1

      Kernel.stub(:rand, random_amount) do
        RetryJob.perform_later "PolynomialWaitTenAttemptsError", 5, :log_scheduled_at

        assert_equal [
          "Raised PolynomialWaitTenAttemptsError for the 1st time",
          "Next execution scheduled at #{(Time.now + 7.seconds).to_f}",
          "Raised PolynomialWaitTenAttemptsError for the 2nd time",
          "Next execution scheduled at #{(Time.now + 82.seconds).to_f}",
          "Raised PolynomialWaitTenAttemptsError for the 3rd time",
          "Next execution scheduled at #{(Time.now + 407.seconds).to_f}",
          "Raised PolynomialWaitTenAttemptsError for the 4th time",
          "Next execution scheduled at #{(Time.now + 1282.seconds).to_f}",
          "Successfully completed job"
        ], JobBuffer.values
      end
    ensure
      ActiveJob::Base.retry_jitter = old_jitter
    end

    test "random wait time for default job when retry jitter delay multiplier value is between 1 and 2" do
      old_jitter = ActiveJob::Base.retry_jitter
      ActiveJob::Base.retry_jitter = 0.6

      travel_to Time.now

      RetryJob.perform_later "DefaultsError", 2, :log_scheduled_at

      assert_not_equal [
        "Raised DefaultsError for the 1st time",
        "Next execution scheduled at #{(Time.now + 3.seconds).to_f}",
        "Successfully completed job"
      ], JobBuffer.values
    ensure
      ActiveJob::Base.retry_jitter = old_jitter
    end

    test "random wait time for polynomially retrying job when retry jitter delay multiplier value is between 1 and 2" do
      old_jitter = ActiveJob::Base.retry_jitter
      ActiveJob::Base.retry_jitter = 1.2

      travel_to Time.now

      RetryJob.perform_later "PolynomialWaitTenAttemptsError", 2, :log_scheduled_at

      assert_not_equal [
        "Raised PolynomialWaitTenAttemptsError for the 1st time",
        "Next execution scheduled at #{(Time.now + 3.seconds).to_f}",
        "Successfully completed job"
      ], JobBuffer.values
    ensure
      ActiveJob::Base.retry_jitter = old_jitter
    end

    test "random wait time for negative jitter value" do
      old_jitter = ActiveJob::Base.retry_jitter
      ActiveJob::Base.retry_jitter = -1.2

      travel_to Time.now

      RetryJob.perform_later "PolynomialWaitTenAttemptsError", 2, :log_scheduled_at

      assert_not_equal [
        "Raised PolynomialWaitTenAttemptsError for the 1st time",
        "Next execution scheduled at #{(Time.now + 3.seconds).to_f}",
        "Successfully completed job"
      ], JobBuffer.values
    ensure
      ActiveJob::Base.retry_jitter = old_jitter
    end

    test "retry jitter disabled with nil" do
      travel_to Time.now

      RetryJob.perform_later "DisabledJitterError", 3, :log_scheduled_at

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

    test "retry jitter disabled with zero" do
      travel_to Time.now

      RetryJob.perform_later "ZeroJitterError", 3, :log_scheduled_at

      assert_equal [
        "Raised ZeroJitterError for the 1st time",
        "Next execution scheduled at #{(Time.now + 3.seconds).to_f}",
        "Raised ZeroJitterError for the 2nd time",
        "Next execution scheduled at #{(Time.now + 3.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(PolynomialWaitTenAttemptsError CustomWaitTenAttemptsError PolynomialWaitTenAttemptsError CustomWaitTenAttemptsError)

      random_amount = 1

      Kernel.stub(:rand, random_amount) do
        RetryJob.perform_later exceptions_to_raise, 5, :log_scheduled_at

        delay_for_jitter = -> (delay) { random_amount * delay * ActiveJob::Base.retry_jitter }

        assert_equal [
          "Raised PolynomialWaitTenAttemptsError for the 1st time",
          "Next execution scheduled at #{(Time.now + 3.seconds + delay_for_jitter.(1)).to_f}",
          "Raised CustomWaitTenAttemptsError for the 2nd time",
          "Next execution scheduled at #{(Time.now + 2.seconds).to_f}",
          "Raised PolynomialWaitTenAttemptsError for the 3rd time",
          "Next execution scheduled at #{(Time.now + 18.seconds + delay_for_jitter.(16)).to_f}",
          "Raised CustomWaitTenAttemptsError for the 4th time",
          "Next execution scheduled at #{(Time.now + 4.seconds).to_f}",
          "Successfully completed job"
        ], JobBuffer.values
      end
    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

    test "successfully retry job throwing UnlimitedRetryError a few times" do
      RetryJob.perform_later "UnlimitedRetryError", 10

      assert_equal 10, JobBuffer.values.size
      assert_equal "Raised UnlimitedRetryError for the 9th time", JobBuffer.values[8]
      assert_equal "Successfully completed job", JobBuffer.values[9]
    end

    test "running a job enqueued by AJ 5.2" do
      job = RetryJob.new("DefaultsError", 6)
      job.exception_executions = nil # This is how jobs from Rails 5.2 will look

      assert_raises DefaultsError do
        job.enqueue
      end

      assert_equal 5, JobBuffer.values.count
    end

    test "running a job enqueued and attempted under AJ 5.2" do
      job = RetryJob.new("DefaultsError", 6)

      # Fake 4 previous executions under AJ 5.2
      job.exception_executions = nil
      job.executions = 4

      assert_raises DefaultsError do
        job.enqueue
      end

      assert_equal ["Raised DefaultsError for the 5th time"], JobBuffer.values
    end

    test "#after_discard block is run when an unhandled error is raised" do
      assert_raises(AfterDiscardRetryJob::UnhandledError) do
        AfterDiscardRetryJob.perform_later("AfterDiscardRetryJob::UnhandledError", 2)
      end

      assert_equal "Ran after_discard for job. Message: AfterDiscardRetryJob::UnhandledError", JobBuffer.last_value
    end

    test "#after_discard block is run when #retry_on is passed a block" do
      AfterDiscardRetryJob.perform_later("AfterDiscardRetryJob::CustomCatchError", 6)

      assert_equal "Ran after_discard for job. Message: AfterDiscardRetryJob::CustomCatchError", JobBuffer.last_value
    end

    test "#after_discard block is only run once when an error class and its superclass are handled by separate #retry_on calls" do
      assert_raises(AfterDiscardRetryJob::ChildAfterDiscardError) do
        AfterDiscardRetryJob.perform_later("AfterDiscardRetryJob::ChildAfterDiscardError", 6)
      end
      assert_equal ["Raised AfterDiscardRetryJob::ChildAfterDiscardError for the 5th time", "Ran after_discard for job. Message: AfterDiscardRetryJob::ChildAfterDiscardError"], JobBuffer.values.last(2)
    end

    test "#after_discard is run when a job is discarded via #discard_on" do
      AfterDiscardRetryJob.perform_later("AfterDiscardRetryJob::DiscardableError", 2)

      assert_equal "Ran after_discard for job. Message: AfterDiscardRetryJob::DiscardableError", JobBuffer.last_value
    end

    test "#after_discard is run when a job is discarded via #discard_on with a block passed to #discard_on" do
      AfterDiscardRetryJob.perform_later("AfterDiscardRetryJob::CustomDiscardableError", 2)

      expected_array = [
        "Dealt with a job that was discarded in a custom way. Message: AfterDiscardRetryJob::CustomDiscardableError",
        "Ran after_discard for job. Message: AfterDiscardRetryJob::CustomDiscardableError"
      ]
      assert_equal expected_array, JobBuffer.values.last(2)
    end
  end
end