1
1
# frozen_string_literal: true
2
2
3
- require "spec_helper"
4
- require "active_job/railtie"
5
-
6
- class NormalJob < ActiveJob ::Base
7
- def perform
8
- "foo"
9
- end
10
- end
11
-
12
- class FailedJob < ActiveJob ::Base
13
- self . logger = nil
14
-
15
- class TestError < RuntimeError
16
- end
17
-
18
- def perform
19
- a = 1
20
- b = 0
21
- raise TestError , "Boom!"
22
- end
23
- end
24
-
25
- class FailedWithExtraJob < FailedJob
26
- def perform
27
- Sentry . get_current_scope . set_extras ( foo : :bar )
28
- super
29
- end
30
- end
31
-
32
- class JobWithArgument < ActiveJob ::Base
33
- def perform ( *args , integer :, post :, **options )
34
- raise "foo"
35
- end
36
- end
37
-
38
- class QueryPostJob < ActiveJob ::Base
39
- self . logger = nil
40
-
41
- def perform
42
- Post . all . to_a
43
- end
44
- end
45
-
46
- class RescuedActiveJob < FailedWithExtraJob
47
- rescue_from TestError , with : :rescue_callback
48
-
49
- def rescue_callback ( error ) ; end
50
- end
51
-
52
- class ProblematicRescuedActiveJob < FailedWithExtraJob
53
- rescue_from TestError , with : :rescue_callback
54
-
55
- def rescue_callback ( error )
56
- raise "foo"
57
- end
58
- end
59
-
60
- class NormalJobWithCron < NormalJob
61
- include Sentry ::Cron ::MonitorCheckIns
62
- sentry_monitor_check_ins
63
- end
64
-
65
- class FailedJobWithCron < FailedJob
66
- include Sentry ::Cron ::MonitorCheckIns
67
- sentry_monitor_check_ins slug : "failed_job" , monitor_config : Sentry ::Cron ::MonitorConfig . from_crontab ( "5 * * * *" )
68
- end
69
-
70
- class FailedJobWithRetryOn < FailedJob
71
- if respond_to? :retry_on
72
- retry_on StandardError , attempts : 3 , wait : 0
73
- end
74
- end
3
+ require_relative "../../support/test_jobs"
75
4
76
5
RSpec . describe "without Sentry initialized" , type : :job do
77
6
it "runs job" do
@@ -109,7 +38,10 @@ class FailedJobWithRetryOn < FailedJob
109
38
JobWithArgument . perform_now ( "foo" , { bar : Sentry } , integer : 1 , post : post , nested : { another_level : { post : post2 } } )
110
39
end . to raise_error ( RuntimeError )
111
40
41
+ expect ( transport . events . size ) . to be ( 1 )
42
+
112
43
event = transport . events . last . to_json_compatible
44
+
113
45
expect ( event . dig ( "extra" , "arguments" ) ) . to eq (
114
46
[
115
47
"foo" ,
@@ -134,7 +66,10 @@ def post.to_global_id
134
66
JobWithArgument . perform_now ( integer : 1 , post : post )
135
67
end . to raise_error ( RuntimeError )
136
68
69
+ expect ( transport . events . size ) . to be ( 1 )
70
+
137
71
event = transport . events . last . to_json_compatible
72
+
138
73
expect ( event . dig ( "extra" , "arguments" ) ) . to eq (
139
74
[
140
75
{
@@ -152,7 +87,10 @@ def post.to_global_id
152
87
JobWithArgument . perform_now ( "foo" , { bar : Sentry } , integer : 1 , post : post , range : 1 ..3 )
153
88
end . to raise_error ( RuntimeError )
154
89
90
+ expect ( transport . events . size ) . to be ( 1 )
91
+
155
92
event = transport . events . last . to_json_compatible
93
+
156
94
expect ( event . dig ( "extra" , "arguments" ) ) . to eq (
157
95
[
158
96
"foo" ,
@@ -174,7 +112,10 @@ def post.to_global_id
174
112
JobWithArgument . perform_now ( "foo" , { bar : Sentry } , integer : 1 , post : post , range : range )
175
113
end . to raise_error ( RuntimeError )
176
114
115
+ expect ( transport . events . size ) . to be ( 1 )
116
+
177
117
event = transport . events . last . to_json_compatible
118
+
178
119
expect ( event . dig ( "extra" , "arguments" ) ) . to eq (
179
120
[
180
121
"foo" ,
@@ -192,7 +133,10 @@ def post.to_global_id
192
133
it "adds useful context to extra" do
193
134
expect { FailedJob . perform_now } . to raise_error ( FailedJob ::TestError )
194
135
136
+ expect ( transport . events . size ) . to be ( 1 )
137
+
195
138
event = transport . events . last . to_json_compatible
139
+
196
140
expect ( event . dig ( "extra" , "active_job" ) ) . to eq ( "FailedJob" )
197
141
expect ( event . dig ( "extra" , "job_id" ) ) . to be_a ( String )
198
142
expect ( event . dig ( "extra" , "provider_job_id" ) ) . to be_nil
@@ -207,6 +151,8 @@ def post.to_global_id
207
151
it "clears context" do
208
152
expect { FailedWithExtraJob . perform_now } . to raise_error ( FailedWithExtraJob ::TestError )
209
153
154
+ expect ( transport . events . size ) . to be ( 1 )
155
+
210
156
event = transport . events . last . to_json_compatible
211
157
212
158
expect ( event [ "extra" ] [ "foo" ] ) . to eq ( "bar" )
@@ -224,7 +170,8 @@ def post.to_global_id
224
170
it "sends transaction" do
225
171
QueryPostJob . perform_now
226
172
227
- expect ( transport . events . count ) . to eq ( 1 )
173
+ expect ( transport . events . size ) . to be ( 1 )
174
+
228
175
transaction = transport . events . last
229
176
expect ( transaction . transaction ) . to eq ( "QueryPostJob" )
230
177
expect ( transaction . transaction_info ) . to eq ( { source : :task } )
@@ -242,7 +189,7 @@ def post.to_global_id
242
189
it "sends transaction and associates it with the event" do
243
190
expect { FailedWithExtraJob . perform_now } . to raise_error ( FailedWithExtraJob ::TestError )
244
191
245
- expect ( transport . events . count ) . to eq ( 2 )
192
+ expect ( transport . events . size ) . to be ( 2 )
246
193
247
194
transaction = transport . events . first
248
195
expect ( transaction . transaction ) . to eq ( "FailedWithExtraJob" )
@@ -277,7 +224,7 @@ def perform
277
224
DeserializationErrorJob . perform_now
278
225
end . to raise_error ( ActiveJob ::DeserializationError , /divided by 0/ )
279
226
280
- expect ( transport . events . size ) . to eq ( 1 )
227
+ expect ( transport . events . size ) . to be ( 1 )
281
228
282
229
event = transport . events . last . to_json_compatible
283
230
expect ( event . dig ( "exception" , "values" , 0 , "type" ) ) . to eq ( "ZeroDivisionError" )
@@ -434,14 +381,22 @@ def perform(event, hint)
434
381
end
435
382
end
436
383
437
- describe "active_job_report_after_job_retries " , skip : RAILS_VERSION < 7.0 do
384
+ describe "Reporting on retry errors " , skip : RAILS_VERSION < 7.0 do
438
385
before do
439
- if defined? ( JRUBY_VERSION ) && JRUBY_VERSION == "9.4.12.0" && RAILS_VERSION <= 7.1
386
+ if defined? ( JRUBY_VERSION ) && JRUBY_VERSION == "9.4.12.0"
440
387
skip "This crashes on jruby + rails 7.0.0.x. See https://github.com/getsentry/sentry-ruby/issues/2612"
441
388
end
442
389
end
443
390
444
- context "when active_job_report_after_job_retries is false" do
391
+ context "when active_job_report_on_retry_error is true" do
392
+ before do
393
+ Sentry . configuration . rails . active_job_report_on_retry_error = true
394
+ end
395
+
396
+ after do
397
+ Sentry . configuration . rails . active_job_report_on_retry_error = false
398
+ end
399
+
445
400
it "reports 3 exceptions" do
446
401
allow ( Sentry ::Rails ::ActiveJobExtensions ::SentryReporter )
447
402
. to receive ( :capture_exception ) . and_call_original
@@ -458,16 +413,8 @@ def perform(event, hint)
458
413
end
459
414
end
460
415
461
- context "when active_job_report_after_job_retries is true" do
462
- before do
463
- Sentry . configuration . rails . active_job_report_after_job_retries = true
464
- end
465
-
466
- after do
467
- Sentry . configuration . rails . active_job_report_after_job_retries = false
468
- end
469
-
470
- it "reports 1 exception" do
416
+ context "when active_job_report_on_retry_error is false" do
417
+ it "reports 1 exception on final attempt failure" do
471
418
allow ( Sentry ::Rails ::ActiveJobExtensions ::SentryReporter )
472
419
. to receive ( :capture_exception ) . and_call_original
473
420
0 commit comments