Skip to content

Commit 9065b8f

Browse files
authored
Fix flaky ResolverGather_TimeoutFromPrimaryRepositoryThrows test (#7242)
1 parent 36a9adb commit 9065b8f

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

test/NuGet.Core.Tests/NuGet.PackageManagement.Test/ResolverGatherTests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,21 @@ public async Task ResolverGather_TimeoutFromPrimaryRepositoryThrows()
5959
ResolutionContext = new ResolutionContext()
6060
};
6161

62-
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));
62+
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));
6363

6464
// Act and Assert
65-
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
65+
// Under CPU contention, cancellation checks in the main loop may fire
66+
// before worker tasks convert OperationCanceledException to
67+
// InvalidOperationException. Both outcomes correctly indicate failure.
68+
Exception exception = await Record.ExceptionAsync(async () =>
6669
{
6770
await ResolverGather.GatherAsync(context, cts.Token);
6871
});
72+
73+
Assert.NotNull(exception);
74+
Assert.True(
75+
exception is InvalidOperationException || exception is OperationCanceledException,
76+
$"Expected InvalidOperationException or OperationCanceledException but got {exception.GetType().Name}: {exception.Message}");
6977
}
7078

7179
[Fact]
@@ -108,7 +116,7 @@ public async Task ResolverGather_TimeoutFromSecondaryRepositoryIgnored()
108116
ResolutionContext = new ResolutionContext()
109117
};
110118

111-
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(5000));
119+
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(5000));
112120

113121
// Act
114122
var results = await ResolverGather.GatherAsync(context, cts.Token);

0 commit comments

Comments
 (0)