Skip to content

Commit

Permalink
fix(alerts): Fix flaky anomaly detection tests (#78218)
Browse files Browse the repository at this point in the history
Follow up to #78186 to add
`freeze_time` and fix the flakiness.
  • Loading branch information
ceorourke authored Sep 26, 2024
1 parent baa89e4 commit c344143
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions tests/sentry/seer/anomaly_detection/test_store_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from datetime import datetime
from datetime import UTC, datetime, timedelta
from typing import Any

import pytest
Expand All @@ -10,7 +10,7 @@
from sentry.snuba.dataset import Dataset
from sentry.snuba.models import SnubaQuery
from sentry.testutils.cases import BaseMetricsTestCase, PerformanceIssueTestCase
from sentry.testutils.helpers.datetime import iso_format
from sentry.testutils.helpers.datetime import before_now, freeze_time, iso_format
from sentry.testutils.performance_issues.event_generators import get_event
from sentry.utils.snuba import SnubaTSResult
from tests.sentry.incidents.endpoints.test_organization_alert_rule_index import AlertRuleBase
Expand All @@ -30,15 +30,18 @@ def make_event(**kwargs: Any) -> dict[str, Any]:
return result


@freeze_time(before_now(days=2).replace(hour=0, minute=0, second=0, microsecond=0))
class AnomalyDetectionStoreDataTest(AlertRuleBase, BaseMetricsTestCase, PerformanceIssueTestCase):
def setUp(self):
super().setUp()
self.time_1 = "2024-08-29T00:00:00Z"
self.time_1_dt = datetime(2024, 8, 29, 0, 0)

self.now = datetime.now(UTC)
self.time_1_dt = self.now - timedelta(days=2)
self.time_1 = self.time_1_dt.strftime("%Y-%m-%dT%H:%M:%SZ")
self.time_1_ts = self.time_1_dt.timestamp()

self.time_2 = "2024-08-29T02:00:00Z"
self.time_2_dt = datetime(2024, 8, 29, 2, 0)
self.time_2_dt = self.now - timedelta(days=3)
self.time_2 = self.time_2_dt.strftime("%Y-%m-%dT%H:%M:%SZ")
self.time_2_ts = self.time_2_dt.timestamp()

self.received = self.time_1_ts
Expand Down Expand Up @@ -84,9 +87,6 @@ def test_anomaly_detection_format_historical_data_two(self):
)
assert result == expected_return_value

@pytest.mark.skip(
reason="This test is flaking, skipping for now - this feature isn't released."
)
def test_anomaly_detection_fetch_historical_data(self):
alert_rule = self.create_alert_rule(organization=self.organization, projects=[self.project])
snuba_query = SnubaQuery.objects.get(id=alert_rule.snuba_query_id)
Expand Down Expand Up @@ -119,9 +119,6 @@ def test_anomaly_detection_fetch_historical_data(self):
assert {"time": int(self.time_1_ts), "count": 1} in result.data.get("data")
assert {"time": int(self.time_2_ts), "count": 1} in result.data.get("data")

@pytest.mark.skip(
reason="This test is flaking, skipping for now - this feature isn't released."
)
def test_anomaly_detection_fetch_historical_data_is_unresolved_query(self):
alert_rule = self.create_alert_rule(organization=self.organization, projects=[self.project])
snuba_query = SnubaQuery.objects.get(id=alert_rule.snuba_query_id)
Expand Down Expand Up @@ -156,9 +153,6 @@ def test_anomaly_detection_fetch_historical_data_is_unresolved_query(self):
assert {"time": int(self.time_1_ts), "count": 1} in result.data.get("data")
assert {"time": int(self.time_2_ts), "count": 1} in result.data.get("data")

@pytest.mark.skip(
reason="This test is flaking, skipping for now - this feature isn't released."
)
def test_anomaly_detection_fetch_historical_data_performance_alert(self):
alert_rule = self.create_alert_rule(
organization=self.organization, projects=[self.project], dataset=Dataset.Transactions
Expand Down Expand Up @@ -197,9 +191,6 @@ def test_anomaly_detection_format_historical_data_crash_rate_alert(self):
result = format_historical_data(data, ["count()"], metrics_performance, self.organization)
assert result == expected_return_value

@pytest.mark.skip(
reason="This test is flaking, skipping for now - this feature isn't released."
)
def test_anomaly_detection_fetch_historical_data_crash_rate_alert(self):
self.store_session(
self.build_session(
Expand Down

0 comments on commit c344143

Please sign in to comment.