@@ -5210,7 +5210,58 @@ async def run(self) -> None:
52105210 await asyncio .sleep (0.1 )
52115211
52125212
5213- async def test_workflow_replace_worker_client (client : Client ):
5213+ async def test_workflow_replace_worker_client (client : Client , env : WorkflowEnvironment ):
5214+ if env .supports_time_skipping :
5215+ pytest .skip ("Only testing against two real servers" )
5216+ # We are going to start a second ephemeral server and then replace the
5217+ # client. So we will start a no-cache ticking workflow with the current
5218+ # client and confirm it has accomplished at least one task. Then we will
5219+ # start another on the other client, and confirm it gets started too. Then
5220+ # we will terminate both. We have to use a ticking workflow with only one
5221+ # poller to force a quick re-poll to recognize our client change quickly (as
5222+ # opposed to just waiting the minute for poll timeout).
5223+ async with await WorkflowEnvironment .start_local (
5224+ runtime = client .service_client .config .runtime ,
5225+ dev_server_download_version = DEV_SERVER_DOWNLOAD_VERSION ,
5226+ ) as other_env :
5227+ # Start both workflows on different servers
5228+ task_queue = f"tq-{ uuid .uuid4 ()} "
5229+ handle1 = await client .start_workflow (
5230+ TickingWorkflow .run , id = f"wf-{ uuid .uuid4 ()} " , task_queue = task_queue
5231+ )
5232+ handle2 = await other_env .client .start_workflow (
5233+ TickingWorkflow .run , id = f"wf-{ uuid .uuid4 ()} " , task_queue = task_queue
5234+ )
5235+
5236+ async def any_task_completed (handle : WorkflowHandle ) -> bool :
5237+ async for e in handle .fetch_history_events ():
5238+ if e .HasField ("workflow_task_completed_event_attributes" ):
5239+ return True
5240+ return False
5241+
5242+ # Now start the worker on the first env
5243+ async with Worker (
5244+ client ,
5245+ task_queue = task_queue ,
5246+ workflows = [TickingWorkflow ],
5247+ max_cached_workflows = 0 ,
5248+ max_concurrent_workflow_task_polls = 1 ,
5249+ ) as worker :
5250+ # Confirm the first ticking workflow has completed a task but not
5251+ # the second
5252+ await assert_eq_eventually (True , lambda : any_task_completed (handle1 ))
5253+ assert not await any_task_completed (handle2 )
5254+ # Now replace the client, which should be used fairly quickly
5255+ # because we should have timer-done poll completions every 100ms
5256+ worker .client = other_env .client
5257+ # Now confirm the other workflow has started
5258+ await assert_eq_eventually (True , lambda : any_task_completed (handle2 ))
5259+ # Terminate both
5260+ await handle1 .terminate ()
5261+ await handle2 .terminate ()
5262+
5263+
5264+ async def test_workflow_replace_worker_client_diff_runtimes_fail (client : Client ):
52145265 other_runtime = Runtime (telemetry = TelemetryConfig ())
52155266 other_client = await Client .connect (
52165267 client .service_client .config .target_host ,
0 commit comments