Skip to content

Commit b4ef139

Browse files
authored
add test to hello_cancellation (temporalio#30)
* add test hello_cancellation
1 parent 1a414e2 commit b4ef139

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import asyncio
2+
import uuid
3+
4+
import pytest
5+
from temporalio.client import Client, WorkflowExecutionStatus, WorkflowFailureError
6+
from temporalio.exceptions import CancelledError
7+
from temporalio.worker import Worker
8+
9+
from hello.hello_cancellation import (
10+
CancellationWorkflow,
11+
cleanup_activity,
12+
never_complete_activity,
13+
)
14+
15+
16+
async def test_cancel_workflow(client: Client):
17+
task_queue_name = str(uuid.uuid4())
18+
19+
async with Worker(
20+
client,
21+
task_queue=task_queue_name,
22+
workflows=[CancellationWorkflow],
23+
activities=[cleanup_activity, never_complete_activity],
24+
):
25+
handle = await client.start_workflow(
26+
CancellationWorkflow.run,
27+
id=(str(uuid.uuid4())),
28+
task_queue=task_queue_name,
29+
)
30+
31+
await asyncio.wait_for(
32+
wait_for_activity_to_start("never_complete_activity", handle),
33+
timeout=5,
34+
)
35+
36+
await handle.cancel()
37+
38+
with pytest.raises(WorkflowFailureError) as err:
39+
await handle.result()
40+
assert isinstance(err.value.cause, CancelledError)
41+
42+
assert WorkflowExecutionStatus.CANCELED == (await handle.describe()).status
43+
44+
45+
async def wait_for_activity_to_start(activity_name, handle):
46+
while not (await has_activity_started(activity_name, handle)):
47+
await asyncio.sleep(0.2)
48+
49+
50+
async def has_activity_started(activity_name, handle):
51+
pending_activities = (await handle.describe()).raw_description.pending_activities
52+
for pending_activity in pending_activities:
53+
if pending_activity.activity_type.name == activity_name:
54+
return True
55+
56+
return False

0 commit comments

Comments
 (0)