Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ async def env(env_type: str) -> AsyncGenerator[WorkflowEnvironment, None]:
"frontend.activityAPIsEnabled=true",
"--dynamic-config-value",
"frontend.enableCancelWorkerPollsOnShutdown=true",
# The server can miss a poll that comes right after `ShutdownWorker`, and the Worker then
# waits until that poll expires. A short expiration keeps such a shutdown fast.
"--dynamic-config-value",
'matching.longPollExpirationInterval="5s"',
"--dynamic-config-value",
"frontend.WorkerHeartbeatsEnabled=true",
"--dynamic-config-value",
Expand Down
67 changes: 35 additions & 32 deletions tests/worker/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,9 @@ async def test_worker_with_worker_deployment_config(
await set_current_deployment_version(
client, describe_resp.conflict_token, worker_v1
)
await wait_for_worker_deployment_routing_config_propagation(
client, deployment_name, worker_v1.build_id
)

# Start workflow 1 which will use the 1.0 worker on auto-upgrade
wf1 = await client.start_workflow(
Expand All @@ -973,6 +976,9 @@ async def test_worker_with_worker_deployment_config(
await set_current_deployment_version(
client, describe_resp2.conflict_token, worker_v2
)
await wait_for_worker_deployment_routing_config_propagation(
client, deployment_name, worker_v2.build_id
)

wf2 = await client.start_workflow(
DeploymentVersioningWorkflowV2Pinned.run,
Expand All @@ -985,6 +991,9 @@ async def test_worker_with_worker_deployment_config(
await set_current_deployment_version(
client, describe_resp3.conflict_token, worker_v3
)
await wait_for_worker_deployment_routing_config_propagation(
client, deployment_name, worker_v3.build_id
)

wf3 = await client.start_workflow(
DeploymentVersioningWorkflowV3AutoUpgrade.run,
Expand Down Expand Up @@ -1149,6 +1158,9 @@ async def _test_worker_deployment_dynamic_workflow(
await set_current_deployment_version(
client, describe_resp.conflict_token, worker_v1
)
await wait_for_worker_deployment_routing_config_propagation(
client, deployment_name, worker_v1.build_id
)

wf = await client.start_workflow(
"cooldynamicworkflow",
Expand Down Expand Up @@ -1262,6 +1274,9 @@ async def test_workflows_can_use_default_versioning_behavior(
await set_current_deployment_version(
client, describe_resp.conflict_token, worker_v1
)
await wait_for_worker_deployment_routing_config_propagation(
client, deployment_name, worker_v1.build_id
)

wf = await client.start_workflow(
NoVersioningAnnotationWorkflow.run,
Expand Down Expand Up @@ -1360,6 +1375,9 @@ async def test_workflows_can_use_versioning_override(
await set_current_deployment_version(
client, describe_resp.conflict_token, worker_v1
)
await wait_for_worker_deployment_routing_config_propagation(
client, deployment_name, worker_v1.build_id
)

handle = await client.start_workflow(
DeploymentVersioningWorkflowV1AutoUpgrade.run,
Expand Down Expand Up @@ -1651,9 +1669,8 @@ async def wait_for_worker_deployment_routing_config_propagation(
expected_ramping_percentage: float | None = None,
) -> None:
"""Wait for routing config to be propagated to all task queues."""
import temporalio.api.enums.v1

async def check() -> bool:
async def check() -> None:
resp = await client.workflow_service.describe_worker_deployment(
DescribeWorkerDeploymentRequest(
namespace=client.namespace,
Expand All @@ -1662,38 +1679,24 @@ async def check() -> bool:
retry=True,
)
routing_config = resp.worker_deployment_info.routing_config
if (
assert (
routing_config.current_deployment_version.build_id
!= expected_current_build_id
):
return False
if (
== expected_current_build_id
)
assert (
routing_config.ramping_deployment_version.build_id
!= expected_ramping_build_id
):
return False
if (
expected_ramping_percentage is not None
and routing_config.ramping_version_percentage != expected_ramping_percentage
):
return False
state = resp.worker_deployment_info.routing_config_update_state
if (
state
== temporalio.api.enums.v1.RoutingConfigUpdateState.ROUTING_CONFIG_UPDATE_STATE_COMPLETED
):
return True
if (
state
== temporalio.api.enums.v1.RoutingConfigUpdateState.ROUTING_CONFIG_UPDATE_STATE_UNSPECIFIED
):
return True # unimplemented
if (
state
== temporalio.api.enums.v1.RoutingConfigUpdateState.ROUTING_CONFIG_UPDATE_STATE_IN_PROGRESS
):
return False
return False
== expected_ramping_build_id
)
if expected_ramping_percentage is not None:
assert (
routing_config.ramping_version_percentage == expected_ramping_percentage
)
# The set calls can return before the task queues get the new routing. A server that
# doesn't report this state leaves it unspecified.
assert (
resp.worker_deployment_info.routing_config_update_state
!= temporalio.api.enums.v1.RoutingConfigUpdateState.ROUTING_CONFIG_UPDATE_STATE_IN_PROGRESS
)

await assert_eventually(check)

Expand Down
26 changes: 20 additions & 6 deletions tests/worker/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,15 @@ def started(self) -> bool:
return self._started


@workflow.defn
class WaitForCancelWorkflow:
@workflow.run
async def run(self) -> None:
# A timer would not hold the Workflow open, because a time-skipping server can fire it
# at once. Only a cancel ends this wait.
await workflow.wait_condition(lambda: False)


async def test_workflow_simple_cancel(client: Client):
async with new_worker(client, LongSleepWorkflow) as worker:
handle = await client.start_workflow(
Expand Down Expand Up @@ -1932,12 +1941,12 @@ async def run(self, use_execute: bool) -> None:
if use_execute:
self._task = asyncio.create_task(
workflow.execute_child_workflow(
LongSleepWorkflow.run, id=f"{workflow.info().workflow_id}_child"
WaitForCancelWorkflow.run, id=f"{workflow.info().workflow_id}_child"
)
)
else:
self._task = await workflow.start_child_workflow(
LongSleepWorkflow.run, id=f"{workflow.info().workflow_id}_child"
WaitForCancelWorkflow.run, id=f"{workflow.info().workflow_id}_child"
)
self._ready = True
await self._task
Expand All @@ -1954,7 +1963,7 @@ async def cancel_child(self) -> None:

@pytest.mark.parametrize("use_execute", [True, False])
async def test_workflow_cancel_child_started(client: Client, use_execute: bool):
async with new_worker(client, CancelChildWorkflow, LongSleepWorkflow) as worker:
async with new_worker(client, CancelChildWorkflow, WaitForCancelWorkflow) as worker:
# Start workflow
handle = await client.start_workflow(
CancelChildWorkflow.run,
Expand All @@ -1964,7 +1973,12 @@ async def test_workflow_cancel_child_started(client: Client, use_execute: bool):
)

await assert_workflow_exists_eventually(
client, LongSleepWorkflow.run, f"{handle.id}_child"
client, WaitForCancelWorkflow.run, f"{handle.id}_child"
)
# The child can exist before the parent sees it start. A Signal in that same Workflow
# Task runs its handler before `run` resumes, so wait until the parent holds the child.
await assert_eq_eventually(
True, lambda: handle.query(CancelChildWorkflow.ready)
)
# Send cancel signal and wait on the handle
await handle.signal(CancelChildWorkflow.cancel_child)
Expand Down Expand Up @@ -2296,7 +2310,7 @@ async def activity():
async def child(id: str):
nonlocal events
try:
await workflow.execute_child_workflow(LongSleepWorkflow.run, id=id)
await workflow.execute_child_workflow(WaitForCancelWorkflow.run, id=id)
events.append("child success")
except ChildWorkflowError as err:
if isinstance(err.cause, CancelledError):
Expand All @@ -2323,7 +2337,7 @@ async def child(id: str):

async def test_workflow_cancel_multi(client: Client):
async with new_worker(
client, MultiCancelWorkflow, LongSleepWorkflow, activities=[wait_cancel]
client, MultiCancelWorkflow, WaitForCancelWorkflow, activities=[wait_cancel]
) as worker:
results = await client.execute_workflow(
MultiCancelWorkflow.run,
Expand Down
Loading