forked from temporalio/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_activity.py
More file actions
124 lines (103 loc) · 3.8 KB
/
Copy pathtest_activity.py
File metadata and controls
124 lines (103 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import asyncio
import threading
import time
from contextvars import copy_context
from temporalio import activity
from temporalio.exceptions import CancelledError
from temporalio.testing import ActivityEnvironment
async def test_activity_env_async():
waiting = asyncio.Event()
async def do_stuff(param: str) -> str:
activity.heartbeat(f"param: {param}")
# Ensure it works across create_task
async def via_create_task():
activity.heartbeat(f"task, type: {activity.info().activity_type}")
await asyncio.create_task(via_create_task())
# Wait for cancel
try:
waiting.set()
await asyncio.Future()
raise RuntimeError("Unreachable")
except asyncio.CancelledError:
cancellation_details = activity.cancellation_details()
if cancellation_details:
activity.heartbeat(
f"cancelled={cancellation_details.cancel_requested}",
)
return "done"
env = ActivityEnvironment()
# Set heartbeat handler to add to list
heartbeats = []
env.on_heartbeat = lambda *args: heartbeats.append(args[0])
# Start task and wait until waiting
task = asyncio.create_task(env.run(do_stuff, "param1"))
await waiting.wait()
# Cancel and confirm done
env.cancel(
cancellation_details=activity.ActivityCancellationDetails(cancel_requested=True)
)
assert "done" == await task
assert heartbeats == ["param: param1", "task, type: unknown", "cancelled=True"]
def test_activity_env_sync():
waiting = threading.Event()
properly_cancelled = False
def do_stuff(param: str) -> None:
activity.heartbeat(f"param: {param}")
# Ensure it works across thread
context = copy_context()
def via_thread():
activity.heartbeat(f"task, type: {activity.info().activity_type}")
thread = threading.Thread(target=context.run, args=[via_thread])
thread.start()
thread.join()
# Wait for cancel
waiting.set()
try:
# Confirm shielding works
with activity.shield_thread_cancel_exception():
try:
while not activity.is_cancelled():
time.sleep(0.2)
time.sleep(0.2)
except:
raise RuntimeError("Unexpected")
except CancelledError:
nonlocal properly_cancelled
cancellation_details = activity.cancellation_details()
if cancellation_details:
properly_cancelled = cancellation_details.cancel_requested
else:
properly_cancelled = False
env = ActivityEnvironment()
# Set heartbeat handler to add to list
heartbeats = []
env.on_heartbeat = lambda *args: heartbeats.append(args[0])
# Start thread and wait until waiting
thread = threading.Thread(target=env.run, args=[do_stuff, "param1"])
thread.start()
waiting.wait()
# Cancel and confirm done
time.sleep(1)
env.cancel(
cancellation_details=activity.ActivityCancellationDetails(cancel_requested=True)
)
thread.join()
assert heartbeats == ["param: param1", "task, type: unknown"]
assert properly_cancelled
async def test_activity_env_assert():
async def assert_equals(a: str, b: str) -> None:
assert a == b
# Get out-of-env expected err
try:
await assert_equals("foo", "bar")
assert False
except Exception as err:
expected_err = err
# Get in-env actual err
try:
await ActivityEnvironment().run(assert_equals, "foo", "bar")
assert False
except Exception as err:
actual_err = err
assert type(expected_err) == type(actual_err)
assert str(expected_err) == str(actual_err)