Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(experiments): cap cohort polling in exposure cohort tests (#124523)

Phil Haackede540e649e0
diff --git a/products/experiments/backend/test/test_presentation_api.py b/products/experiments/backend/test/test_presentation_api.py
index ba3a89dbc538..084ecb668111 100644
--- a/products/experiments/backend/test/test_presentation_api.py
+++ b/products/experiments/backend/test/test_presentation_api.py
@@ -7263,6 +7263,14 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTe
self.assertEqual(response.json()["feature_flag_key"], ff_key)
return response
+ def _assert_cohort_calculated(self, cohort: dict[str, Any]) -> None:
+ # Every caller runs transaction.on_commit callbacks at once, so one refetch reads the finished calculation.
+ if cohort["is_calculating"]:
+ response = self.client.get(f"/api/projects/{self.team.id}/cohorts/{cohort['id']}")
+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)
+ cohort = response.json()
+ self.assertFalse(cohort["is_calculating"], f"Cohort {cohort['id']} is still calculating")
+
@patch("django.db.transaction.on_commit", side_effect=lambda func: func())
def test_create_exposure_cohort_for_experiment(self, patch_on_commit: MagicMock):
# From mode 1 on, the cohorts API rejects new criteria on $feature_flag_called, and the exposure cohort must still save.
@@ -7340,9 +7348,7 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTe
cohort_id = cohort["id"]
- while cohort["is_calculating"]:
- response = self.client.get(f"/api/projects/{self.team.id}/cohorts/{cohort_id}")
- cohort = response.json()
+ self._assert_cohort_calculated(cohort)
response = self.client.get(f"/api/projects/{self.team.id}/cohorts/{cohort_id}/persons/?cohort={cohort_id}")
self.assertEqual(response.status_code, 200, response.content)
@@ -7408,9 +7414,7 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTe
cohort = response.json()["cohort"]
cohort_id = cohort["id"]
- while cohort["is_calculating"]:
- response = self.client.get(f"/api/projects/{self.team.id}/cohorts/{cohort_id}")
- cohort = response.json()
+ self._assert_cohort_calculated(cohort)
response = self.client.get(f"/api/projects/{self.team.id}/cohorts/{cohort_id}/persons/?cohort={cohort_id}")
self.assertEqual(response.status_code, 200, response.content)
@@ -7593,9 +7597,7 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTe
cohort_id = cohort["id"]
- while cohort["is_calculating"]:
- response = self.client.get(f"/api/projects/{self.team.id}/cohorts/{cohort_id}")
- cohort = response.json()
+ self._assert_cohort_calculated(cohort)
response = self.client.get(f"/api/projects/{self.team.id}/cohorts/{cohort_id}/persons/?cohort={cohort_id}")
self.assertEqual(response.status_code, 200, response.content)
@@ -7798,9 +7800,7 @@ class TestExperimentAuxiliaryEndpoints(_HoistFlagConfigClientMixin, ClickhouseTe
cohort_id = cohort["id"]
- while cohort["is_calculating"]:
- response = self.client.get(f"/api/projects/{self.team.id}/cohorts/{cohort_id}")
- cohort = response.json()
+ self._assert_cohort_calculated(cohort)
response = self.client.get(f"/api/projects/{self.team.id}/cohorts/{cohort_id}/persons/?cohort={cohort_id}")
self.assertEqual(response.status_code, 200, response.content)