Skip to content

Commit d5c6951

Browse files
committed
Fix test assertions
1 parent 763f3ce commit d5c6951

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

tests/nexus/test_workflow_caller_cancellation_types.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from temporalio.api.history.v1 import HistoryEvent
1313
from temporalio.client import (
1414
WorkflowExecutionStatus,
15-
WorkflowFailureError,
1615
WorkflowHandle,
1716
)
1817
from temporalio.testing import WorkflowEnvironment
@@ -91,58 +90,64 @@ async def check_behavior_for_abandon(
9190
handler_wf: WorkflowHandle,
9291
) -> None:
9392
"""
94-
Check that backing workflow has not received a cancellation request and has not been
95-
canceled (is still running) and does not receive a cancellation request.
93+
Check that a cancellation request is not sent.
9694
"""
9795
await asyncio.sleep(0.5)
9896

9997
await print_interleaved_histories(caller_wf, handler_wf)
10098

10199
handler_status = (await handler_wf.describe()).status
102100
assert handler_status == WorkflowExecutionStatus.RUNNING
101+
await _assert_event_subsequence(
102+
[
103+
(caller_wf, EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED),
104+
(caller_wf, EventType.EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED),
105+
]
106+
)
103107
assert not await _has_event(
104108
caller_wf,
105109
EventType.EVENT_TYPE_NEXUS_OPERATION_CANCEL_REQUESTED,
106110
)
107-
assert not await _has_event(
108-
handler_wf,
109-
EventType.EVENT_TYPE_WORKFLOW_EXECUTION_CANCEL_REQUESTED,
110-
)
111111

112112

113113
async def check_behavior_for_try_cancel(
114114
caller_wf: WorkflowHandle,
115115
handler_wf: WorkflowHandle,
116116
) -> None:
117117
"""
118-
Check that backing workflow has not received a cancellation request initially
119-
and is still running, but eventually does receive a cancellation request
118+
Check that a cancellation request is sent and the caller workflow exits before the operation is
119+
canceled.
120120
"""
121121
handler_status = (await handler_wf.describe()).status
122122
assert handler_status == WorkflowExecutionStatus.RUNNING
123-
124-
await print_interleaved_histories(caller_wf, handler_wf)
125-
126-
# TODO(nexus-preview): I was expecting the handler workflow to eventually receive a
127-
# cancellation request, but it seems not to.
128-
# await _assert_has_event_eventually(
129-
# wf_handle, EventType.EVENT_TYPE_WORKFLOW_EXECUTION_CANCEL_REQUESTED
130-
# )
123+
await _assert_event_subsequence(
124+
[
125+
(caller_wf, EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED),
126+
(caller_wf, EventType.EVENT_TYPE_NEXUS_OPERATION_CANCEL_REQUESTED),
127+
(caller_wf, EventType.EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED),
128+
]
129+
)
130+
# This event would be seen if the caller workflow stayed alive longer.
131+
assert not await _has_event(
132+
handler_wf,
133+
EventType.EVENT_TYPE_WORKFLOW_EXECUTION_CANCEL_REQUESTED,
134+
)
131135

132136

133137
async def check_behavior_for_wait_cancellation_completed(
134138
caller_wf: WorkflowHandle,
135139
handler_wf: WorkflowHandle,
136140
) -> None:
137141
"""
138-
Check that backing workflow received a cancellation request and has been canceled (is
139-
not running)
142+
Check that a cancellation request is sent and the caller workflow exits after the operation is
143+
canceled.
140144
"""
141145
handler_status = (await handler_wf.describe()).status
142146
assert handler_status == WorkflowExecutionStatus.CANCELED
143147
await print_interleaved_histories(caller_wf, handler_wf)
144-
await _assert_event_sequence(
148+
await _assert_event_subsequence(
145149
[
150+
(caller_wf, EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED),
146151
(caller_wf, EventType.EVENT_TYPE_NEXUS_OPERATION_CANCEL_REQUESTED),
147152
(
148153
handler_wf,
@@ -197,14 +202,6 @@ async def test_cancellation_type(
197202
operation_token
198203
)._to_client_workflow_handle(client)
199204

200-
# Under ABANDON a cancellation request will never be sent hence the handler workflow will
201-
# never complete.
202-
if input.cancellation_type != workflow.NexusOperationCancellationType.ABANDON:
203-
try:
204-
await handler_wf.result()
205-
except WorkflowFailureError:
206-
pass
207-
208205
if input.cancellation_type == workflow.NexusOperationCancellationType.ABANDON:
209206
await check_behavior_for_abandon(caller_wf, handler_wf)
210207
elif (
@@ -228,7 +225,7 @@ async def _has_event(wf_handle: WorkflowHandle, event_type: EventType.ValueType)
228225
return False
229226

230227

231-
async def _assert_event_sequence(
228+
async def _assert_event_subsequence(
232229
expected_events: list[tuple[WorkflowHandle, EventType.ValueType]],
233230
) -> None:
234231
"""

0 commit comments

Comments
 (0)