Skip to content

Commit c3634fe

Browse files
committed
make cancellation details non-optional for testing activity env
1 parent af874ed commit c3634fe

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

temporalio/testing/_activity.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,21 @@ def __init__(self) -> None:
8080

8181
def cancel(
8282
self,
83-
cancellation_details: Optional[
84-
temporalio.activity.ActivityCancellationDetails
85-
] = None,
83+
cancellation_details: temporalio.activity.ActivityCancellationDetails,
8684
) -> None:
8785
"""Cancel the activity.
8886
8987
Args:
90-
cancellation_details: Optional details about the cancellation. When provided, these
91-
will be accessible through temporalio.activity.cancellation_details()
88+
cancellation_details: details about the cancellation. These will
89+
be accessible through temporalio.activity.cancellation_details()
9290
in the activity after cancellation.
9391
9492
This only has an effect on the first call.
9593
"""
9694
if self._cancelled:
9795
return
9896
self._cancelled = True
99-
if cancellation_details:
100-
self._cancellation_details.details = cancellation_details
97+
self._cancellation_details.details = cancellation_details
10198
for act in self._activities:
10299
act.cancel()
103100

tests/testing/test_activity.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ async def via_create_task():
2626
await asyncio.Future()
2727
raise RuntimeError("Unreachable")
2828
except asyncio.CancelledError:
29-
activity.heartbeat("cancelled")
29+
cancellation_details = activity.cancellation_details()
30+
if cancellation_details:
31+
activity.heartbeat(
32+
f"cancelled={cancellation_details.cancel_requested}",
33+
)
3034
return "done"
3135

3236
env = ActivityEnvironment()
@@ -37,9 +41,11 @@ async def via_create_task():
3741
task = asyncio.create_task(env.run(do_stuff, "param1"))
3842
await waiting.wait()
3943
# Cancel and confirm done
40-
env.cancel()
44+
env.cancel(
45+
cancellation_details=activity.ActivityCancellationDetails(cancel_requested=True)
46+
)
4147
assert "done" == await task
42-
assert heartbeats == ["param: param1", "task, type: unknown", "cancelled"]
48+
assert heartbeats == ["param: param1", "task, type: unknown", "cancelled=True"]
4349

4450

4551
def test_activity_env_sync():
@@ -72,7 +78,11 @@ def via_thread():
7278
raise RuntimeError("Unexpected")
7379
except CancelledError:
7480
nonlocal properly_cancelled
75-
properly_cancelled = True
81+
cancellation_details = activity.cancellation_details()
82+
if cancellation_details:
83+
properly_cancelled = cancellation_details.cancel_requested
84+
else:
85+
properly_cancelled = False
7686

7787
env = ActivityEnvironment()
7888
# Set heartbeat handler to add to list
@@ -84,7 +94,9 @@ def via_thread():
8494
waiting.wait()
8595
# Cancel and confirm done
8696
time.sleep(1)
87-
env.cancel()
97+
env.cancel(
98+
cancellation_details=activity.ActivityCancellationDetails(cancel_requested=True)
99+
)
88100
thread.join()
89101
assert heartbeats == ["param: param1", "task, type: unknown"]
90102
assert properly_cancelled

0 commit comments

Comments
 (0)