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(batch-exports): Wait for at least workflows to be available (#64793)

Tomás Farías Santana54e3399ba52a
diff --git a/products/batch_exports/backend/tests/temporal/backfills/conftest.py b/products/batch_exports/backend/tests/temporal/backfills/conftest.py
index 765502ff25a2..c59d6dcc8db0 100644
--- a/products/batch_exports/backend/tests/temporal/backfills/conftest.py
+++ b/products/batch_exports/backend/tests/temporal/backfills/conftest.py
@@ -13,9 +13,20 @@ async def wait_for_workflows(
temporal_client: temporalio.client.Client,
schedule_id: str,
expected_count: int,
+ assert_exact: bool = True,
timeout: int = 60,
) -> list[temporalio.client.WorkflowExecution]:
- """Wait for workflows to be queryable and return them."""
+ """Wait for workflows to be queryable and return them.
+
+ Arguments:
+ temporal_client: A Temporal client to be used to check for workflows.
+ schedule_id: The schedule whose workflows we are looking for.
+ expected_count: Wait for this number of workflows.
+ assert_exact: Whether to assert that the exact expected_count of
+ workflows is availale. Set this to False if any number of workflows
+ past expected_count is expected rather than an exact match.
+ timeout: Wait for at most this amount of seconds.
+ """
query = f'TemporalScheduledById="{schedule_id}" order by StartTime asc'
workflows: list[temporalio.client.WorkflowExecution] = []
elapsed = 0.0
@@ -33,6 +44,9 @@ async def wait_for_workflows(
delay = min(delay * 2, 5)
workflows = [workflow async for workflow in temporal_client.list_workflows(query=query)]
+ if assert_exact:
+ assert len(workflows) == expected_count
+
return workflows
diff --git a/products/batch_exports/backend/tests/temporal/backfills/test_backfill_workflow.py b/products/batch_exports/backend/tests/temporal/backfills/test_backfill_workflow.py
index aaf099c6c780..1e18ff7f71a1 100644
--- a/products/batch_exports/backend/tests/temporal/backfills/test_backfill_workflow.py
+++ b/products/batch_exports/backend/tests/temporal/backfills/test_backfill_workflow.py
@@ -300,7 +300,6 @@ async def test_backfill_batch_export_workflow(
raise AssertionError("Backfill completed early with no records")
workflows = await wait_for_workflows(temporal_client, batch_export.id, expected_num_workflows, timeout=50)
- assert len(workflows) == expected_num_workflows
# Check that the individual workflow IDs and TemporalScheduledStartTime search attributes are set correctly.
for index, workflow in enumerate(workflows, start=1):
@@ -353,7 +352,6 @@ async def test_backfill_batch_export_workflow_no_end_at(
)
workflows = await wait_for_workflows(temporal_client, desc.id, expected_count=2)
- assert len(workflows) == 2
event_backfill_ids = await assert_backfill_details_in_workflow_events(
temporal_client,
@@ -439,7 +437,7 @@ async def test_backfill_batch_export_workflow_fails_when_schedule_deleted_after_
)
# Wait for at least one workflow to start running before deleting the schedule
- await wait_for_workflows(temporal_client, desc.id, expected_count=1, timeout=10)
+ await wait_for_workflows(temporal_client, desc.id, expected_count=1, assert_exact=False, timeout=10)
await temporal_schedule_every_5_minutes.delete()