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.

fix(tests): isolate temporal CI flakes (#73968)

Raúl Negrón-Oteroa35862e88f2a
diff --git a/posthog/temporal/tests/data_modeling/test_run_workflow.py b/posthog/temporal/tests/data_modeling/test_run_workflow.py
index 4555bbf58013..4c94e77db4cc 100644
--- a/posthog/temporal/tests/data_modeling/test_run_workflow.py
+++ b/posthog/temporal/tests/data_modeling/test_run_workflow.py
@@ -932,66 +932,47 @@ async def test_run_workflow_with_minio_bucket_with_errors(
assert job.rows_materialized == 0
-async def test_run_workflow_revert_materialization(
- minio_client,
- ateam,
- bucket_name,
- pageview_events,
- saved_queries,
- temporal_client,
- test_time,
-):
- workflow_id = str(uuid.uuid4())
- inputs = RunWorkflowInputs(team_id=ateam.pk)
-
+async def test_materialize_model_reverts_materialization_on_unknown_table(ateam):
def mock_hogql_table(_query, _team, _logger):
raise Exception("Unknown table")
with (
- override_settings(
- BUCKET_URL=f"s3://{bucket_name}",
- DATAWAREHOUSE_LOCAL_ACCESS_KEY=settings.OBJECT_STORAGE_ACCESS_KEY_ID,
- DATAWAREHOUSE_LOCAL_ACCESS_SECRET=settings.OBJECT_STORAGE_SECRET_ACCESS_KEY,
- DATAWAREHOUSE_LOCAL_BUCKET_REGION="us-east-1",
- DATAWAREHOUSE_BUCKET_DOMAIN="objectstorage:19000",
- ),
- freeze_time(test_time),
unittest.mock.patch("posthog.temporal.data_modeling.run_workflow.hogql_table", mock_hogql_table),
+ unittest.mock.patch("posthog.temporal.data_modeling.run_workflow.get_query_row_count", return_value=0),
+ unittest.mock.patch("posthog.temporal.data_modeling.run_workflow.get_s3_client"),
+ # Reaches object storage before hogql_table runs, so an unready bucket would land in the
+ # generic error branch rather than the revert one under test.
+ unittest.mock.patch("posthog.temporal.data_modeling.run_workflow._get_credentials", return_value={}),
+ unittest.mock.patch("products.data_modeling.backend.logic.enrich_view_semantics._start_enrichment_workflow"),
+ unittest.mock.patch(
+ "products.data_warehouse.backend.logic.data_load.saved_query_service.delete_saved_query_schedule"
+ ),
):
- async with temporalio.worker.Worker(
- temporal_client,
- task_queue=settings.DATA_MODELING_TASK_QUEUE,
- workflows=[RunWorkflow],
- activities=[
- start_run_activity,
- build_dag_activity,
- run_dag_activity,
- finish_run_activity,
- create_job_model_activity,
- fail_jobs_activity,
- cleanup_running_jobs_activity,
- ],
- workflow_runner=temporalio.worker.UnsandboxedWorkflowRunner(),
- ):
- # Ensure the team exists in the DB context before running workflow
- await database_sync_to_async(Team.objects.get)(pk=ateam.pk)
- await temporal_client.execute_workflow(
- RunWorkflow.run,
- inputs,
- id=workflow_id,
- task_queue=settings.DATA_MODELING_TASK_QUEUE,
- retry_policy=temporalio.common.RetryPolicy(maximum_attempts=1),
- execution_timeout=dt.timedelta(seconds=30),
- )
+ saved_query = await DataWarehouseSavedQuery.objects.acreate(
+ team=ateam,
+ name="my_model",
+ query={"query": "select 1 as one", "kind": "HogQLQuery"},
+ is_materialized=True,
+ sync_frequency_interval=dt.timedelta(hours=1),
+ status=DataWarehouseSavedQuery.Status.COMPLETED,
+ )
+ job = await database_sync_to_async(DataModelingJob.objects.create)(
+ team=ateam,
+ status=DataModelingJob.Status.RUNNING,
+ workflow_id=str(uuid.uuid4()),
+ )
- job = await DataModelingJob.objects.aget(workflow_id=workflow_id)
- assert job is not None
+ with pytest.raises(Exception, match="Table reference missing"):
+ await materialize_model(saved_query.id.hex, ateam, saved_query, job, unittest.mock.AsyncMock())
+
+ await database_sync_to_async(job.refresh_from_db)()
assert job.status == DataModelingJob.Status.FAILED
assert job.rows_materialized == 0
- for query in saved_queries:
- await database_sync_to_async(query.refresh_from_db)()
- assert query.is_materialized is False
+ await database_sync_to_async(saved_query.refresh_from_db)()
+ assert saved_query.is_materialized is False
+ assert saved_query.sync_frequency_interval is None
+ assert saved_query.status is None
async def test_run_workflow_timeout_does_not_pause_schedule_without_consecutive_failures(
diff --git a/posthog/temporal/tests/utils/events.py b/posthog/temporal/tests/utils/events.py
index 5799c1db6985..1a2cb224891f 100644
--- a/posthog/temporal/tests/utils/events.py
+++ b/posthog/temporal/tests/utils/events.py
@@ -14,6 +14,10 @@ from posthog.models.raw_sessions.sessions_v2 import RAW_SESSION_TABLE_BACKFILL_S
from posthog.temporal.common.clickhouse import ClickHouseClient, ClickHouseError
from posthog.temporal.tests.utils.datetimes import date_range
+# Far enough above any real test team's sequential pk that synthetic rows can never
+# land on a team a later test asserts counts for.
+OTHER_TEAM_ID_OFFSET = 1_000_000_000
+
@retry(
retry=retry_if_exception_type(
@@ -279,7 +283,7 @@ async def generate_test_events_in_clickhouse(
# Events generated for a different team
events_from_other_team = generate_test_events(
count=count_other_team,
- team_id=team_id + random.randint(1, 1000),
+ team_id=team_id + OTHER_TEAM_ID_OFFSET,
possible_datetimes=possible_datetimes,
event_name=event_name,
properties=properties,