Skip to content

Commit c5ca36e

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

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-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

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def via_create_task():
2626
await asyncio.Future()
2727
raise RuntimeError("Unreachable")
2828
except asyncio.CancelledError:
29-
activity.heartbeat("cancelled")
29+
activity.heartbeat(f"cancelled={activity.cancellation_details().cancel_requested}",)
3030
return "done"
3131

3232
env = ActivityEnvironment()
@@ -37,9 +37,9 @@ async def via_create_task():
3737
task = asyncio.create_task(env.run(do_stuff, "param1"))
3838
await waiting.wait()
3939
# Cancel and confirm done
40-
env.cancel()
40+
env.cancel(cancellation_details=activity.ActivityCancellationDetails(cancel_requested=True))
4141
assert "done" == await task
42-
assert heartbeats == ["param: param1", "task, type: unknown", "cancelled"]
42+
assert heartbeats == ["param: param1", "task, type: unknown", "cancelled=True"]
4343

4444

4545
def test_activity_env_sync():
@@ -72,7 +72,7 @@ def via_thread():
7272
raise RuntimeError("Unexpected")
7373
except CancelledError:
7474
nonlocal properly_cancelled
75-
properly_cancelled = True
75+
properly_cancelled = activity.cancellation_details().cancel_requested
7676

7777
env = ActivityEnvironment()
7878
# Set heartbeat handler to add to list
@@ -84,7 +84,7 @@ def via_thread():
8484
waiting.wait()
8585
# Cancel and confirm done
8686
time.sleep(1)
87-
env.cancel()
87+
env.cancel(cancellation_details=activity.ActivityCancellationDetails(cancel_requested=True))
8888
thread.join()
8989
assert heartbeats == ["param: param1", "task, type: unknown"]
9090
assert properly_cancelled

0 commit comments

Comments
 (0)