@@ -78,7 +78,10 @@ def test_general_check_threshold_crossed_deferred(self):
78
78
self ._create_alert_settings (
79
79
metric = m , custom_operator = ">" , custom_threshold = 90 , custom_tolerance = 1
80
80
)
81
- m .write (99 , time = ten_minutes_ago )
81
+ with freeze_time (start_time ):
82
+ m .write (99 )
83
+ with freeze_time (ten_minutes_after ):
84
+ m .write (99 )
82
85
m .refresh_from_db ()
83
86
self .assertEqual (m .is_healthy , False )
84
87
self .assertEqual (m .is_healthy_tolerant , False )
@@ -122,7 +125,8 @@ def test_general_check_threshold_crossed_for_long_time(self):
122
125
)
123
126
124
127
with self .subTest ("Test no notification is generated for healthy status" ):
125
- m .write (89 , time = ten_minutes_ago )
128
+ with freeze_time (ten_minutes_ago ):
129
+ m .write (89 )
126
130
m .refresh_from_db ()
127
131
self .assertEqual (m .is_healthy , True )
128
132
self .assertEqual (m .is_healthy_tolerant , True )
@@ -133,7 +137,8 @@ def test_general_check_threshold_crossed_for_long_time(self):
133
137
# timeseries database to process the transaction.
134
138
self ._read_metric (m )
135
139
with self .subTest ("Test no notification is generated when check=False" ):
136
- m .write (91 , time = ten_minutes_ago , check = False )
140
+ with freeze_time (ten_minutes_ago ):
141
+ m .write (91 , check = False )
137
142
self .assertEqual (Notification .objects .count (), 0 )
138
143
139
144
self ._read_metric (m )
@@ -241,7 +246,10 @@ def test_object_check_threshold_crossed_deferred(self):
241
246
alert_s = self ._create_alert_settings (
242
247
metric = om , custom_operator = ">" , custom_threshold = 90 , custom_tolerance = 1
243
248
)
244
- om .write (99 , time = ten_minutes_ago )
249
+ with freeze_time (start_time ):
250
+ om .write (99 )
251
+ with freeze_time (ten_minutes_after ):
252
+ om .write (99 )
245
253
om .refresh_from_db ()
246
254
self .assertEqual (om .is_healthy , False )
247
255
self .assertEqual (om .is_healthy_tolerant , False )
@@ -271,9 +279,11 @@ def test_object_check_threshold_crossed_for_long_time(self):
271
279
alert_s = self ._create_alert_settings (
272
280
metric = om , custom_operator = ">" , custom_threshold = 90 , custom_tolerance = 1
273
281
)
274
- self ._write_metric (om , 89 , time = ten_minutes_ago )
282
+ with freeze_time (ten_minutes_ago ):
283
+ self ._write_metric (om , 89 )
275
284
self .assertEqual (Notification .objects .count (), 0 )
276
- self ._write_metric (om , 91 , time = ten_minutes_ago , check = False )
285
+ with freeze_time (ten_minutes_ago ):
286
+ self ._write_metric (om , 91 , check = False )
277
287
self .assertEqual (Notification .objects .count (), 0 )
278
288
self ._write_metric (om , 92 )
279
289
om .refresh_from_db ()
@@ -319,6 +329,38 @@ def test_object_check_threshold_crossed_for_long_time(self):
319
329
self .assertEqual (om .is_healthy_tolerant , True )
320
330
self .assertEqual (Notification .objects .count (), 2 )
321
331
332
+ @freeze_time (start_time )
333
+ def test_object_check_threshold_crossed_historical_data (self ):
334
+ """
335
+ Do not evaluate threshold crossed for historical data
336
+ """
337
+ self ._create_admin ()
338
+ om = self ._create_object_metric (name = "load" )
339
+ self ._create_alert_settings (
340
+ metric = om , custom_operator = ">" , custom_threshold = 90 , custom_tolerance = 1
341
+ )
342
+
343
+ # We need to write with "time" argument instead of freeze_time to
344
+ # simulate historical data
345
+ self ._write_metric (om , 99 , time = start_time - timedelta (minutes = 60 ))
346
+ om .refresh_from_db ()
347
+ self .assertEqual (om .is_healthy , True )
348
+ self .assertEqual (om .is_healthy_tolerant , True )
349
+ self .assertEqual (Notification .objects .count (), 0 )
350
+
351
+ self ._write_metric (om , 99 , time = start_time - timedelta (minutes = 10 ))
352
+ om .refresh_from_db ()
353
+ self .assertEqual (om .is_healthy , True )
354
+ self .assertEqual (om .is_healthy_tolerant , True )
355
+ self .assertEqual (Notification .objects .count (), 0 )
356
+
357
+ # Writing real-time data should enforce the threshold check
358
+ self ._write_metric (om , 99 , time = start_time )
359
+ om .refresh_from_db ()
360
+ self .assertEqual (om .is_healthy , False )
361
+ self .assertEqual (om .is_healthy_tolerant , True )
362
+ self .assertEqual (Notification .objects .count (), 0 )
363
+
322
364
def test_flapping_metric_with_tolerance (self ):
323
365
self ._create_admin ()
324
366
om = self ._create_object_metric (name = "ping" )
@@ -403,7 +445,10 @@ def test_alerts_disabled(self):
403
445
custom_tolerance = 1 ,
404
446
is_active = False ,
405
447
)
406
- m .write (99 , time = ten_minutes_ago )
448
+ with freeze_time (start_time ):
449
+ m .write (99 )
450
+ with freeze_time (ten_minutes_after ):
451
+ m .write (99 )
407
452
m .refresh_from_db ()
408
453
self .assertEqual (m .is_healthy , False )
409
454
self .assertEqual (m .is_healthy_tolerant , False )
@@ -511,7 +556,10 @@ def test_general_check_threshold_with_alert_field_crossed_deferred(self):
511
556
self ._create_alert_settings (
512
557
metric = m , custom_operator = ">" , custom_threshold = 30 , custom_tolerance = 1
513
558
)
514
- m .write (10 , time = ten_minutes_ago , extra_values = {"test_related_2" : 35 })
559
+ with freeze_time (start_time ):
560
+ m .write (10 , extra_values = {"test_related_2" : 35 })
561
+ with freeze_time (ten_minutes_after ):
562
+ m .write (10 , extra_values = {"test_related_2" : 35 })
515
563
m .refresh_from_db ()
516
564
self .assertEqual (m .is_healthy , False )
517
565
self .assertEqual (m .is_healthy_tolerant , False )
0 commit comments