diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs index 10b588ebda6c8f..02cb60e610080e 100644 --- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs +++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs @@ -184,13 +184,17 @@ public async Task DnsGetHostAddresses_PostCancelledToken_Throws() using var cts = new CancellationTokenSource(); Task task = Dns.GetHostAddressesAsync(TestSettings.UncachedHost, cts.Token); - - // This test might flake if the cancellation token takes too long to trigger: - // It's a race between the DNS server getting back to us and the cancellation processing. cts.Cancel(); - OperationCanceledException oce = await Assert.ThrowsAnyAsync(() => task); - Assert.Equal(cts.Token, oce.CancellationToken); + // This test is nondeterministic, the cancellation may take too long to trigger: + // It's a race between the DNS server getting back to us and the cancellation processing. + // since the host does not exist, both cases throw an exception. + Exception ex = await Assert.ThrowsAnyAsync(() => task); + if (ex is OperationCanceledException oce) + { + // canceled in time + Assert.Equal(cts.Token, oce.CancellationToken); + } } // This is a regression test for https://github.com/dotnet/runtime/issues/63552