@@ -1456,4 +1456,54 @@ public async Task WaitForExternalEvent_WithTimeoutAndCancellationToken_TimeoutWi
14561456 string ? result = metadata . ReadOutputAs < string > ( ) ;
14571457 Assert . Equal ( "Timeout occurred" , result ) ;
14581458 }
1459+
1460+ [ Fact ]
1461+ public async Task WaitForExternalEvent_WithTimeoutAndCancellationToken_ExternalCancellationWins ( )
1462+ {
1463+ const string EventName = "TestEvent" ;
1464+ TaskName orchestratorName = nameof ( WaitForExternalEvent_WithTimeoutAndCancellationToken_ExternalCancellationWins ) ;
1465+
1466+ await using HostTestLifetime server = await this . StartWorkerAsync ( b =>
1467+ {
1468+ b . AddTasks ( tasks => tasks . AddOrchestratorFunc ( orchestratorName , async ctx =>
1469+ {
1470+ using CancellationTokenSource cts = new ( ) ;
1471+
1472+ // Create a timer that will fire and trigger cancellation
1473+ Task cancelTrigger = ctx . CreateTimer ( TimeSpan . FromMilliseconds ( 100 ) , CancellationToken . None ) ;
1474+
1475+ // Wait for external event with a long timeout
1476+ Task < string > eventTask = ctx . WaitForExternalEvent < string > ( EventName , TimeSpan . FromDays ( 7 ) , cts . Token ) ;
1477+
1478+ // Wait for either the cancel trigger or the event
1479+ Task winner = await Task . WhenAny ( cancelTrigger , eventTask ) ;
1480+
1481+ if ( winner == cancelTrigger )
1482+ {
1483+ // Cancel the external cancellation token
1484+ cts . Cancel ( ) ;
1485+ }
1486+
1487+ try
1488+ {
1489+ string result = await eventTask ;
1490+ return $ "Event: { result } ";
1491+ }
1492+ catch ( OperationCanceledException )
1493+ {
1494+ return "External cancellation occurred" ;
1495+ }
1496+ } ) ) ;
1497+ } ) ;
1498+
1499+ string instanceId = await server . Client . ScheduleNewOrchestrationInstanceAsync ( orchestratorName ) ;
1500+
1501+ OrchestrationMetadata metadata = await server . Client . WaitForInstanceCompletionAsync (
1502+ instanceId , getInputsAndOutputs : true , this . TimeoutToken ) ;
1503+ Assert . NotNull ( metadata ) ;
1504+ Assert . Equal ( OrchestrationRuntimeStatus . Completed , metadata . RuntimeStatus ) ;
1505+
1506+ string ? result = metadata . ReadOutputAs < string > ( ) ;
1507+ Assert . Equal ( "External cancellation occurred" , result ) ;
1508+ }
14591509}
0 commit comments