-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(batch-exports-backfills): Add test on delete
- Loading branch information
1 parent
ef4021b
commit cac546d
Showing
4 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import asyncio | ||
|
||
import pytest | ||
import temporalio.client | ||
from asgiref.sync import async_to_sync | ||
from django.test.client import Client as HttpClient | ||
from rest_framework import status | ||
from temporalio.service import RPCError | ||
|
||
from posthog.api.test.batch_exports.conftest import start_test_worker | ||
from posthog.api.test.batch_exports.operations import ( | ||
backfill_batch_export_ok, | ||
create_batch_export_ok, | ||
delete_batch_export, | ||
delete_batch_export_ok, | ||
|
@@ -59,6 +64,105 @@ def test_delete_batch_export(client: HttpClient): | |
describe_schedule(temporal, batch_export_id) | ||
|
||
|
||
@async_to_sync | ||
async def wait_for_workflow_executions( | ||
temporal: temporalio.client.Client, query: str, timeout: int = 30, sleep: int = 1 | ||
): | ||
"""Wait for Workflow Executions matching query.""" | ||
workflows = [workflow async for workflow in temporal.list_workflows(query=query)] | ||
|
||
total = 0 | ||
while not workflows: | ||
total += sleep | ||
|
||
if total > timeout: | ||
raise TimeoutError(f"No backfill Workflow Executions after {timeout} seconds") | ||
|
||
await asyncio.sleep(sleep) | ||
workflows = [workflow async for workflow in temporal.list_workflows(query=query)] | ||
|
||
return workflows | ||
|
||
|
||
@async_to_sync | ||
async def wait_for_workflow_in_status( | ||
temporal: temporalio.client.Client, | ||
workflow_id: str, | ||
status: temporalio.client.WorkflowExecutionStatus, | ||
sleep: int = 1, | ||
timeout: int = 30, | ||
): | ||
"""Wait for a Workflow to be in a given status.""" | ||
handle = temporal.get_workflow_handle(workflow_id) | ||
workflow = await handle.describe() | ||
|
||
total = 0 | ||
while workflow.status != status: | ||
total += sleep | ||
|
||
if total > timeout: | ||
break | ||
|
||
await asyncio.sleep(sleep) | ||
workflow = await handle.describe() | ||
|
||
return workflow | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
def test_delete_batch_export_cancels_backfills(client: HttpClient): | ||
"""Test deleting a BatchExport cancels ongoing BatchExportBackfill.""" | ||
temporal = sync_connect() | ||
|
||
destination_data = { | ||
"type": "S3", | ||
"config": { | ||
"bucket_name": "my-production-s3-bucket", | ||
"region": "us-east-1", | ||
"prefix": "posthog-events/", | ||
"aws_access_key_id": "abc123", | ||
"aws_secret_access_key": "secret", | ||
}, | ||
} | ||
batch_export_data = { | ||
"name": "my-production-s3-bucket-destination", | ||
"destination": destination_data, | ||
"interval": "hour", | ||
} | ||
|
||
organization = create_organization("Test Org") | ||
team = create_team(organization) | ||
user = create_user("[email protected]", "Test User", organization) | ||
client.force_login(user) | ||
|
||
with start_test_worker(temporal): | ||
batch_export = create_batch_export_ok(client, team.pk, batch_export_data) | ||
batch_export_id = batch_export["id"] | ||
|
||
start_at = "2023-10-23 00:00:00" | ||
end_at = "2023-10-24 00:00:00" | ||
batch_export_backfill = backfill_batch_export_ok(client, team.pk, batch_export_id, start_at, end_at) | ||
|
||
# In order for the backfill to be cancelable, it needs to be running and requesting backfills. | ||
# We check this by waiting for executions scheduled by our BatchExport id to pop up. | ||
_ = wait_for_workflow_executions(temporal, query=f'TemporalScheduledById="{batch_export_id}"') | ||
|
||
delete_batch_export_ok(client, team.pk, batch_export_id) | ||
|
||
response = get_batch_export(client, team.pk, batch_export_id) | ||
assert response.status_code == status.HTTP_404_NOT_FOUND | ||
|
||
workflow = wait_for_workflow_in_status( | ||
temporal, | ||
workflow_id=batch_export_backfill["backfill_id"], | ||
status=temporalio.client.WorkflowExecutionStatus.CANCELED, | ||
) | ||
assert workflow.status == temporalio.client.WorkflowExecutionStatus.CANCELED | ||
|
||
with pytest.raises(RPCError): | ||
describe_schedule(temporal, batch_export_id) | ||
|
||
|
||
def test_cannot_delete_export_of_other_organizations(client: HttpClient): | ||
temporal = sync_connect() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters