@@ -40,11 +40,12 @@ public async Task WaitForExternalEvent_IsDelivered()
4040
4141 string instanceId = await host . Client . ScheduleNewOrchestrationInstanceAsync ( orchestratorName ) ;
4242
43- // Give the instance a moment to start before raising the event.
44- await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
43+ using CancellationTokenSource cts = new ( TimeSpan . FromSeconds ( 30 ) ) ;
44+
45+ // Wait for the orchestration to start before raising the event.
46+ await host . Client . WaitForInstanceStartAsync ( instanceId , cts . Token ) ;
4547 await host . Client . RaiseEventAsync ( instanceId , "MyEvent" , "hello" ) ;
4648
47- using CancellationTokenSource cts = new ( TimeSpan . FromSeconds ( 30 ) ) ;
4849 OrchestrationMetadata metadata = await host . Client . WaitForInstanceCompletionAsync (
4950 instanceId , getInputsAndOutputs : true , cts . Token ) ;
5051
@@ -65,7 +66,8 @@ public async Task WaitForExternalEvent_WithTimeout_EventWins()
6566 tasks . AddOrchestratorFunc < string > ( orchestratorName , async ctx =>
6667 {
6768 using CancellationTokenSource timerCts = new ( ) ;
68- Task < string > eventTask = ctx . WaitForExternalEvent < string > ( "MyEvent" ) ;
69+ using CancellationTokenSource eventCts = new ( ) ;
70+ Task < string > eventTask = ctx . WaitForExternalEvent < string > ( "MyEvent" , eventCts . Token ) ;
6971 Task timerTask = ctx . CreateTimer ( TimeSpan . FromMinutes ( 5 ) , timerCts . Token ) ;
7072 Task winner = await Task . WhenAny ( eventTask , timerTask ) ;
7173 if ( winner == eventTask )
@@ -75,17 +77,21 @@ public async Task WaitForExternalEvent_WithTimeout_EventWins()
7577 return await eventTask ;
7678 }
7779
80+ // Cancel the losing external-event wait, mirroring the SDK's WaitForExternalEvent(name, timeout) helper.
81+ eventCts . Cancel ( ) ;
7882 return "timeout" ;
7983 } ) ;
8084 } ) ;
8185
8286 string instanceId = await host . Client . ScheduleNewOrchestrationInstanceAsync ( orchestratorName ) ;
8387
84- // Raise the event shortly after start, while the 5-minute timer is still pending.
85- await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
88+ using CancellationTokenSource cts = new ( TimeSpan . FromSeconds ( 30 ) ) ;
89+
90+ // Wait for the orchestration to start (and begin waiting on the event) before raising it,
91+ // while the 5-minute timer is still pending.
92+ await host . Client . WaitForInstanceStartAsync ( instanceId , cts . Token ) ;
8693 await host . Client . RaiseEventAsync ( instanceId , "MyEvent" , "event" ) ;
8794
88- using CancellationTokenSource cts = new ( TimeSpan . FromSeconds ( 30 ) ) ;
8995 OrchestrationMetadata metadata = await host . Client . WaitForInstanceCompletionAsync (
9096 instanceId , getInputsAndOutputs : true , cts . Token ) ;
9197
@@ -105,7 +111,8 @@ public async Task WaitForExternalEvent_WithTimeout_TimerWins()
105111 tasks . AddOrchestratorFunc < string > ( orchestratorName , async ctx =>
106112 {
107113 using CancellationTokenSource timerCts = new ( ) ;
108- Task < string > eventTask = ctx . WaitForExternalEvent < string > ( "MyEvent" ) ;
114+ using CancellationTokenSource eventCts = new ( ) ;
115+ Task < string > eventTask = ctx . WaitForExternalEvent < string > ( "MyEvent" , eventCts . Token ) ;
109116 Task timerTask = ctx . CreateTimer ( TimeSpan . FromSeconds ( 2 ) , timerCts . Token ) ;
110117 Task winner = await Task . WhenAny ( eventTask , timerTask ) ;
111118 if ( winner == eventTask )
@@ -114,6 +121,8 @@ public async Task WaitForExternalEvent_WithTimeout_TimerWins()
114121 return await eventTask ;
115122 }
116123
124+ // The timer won: cancel the losing external-event wait so no wait is left outstanding.
125+ eventCts . Cancel ( ) ;
117126 return "timeout" ;
118127 } ) ;
119128 } ) ;
0 commit comments