|
@@ -1,6 +1,9 @@
|
|
|
from datetime import datetime, timedelta
|
|
|
-from unittest.mock import patch
|
|
|
+from unittest import mock
|
|
|
|
|
|
+import msgpack
|
|
|
+from arroyo.backends.kafka import KafkaPayload
|
|
|
+from django.test import override_settings
|
|
|
from django.utils import timezone
|
|
|
|
|
|
from sentry.constants import ObjectStatus
|
|
@@ -13,7 +16,12 @@ from sentry.monitors.models import (
|
|
|
MonitorType,
|
|
|
ScheduleType,
|
|
|
)
|
|
|
-from sentry.monitors.tasks import check_missing, check_timeout, try_monitor_tasks_trigger
|
|
|
+from sentry.monitors.tasks import (
|
|
|
+ check_missing,
|
|
|
+ check_timeout,
|
|
|
+ clock_pulse,
|
|
|
+ try_monitor_tasks_trigger,
|
|
|
+)
|
|
|
from sentry.testutils.cases import TestCase
|
|
|
|
|
|
|
|
@@ -405,7 +413,7 @@ class CheckMonitorsTest(TestCase):
|
|
|
id=monitor_environment.id, status=MonitorStatus.TIMEOUT
|
|
|
).exists()
|
|
|
|
|
|
- @patch("sentry.monitors.tasks.logger")
|
|
|
+ @mock.patch("sentry.monitors.tasks.logger")
|
|
|
def test_missed_exception_handling(self, logger):
|
|
|
org = self.create_organization()
|
|
|
project = self.create_project(organization=org)
|
|
@@ -458,7 +466,7 @@ class CheckMonitorsTest(TestCase):
|
|
|
monitor_environment=monitor_environment.id, status=CheckInStatus.MISSED
|
|
|
).exists()
|
|
|
|
|
|
- @patch("sentry.monitors.tasks.logger")
|
|
|
+ @mock.patch("sentry.monitors.tasks.logger")
|
|
|
def test_timeout_exception_handling(self, logger):
|
|
|
org = self.create_organization()
|
|
|
project = self.create_project(organization=org)
|
|
@@ -548,7 +556,7 @@ class CheckMonitorsTest(TestCase):
|
|
|
id=monitor_environment.id, status=MonitorStatus.TIMEOUT
|
|
|
).exists()
|
|
|
|
|
|
- @patch("sentry.monitors.tasks._dispatch_tasks")
|
|
|
+ @mock.patch("sentry.monitors.tasks._dispatch_tasks")
|
|
|
def test_monitor_task_trigger(self, dispatch_tasks):
|
|
|
now = datetime.now().replace(second=0, microsecond=0)
|
|
|
|
|
@@ -569,13 +577,13 @@ class CheckMonitorsTest(TestCase):
|
|
|
assert dispatch_tasks.call_count == 2
|
|
|
|
|
|
# A skipped minute trigges the task AND captures an error
|
|
|
- with patch("sentry_sdk.capture_message") as capture_message:
|
|
|
+ with mock.patch("sentry_sdk.capture_message") as capture_message:
|
|
|
assert capture_message.call_count == 0
|
|
|
try_monitor_tasks_trigger(ts=now + timedelta(minutes=3, seconds=5))
|
|
|
assert dispatch_tasks.call_count == 3
|
|
|
capture_message.assert_called_with("Monitor task dispatch minute skipped")
|
|
|
|
|
|
- @patch("sentry.monitors.tasks._dispatch_tasks")
|
|
|
+ @mock.patch("sentry.monitors.tasks._dispatch_tasks")
|
|
|
def test_monitor_task_trigger_partition_desync(self, dispatch_tasks):
|
|
|
"""
|
|
|
When consumer partitions are not completely synchronized we may read
|
|
@@ -602,3 +610,19 @@ class CheckMonitorsTest(TestCase):
|
|
|
# Fourth message moves past a new minute boundary, tick
|
|
|
try_monitor_tasks_trigger(ts=now + timedelta(minutes=1, seconds=1))
|
|
|
assert dispatch_tasks.call_count == 2
|
|
|
+
|
|
|
+ @override_settings(KAFKA_INGEST_MONITORS="monitors-test-topic")
|
|
|
+ @override_settings(SENTRY_EVENTSTREAM="sentry.eventstream.kafka.KafkaEventStream")
|
|
|
+ @mock.patch("sentry.monitors.tasks._checkin_producer")
|
|
|
+ def test_clock_pulse(self, _checkin_producer):
|
|
|
+ clock_pulse()
|
|
|
+
|
|
|
+ assert _checkin_producer.produce.call_count == 1
|
|
|
+ assert _checkin_producer.produce.mock_calls[0] == mock.call(
|
|
|
+ mock.ANY,
|
|
|
+ KafkaPayload(
|
|
|
+ None,
|
|
|
+ msgpack.packb({"message_type": "clock_pulse"}),
|
|
|
+ [],
|
|
|
+ ),
|
|
|
+ )
|