From a466063cd273e88276abe9d84eb28dccaf50d633 Mon Sep 17 00:00:00 2001 From: Radek Zikmund <32671551+rzikm@users.noreply.github.com> Date: Wed, 1 Jun 2022 10:26:02 +0200 Subject: [PATCH] Fix flakyness of DnsGetHostAddresses_PostCancelledToken_Throws (#70044) * Fix flakyness of DnsGetHostAddresses_PostCancelledToken_Throws * fixup! Fix flakyness of DnsGetHostAddresses_PostCancelledToken_Throws --- .../tests/FunctionalTests/GetHostAddressesTest.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs index 99999905937c9..a522f4f5824ce 100644 --- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs +++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs @@ -183,13 +183,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