Browse Source

ref(crons): Always pass ts to mark_failed in tests (#56981)

Evan Purkhiser 1 year ago
parent
commit
b06950b2aa

+ 5 - 1
src/sentry/monitors/logic/mark_failed.py

@@ -68,6 +68,10 @@ def mark_failed_threshold(
     if not affected:
         return False
 
+    # refresh the object from the database so we have the updated values in our
+    # cached instance
+    monitor_env.refresh_from_db()
+
     # check to see if we need to update the status
     if monitor_env.status == MonitorStatus.OK:
         previous_checkins = MonitorCheckIn.objects.filter(monitor_environment=monitor_env).order_by(
@@ -85,7 +89,7 @@ def mark_failed_threshold(
         # change monitor status + update fingerprint timestamp
         monitor_env.status = MonitorStatus.ERROR
         monitor_env.last_state_change = last_checkin
-        monitor_env.save()
+        monitor_env.save(update_fields=("status", "last_state_change"))
     elif monitor_env.status in [
         MonitorStatus.ERROR,
         MonitorStatus.MISSED_CHECKIN,

+ 9 - 7
tests/sentry/monitors/logic/test_mark_failed.py

@@ -49,7 +49,7 @@ class MarkFailedTestCase(TestCase):
             project_id=self.project.id,
             status=CheckInStatus.UNKNOWN,
         )
-        assert mark_failed(checkin, ts=None)
+        assert mark_failed(checkin, ts=checkin.date_added)
 
         assert len(mock_insert_data_to_database_legacy.mock_calls) == 1
 
@@ -100,7 +100,7 @@ class MarkFailedTestCase(TestCase):
             project_id=self.project.id,
             status=CheckInStatus.TIMEOUT,
         )
-        assert mark_failed(checkin, ts=None)
+        assert mark_failed(checkin, ts=checkin.date_added)
 
         assert len(mock_insert_data_to_database_legacy.mock_calls) == 1
 
@@ -151,7 +151,7 @@ class MarkFailedTestCase(TestCase):
             project_id=self.project.id,
             status=CheckInStatus.MISSED,
         )
-        assert mark_failed(checkin, ts=None)
+        assert mark_failed(checkin, ts=checkin.date_added)
 
         monitor.refresh_from_db()
         monitor_environment.refresh_from_db()
@@ -508,7 +508,7 @@ class MarkFailedTestCase(TestCase):
                 project_id=self.project.id,
                 status=status,
             )
-            mark_failed(checkin, ts=None)
+            mark_failed(checkin, ts=checkin.date_added)
 
         # failure has not hit threshold, monitor should be in an OK status
         monitor_environment = MonitorEnvironment.objects.get(id=monitor_environment.id)
@@ -532,7 +532,7 @@ class MarkFailedTestCase(TestCase):
                 project_id=self.project.id,
                 status=status,
             )
-            mark_failed(checkin, ts=None)
+            mark_failed(checkin, ts=checkin.date_added)
 
         # failure has hit threshold, monitor should be in a failed state
         monitor_environment = MonitorEnvironment.objects.get(id=monitor_environment.id)
@@ -542,6 +542,8 @@ class MarkFailedTestCase(TestCase):
         # assert correct number of occurrences was sent
         assert len(mock_produce_occurrence_to_kafka.mock_calls) == failure_issue_threshold
 
+        prior_last_state_change = monitor_environment.last_state_change
+
         # send another check-in to make sure we don't update last_state_change
         status = next(failure_statuses)
         checkin = MonitorCheckIn.objects.create(
@@ -550,10 +552,10 @@ class MarkFailedTestCase(TestCase):
             project_id=self.project.id,
             status=status,
         )
-        mark_failed(checkin, ts=None)
+        mark_failed(checkin, ts=checkin.date_added)
         monitor_environment = MonitorEnvironment.objects.get(id=monitor_environment.id)
         assert monitor_environment.status == MonitorStatus.ERROR
-        assert monitor_environment.last_state_change == monitor_environment.last_checkin
+        assert monitor_environment.last_state_change == prior_last_state_change
 
         # assert correct number of occurrences was sent
         assert len(mock_produce_occurrence_to_kafka.mock_calls) == failure_issue_threshold + 1