2
2
from unittest import mock
3
3
4
4
import msgpack
5
+ import pytest
5
6
from arroyo .backends .kafka import KafkaPayload
6
7
from django .test import override_settings
7
8
from django .utils import timezone
20
21
check_missing ,
21
22
check_timeout ,
22
23
clock_pulse ,
24
+ mark_environment_missing ,
23
25
try_monitor_tasks_trigger ,
24
26
)
25
27
from sentry .testutils .cases import TestCase
@@ -48,7 +50,8 @@ def make_ref_time():
48
50
49
51
50
52
class MonitorTaskCheckMissingTest (TestCase ):
51
- def test_missing_checkin (self ):
53
+ @mock .patch ("sentry.monitors.tasks.mark_environment_missing" )
54
+ def test_missing_checkin (self , mark_environment_missing_mock ):
52
55
org = self .create_organization ()
53
56
project = self .create_project (organization = org )
54
57
@@ -78,6 +81,14 @@ def test_missing_checkin(self):
78
81
79
82
check_missing (task_run_ts )
80
83
84
+ # assert that task is called for the specific environment
85
+ assert mark_environment_missing_mock .delay .call_count == 1
86
+ assert mark_environment_missing_mock .delay .mock_calls [0 ] == mock .call (
87
+ monitor_environment .id
88
+ )
89
+
90
+ mark_environment_missing (monitor_environment .id )
91
+
81
92
# Monitor status is updated
82
93
monitor_environment = MonitorEnvironment .objects .get (
83
94
id = monitor_environment .id , status = MonitorStatus .MISSED_CHECKIN
@@ -98,7 +109,8 @@ def test_missing_checkin(self):
98
109
).replace (second = 0 , microsecond = 0 )
99
110
assert missed_checkin .monitor_config == monitor .config
100
111
101
- def test_missing_checkin_with_margin (self ):
112
+ @mock .patch ("sentry.monitors.tasks.mark_environment_missing" )
113
+ def test_missing_checkin_with_margin (self , mark_environment_missing_mock ):
102
114
org = self .create_organization ()
103
115
project = self .create_project (organization = org )
104
116
@@ -132,6 +144,9 @@ def test_missing_checkin_with_margin(self):
132
144
# No missed check-in generated as we're still within the check-in margin
133
145
check_missing (task_run_ts )
134
146
147
+ # assert that task is not called for the specific environment
148
+ assert mark_environment_missing_mock .delay .call_count == 0
149
+
135
150
assert not MonitorEnvironment .objects .filter (
136
151
id = monitor_environment .id , status = MonitorStatus .MISSED_CHECKIN
137
152
).exists ()
@@ -142,6 +157,14 @@ def test_missing_checkin_with_margin(self):
142
157
# Missed check-in generated as clock now exceeds expected time plus margin
143
158
check_missing (task_run_ts + timedelta (minutes = 4 ))
144
159
160
+ # assert that task is called for the specific environment
161
+ assert mark_environment_missing_mock .delay .call_count == 1
162
+ assert mark_environment_missing_mock .delay .mock_calls [0 ] == mock .call (
163
+ monitor_environment .id
164
+ )
165
+
166
+ mark_environment_missing (monitor_environment .id )
167
+
145
168
monitor_environment = MonitorEnvironment .objects .get (
146
169
id = monitor_environment .id , status = MonitorStatus .MISSED_CHECKIN
147
170
)
@@ -157,7 +180,7 @@ def test_missing_checkin_with_margin(self):
157
180
)
158
181
159
182
# Missed checkins are back-dated to when the checkin was expected to
160
- # happpen . In this case the expected_time is equal to the date_added.
183
+ # happen . In this case the expected_time is equal to the date_added.
161
184
assert missed_check .date_added == (
162
185
monitor_environment .last_checkin + timedelta (minutes = 10 )
163
186
).replace (second = 0 , microsecond = 0 )
@@ -269,8 +292,8 @@ def test_not_missing_checkin(self):
269
292
monitor_environment = monitor_environment .id , status = CheckInStatus .MISSED
270
293
).exists ()
271
294
272
- @mock .patch ("sentry.monitors.tasks.logger " )
273
- def test_missed_exception_handling (self , logger ):
295
+ @mock .patch ("sentry.monitors.tasks.mark_environment_missing " )
296
+ def test_missed_exception_handling (self , mark_environment_missing_mock ):
274
297
org = self .create_organization ()
275
298
project = self .create_project (organization = org )
276
299
@@ -287,7 +310,7 @@ def test_missed_exception_handling(self, logger):
287
310
"schedule" : [- 2 , "minute" ],
288
311
},
289
312
)
290
- MonitorEnvironment .objects .create (
313
+ failing_monitor_environment = MonitorEnvironment .objects .create (
291
314
monitor = exception_monitor ,
292
315
environment = self .environment ,
293
316
next_checkin = ts - timedelta (minutes = 1 ),
@@ -301,7 +324,7 @@ def test_missed_exception_handling(self, logger):
301
324
type = MonitorType .CRON_JOB ,
302
325
config = {"schedule" : "* * * * *" },
303
326
)
304
- monitor_environment = MonitorEnvironment .objects .create (
327
+ successful_monitor_environment = MonitorEnvironment .objects .create (
305
328
monitor = monitor ,
306
329
environment = self .environment ,
307
330
next_checkin = ts - timedelta (minutes = 1 ),
@@ -311,15 +334,22 @@ def test_missed_exception_handling(self, logger):
311
334
312
335
check_missing (task_run_ts )
313
336
314
- # Logged the exception
315
- assert logger .exception .call_count == 1
337
+ # assert that task is called for the specific environments
338
+ assert mark_environment_missing_mock .delay .call_count == 2
339
+
340
+ # assert failing monitor raises an error
341
+ with pytest .raises (ValueError ):
342
+ mark_environment_missing (failing_monitor_environment .id )
343
+
344
+ # assert regular monitor works
345
+ mark_environment_missing (successful_monitor_environment .id )
316
346
317
347
# We still marked a monitor as missed
318
348
assert MonitorEnvironment .objects .filter (
319
- id = monitor_environment .id , status = MonitorStatus .MISSED_CHECKIN
349
+ id = successful_monitor_environment .id , status = MonitorStatus .MISSED_CHECKIN
320
350
).exists ()
321
351
assert MonitorCheckIn .objects .filter (
322
- monitor_environment = monitor_environment .id , status = CheckInStatus .MISSED
352
+ monitor_environment = successful_monitor_environment .id , status = CheckInStatus .MISSED
323
353
).exists ()
324
354
325
355
0 commit comments