From 01150adba3f6db1b17ad86dc45ac3f70d13f8f8d Mon Sep 17 00:00:00 2001 From: Nalu Tripician <27316859+NaluTripician@users.noreply.github.com> Date: Thu, 19 Mar 2026 13:00:43 -0700 Subject: [PATCH] [Internal] Tests: Fixes AppCancellationDuringHedging with deterministic synchronization Replaces timing-dependent Task.Delay cancellation pattern with deterministic TaskCompletionSource + ct.Register() approach. Previously used 10ms threshold + 15ms cancel delay (5ms margin), which was unreliable on loaded CI agents. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../AvailabilityStrategyUnitTests.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/AvailabilityStrategyUnitTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/AvailabilityStrategyUnitTests.cs index 8123290a06..e2d91daa3c 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/AvailabilityStrategyUnitTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/AvailabilityStrategyUnitTests.cs @@ -281,8 +281,8 @@ public async Task AppCancellationDuringHedging_DoesNotSpawnNewHedgeRequests() { // Arrange CrossRegionHedgingAvailabilityStrategy availabilityStrategy = new CrossRegionHedgingAvailabilityStrategy( - threshold: TimeSpan.FromMilliseconds(10), - thresholdStep: TimeSpan.FromMilliseconds(10)); + threshold: TimeSpan.FromMilliseconds(100), + thresholdStep: TimeSpan.FromMilliseconds(100)); using RequestMessage request = CreateReadRequest(); using CosmosClient mockCosmosClient = CreateMockClientWithRegions(3); @@ -296,19 +296,16 @@ public async Task AppCancellationDuringHedging_DoesNotSpawnNewHedgeRequests() if (callNumber == 1) { - // First request: cancel the app token after a brief delay + // First request: cancel the app token immediately // This simulates an e2e timeout scenario - _ = Task.Delay(15).ContinueWith(_ => appCts.Cancel()); + appCts.Cancel(); + } - // Then wait - this will be cancelled - try - { - await Task.Delay(TimeSpan.FromSeconds(30), ct); - } - catch (OperationCanceledException) - { - throw; - } + // All requests block deterministically until cancelled via the token + TaskCompletionSource tcs = new TaskCompletionSource(); + using (ct.Register(() => tcs.TrySetCanceled(ct))) + { + await tcs.Task; } return new ResponseMessage(HttpStatusCode.OK);