55from fakeredis import FakeStrictRedis
66import rq
77
8+ try :
9+ from unittest import mock # python 3.3 and above
10+ except ImportError :
11+ import mock # python < 3.3
12+
813
914@pytest .fixture (autouse = True )
1015def _patch_rq_get_server_version (monkeypatch ):
@@ -28,6 +33,14 @@ def crashing_job(foo):
2833 1 / 0
2934
3035
36+ def chew_up_shoes (dog , human , shoes ):
37+ raise Exception ("{}!! Why did you eat {}'s {}??" .format (dog , human , shoes ))
38+
39+
40+ def do_trick (dog , trick ):
41+ return "{}, can you {}? Good dog!" .format (dog , trick )
42+
43+
3144def test_basic (sentry_init , capture_events ):
3245 sentry_init (integrations = [RqIntegration ()])
3346 events = capture_events ()
@@ -71,3 +84,96 @@ def test_transport_shutdown(sentry_init, capture_events_forksafe):
7184
7285 (exception ,) = event ["exception" ]["values" ]
7386 assert exception ["type" ] == "ZeroDivisionError"
87+
88+
89+ def test_transaction_with_error (
90+ sentry_init , capture_events , DictionaryContaining # noqa:N803
91+ ):
92+
93+ sentry_init (integrations = [RqIntegration ()], traces_sample_rate = 1.0 )
94+ events = capture_events ()
95+
96+ queue = rq .Queue (connection = FakeStrictRedis ())
97+ worker = rq .SimpleWorker ([queue ], connection = queue .connection )
98+
99+ queue .enqueue (chew_up_shoes , "Charlie" , "Katie" , shoes = "flip-flops" )
100+ worker .work (burst = True )
101+
102+ error_event , envelope = events
103+
104+ assert error_event ["transaction" ] == "tests.integrations.rq.test_rq.chew_up_shoes"
105+ assert error_event ["contexts" ]["trace" ]["op" ] == "rq.task"
106+ assert error_event ["exception" ]["values" ][0 ]["type" ] == "Exception"
107+ assert (
108+ error_event ["exception" ]["values" ][0 ]["value" ]
109+ == "Charlie!! Why did you eat Katie's flip-flops??"
110+ )
111+
112+ assert envelope ["type" ] == "transaction"
113+ assert envelope ["contexts" ]["trace" ] == error_event ["contexts" ]["trace" ]
114+ assert envelope ["transaction" ] == error_event ["transaction" ]
115+ assert envelope ["extra" ]["rq-job" ] == DictionaryContaining (
116+ {
117+ "args" : ["Charlie" , "Katie" ],
118+ "kwargs" : {"shoes" : "flip-flops" },
119+ "func" : "tests.integrations.rq.test_rq.chew_up_shoes" ,
120+ "description" : "tests.integrations.rq.test_rq.chew_up_shoes('Charlie', 'Katie', shoes='flip-flops')" ,
121+ }
122+ )
123+
124+
125+ def test_transaction_no_error (
126+ sentry_init , capture_events , DictionaryContaining # noqa:N803
127+ ):
128+ sentry_init (integrations = [RqIntegration ()], traces_sample_rate = 1.0 )
129+ events = capture_events ()
130+
131+ queue = rq .Queue (connection = FakeStrictRedis ())
132+ worker = rq .SimpleWorker ([queue ], connection = queue .connection )
133+
134+ queue .enqueue (do_trick , "Maisey" , trick = "kangaroo" )
135+ worker .work (burst = True )
136+
137+ envelope = events [0 ]
138+
139+ assert envelope ["type" ] == "transaction"
140+ assert envelope ["contexts" ]["trace" ]["op" ] == "rq.task"
141+ assert envelope ["transaction" ] == "tests.integrations.rq.test_rq.do_trick"
142+ assert envelope ["extra" ]["rq-job" ] == DictionaryContaining (
143+ {
144+ "args" : ["Maisey" ],
145+ "kwargs" : {"trick" : "kangaroo" },
146+ "func" : "tests.integrations.rq.test_rq.do_trick" ,
147+ "description" : "tests.integrations.rq.test_rq.do_trick('Maisey', trick='kangaroo')" ,
148+ }
149+ )
150+
151+
152+ def test_traces_sampler_gets_correct_values_in_sampling_context (
153+ sentry_init , DictionaryContaining , ObjectDescribedBy # noqa:N803
154+ ):
155+ traces_sampler = mock .Mock (return_value = True )
156+ sentry_init (integrations = [RqIntegration ()], traces_sampler = traces_sampler )
157+
158+ queue = rq .Queue (connection = FakeStrictRedis ())
159+ worker = rq .SimpleWorker ([queue ], connection = queue .connection )
160+
161+ queue .enqueue (do_trick , "Bodhi" , trick = "roll over" )
162+ worker .work (burst = True )
163+
164+ traces_sampler .assert_any_call (
165+ DictionaryContaining (
166+ {
167+ "rq_job" : ObjectDescribedBy (
168+ type = rq .job .Job ,
169+ attrs = {
170+ "description" : "tests.integrations.rq.test_rq.do_trick('Bodhi', trick='roll over')" ,
171+ "result" : "Bodhi, can you roll over? Good dog!" ,
172+ "func_name" : "tests.integrations.rq.test_rq.do_trick" ,
173+ "args" : ("Bodhi" ,),
174+ "kwargs" : {"trick" : "roll over" },
175+ },
176+ ),
177+ }
178+ )
179+ )
0 commit comments