Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sentry.issues.issue_occurrence import IssueOccurrence
from sentry.issues.status_change_message import StatusChangeMessage
from sentry.testutils.cases import TestCase
from sentry.workflow_engine.models import DataPacket, Detector
from sentry.workflow_engine.models import DataPacket, Detector, DetectorState
from sentry.workflow_engine.types import (
DataConditionResult,
DetectorGroupKey,
Expand Down Expand Up @@ -100,6 +100,43 @@ def test_init__threshold_makes_query(self) -> None:
handler = MockDetectorStateHandler(detector=fetched_detector)
assert handler._thresholds == {Level.OK: 1, Level.HIGH: 1}

def test_bulk_commit_skips_update_when_state_unchanged(self) -> None:
group_key: DetectorGroupKey = None

detector_state = self.create_detector_state(
detector=self.detector,
detector_group_key=group_key,
is_triggered=False,
state=Level.OK,
)

handler = MockDetectorStateHandler(
detector=self.detector,
thresholds={Level.HIGH: 1},
)

# First update changes state - should call bulk_update
handler.state_manager.enqueue_state_update(
group_key, is_triggered=True, priority=Level.HIGH
)
with mock.patch.object(
DetectorState.objects, "bulk_update", wraps=DetectorState.objects.bulk_update
) as mock_bulk_update:
handler.state_manager.commit_state_updates()
mock_bulk_update.assert_called_once()

detector_state.refresh_from_db()
assert detector_state.is_triggered is True
assert detector_state.state == str(Level.HIGH)

# Second update with same state - should not call bulk_update
handler.state_manager.enqueue_state_update(
group_key, is_triggered=True, priority=Level.HIGH
)
with mock.patch.object(DetectorState.objects, "bulk_update") as mock_bulk_update:
handler.state_manager.commit_state_updates()
mock_bulk_update.assert_not_called()


class TestStatefulDetectorIncrementThresholds(TestCase):
def setUp(self) -> None:
Expand Down
Loading