Просмотр исходного кода

ref: fix flakiness of test_issue_velocity by freezing time (#66872)

resolves https://sentry.sentry.io/issues/5059849041
resolves https://sentry.sentry.io/issues/5059074130

this patch reproduces the flake:

```diff
diff --git a/src/sentry/issues/issue_velocity.py b/src/sentry/issues/issue_velocity.py
index 015cb332e2..2a0fd41361 100644
--- a/src/sentry/issues/issue_velocity.py
+++ b/src/sentry/issues/issue_velocity.py
@@ -188,6 +188,7 @@ def fallback_to_stale_or_zero(
         - timedelta(seconds=TIME_TO_USE_EXISTING_THRESHOLD)
         + timedelta(seconds=FALLBACK_TTL)
     )
+    time.sleep(1)
     client = get_redis_client()
     with client.pipeline() as p:
         p.watch(threshold_key)
```

<!-- Describe your PR here. -->
anthony sottile 1 год назад
Родитель
Сommit
f5dfe6b63a
1 измененных файлов с 17 добавлено и 38 удалено
  1. 17 38
      tests/sentry/issues/test_issue_velocity.py

+ 17 - 38
tests/sentry/issues/test_issue_velocity.py

@@ -18,13 +18,14 @@ from sentry.issues.issue_velocity import (
 )
 from sentry.tasks.post_process import locks
 from sentry.testutils.cases import SnubaTestCase, TestCase
-from sentry.testutils.helpers.datetime import iso_format
+from sentry.testutils.helpers.datetime import freeze_time, iso_format
 from sentry.testutils.silo import region_silo_test
 
 WEEK_IN_HOURS = 7 * 24
 
 
 @region_silo_test
+@freeze_time()
 class IssueVelocityTests(TestCase, SnubaTestCase):
     def setUp(self):
         self.now = timezone.now()
@@ -182,7 +183,7 @@ class IssueVelocityTests(TestCase, SnubaTestCase):
         redis_client.set(THRESHOLD_KEY.format(project_id=self.project.id), 1.2)
         redis_client.set(
             STALE_DATE_KEY.format(project_id=self.project.id),
-            str(self.utcnow - timedelta(days=1)),
+            str(self.utcnow - timedelta(days=1, seconds=1)),
         )
         mock_update.return_value = 1.5
         assert get_latest_threshold(self.project) == 1.5
@@ -244,8 +245,7 @@ class IssueVelocityTests(TestCase, SnubaTestCase):
         assert redis_client.get("threshold-key") == "5"
         stored_date = redis_client.get("date-key")
         assert isinstance(stored_date, str)
-        # self.utcnow and the datetime.utcnow() used in the update method may vary in milliseconds so we can't do a direct comparison
-        assert 0 <= (datetime.fromisoformat(stored_date) - self.utcnow).total_seconds() < 1
+        assert datetime.fromisoformat(stored_date) == self.utcnow
         assert redis_client.ttl("threshold-key") == DEFAULT_TTL
         assert redis_client.ttl("date-key") == DEFAULT_TTL
 
@@ -280,7 +280,7 @@ class IssueVelocityTests(TestCase, SnubaTestCase):
         assert redis_client.get("threshold-key") == "0"
         stored_date = redis_client.get("date-key")
         assert isinstance(stored_date, str)
-        assert 0 <= (datetime.fromisoformat(stored_date) - self.utcnow).total_seconds() < 1
+        assert datetime.fromisoformat(stored_date) == self.utcnow
         assert redis_client.ttl("threshold-key") == DEFAULT_TTL
 
     def test_fallback_to_stale(self):
@@ -295,17 +295,10 @@ class IssueVelocityTests(TestCase, SnubaTestCase):
         assert redis_client.get("threshold-key") == "0.5"
         stored_date = redis_client.get("date-key")
         assert isinstance(stored_date, str)
-        assert (
-            0
-            <= (
-                datetime.fromisoformat(stored_date)
-                - (
-                    self.utcnow
-                    - timedelta(seconds=TIME_TO_USE_EXISTING_THRESHOLD)
-                    + timedelta(seconds=FALLBACK_TTL)
-                )
-            ).total_seconds()
-            < 1
+        assert datetime.fromisoformat(stored_date) == (
+            self.utcnow
+            - timedelta(seconds=TIME_TO_USE_EXISTING_THRESHOLD)
+            + timedelta(seconds=FALLBACK_TTL)
         )
 
         assert redis_client.ttl("threshold-key") == 86400
@@ -321,17 +314,10 @@ class IssueVelocityTests(TestCase, SnubaTestCase):
         assert redis_client.get("threshold-key") == "0"
         stored_date = redis_client.get("date-key")
         assert isinstance(stored_date, str)
-        assert (
-            0
-            <= (
-                datetime.fromisoformat(stored_date)
-                - (
-                    self.utcnow
-                    - timedelta(seconds=TIME_TO_USE_EXISTING_THRESHOLD)
-                    + timedelta(seconds=FALLBACK_TTL)
-                )
-            ).total_seconds()
-            < 1
+        assert datetime.fromisoformat(stored_date) == (
+            self.utcnow
+            - timedelta(seconds=TIME_TO_USE_EXISTING_THRESHOLD)
+            + timedelta(seconds=FALLBACK_TTL)
         )
         assert redis_client.ttl("threshold-key") == FALLBACK_TTL
         assert redis_client.ttl("date-key") == FALLBACK_TTL
@@ -346,17 +332,10 @@ class IssueVelocityTests(TestCase, SnubaTestCase):
         assert redis_client.get("threshold-key") == "0"
         stored_date = redis_client.get("date-key")
         assert isinstance(stored_date, str)
-        assert (
-            0
-            <= (
-                datetime.fromisoformat(stored_date)
-                - (
-                    self.utcnow
-                    - timedelta(seconds=TIME_TO_USE_EXISTING_THRESHOLD)
-                    + timedelta(seconds=FALLBACK_TTL)
-                )
-            ).total_seconds()
-            < 1
+        assert datetime.fromisoformat(stored_date) == (
+            self.utcnow
+            - timedelta(seconds=TIME_TO_USE_EXISTING_THRESHOLD)
+            + timedelta(seconds=FALLBACK_TTL)
         )
 
         assert redis_client.ttl("threshold-key") == FALLBACK_TTL