From 7122fa0bfce65812129a9d9891b4f6ace81ecd2a Mon Sep 17 00:00:00 2001 From: Radek Zikmund Date: Tue, 31 May 2022 21:19:55 +0200 Subject: [PATCH 1/2] Fix flakyness of DnsGetHostAddresses_PostCancelledToken_Throws --- .../tests/FunctionalTests/GetHostAddressesTest.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs index 10b588ebda6c8f..3914e61bcc2c20 100644 --- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs +++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs @@ -185,12 +185,16 @@ public async Task DnsGetHostAddresses_PostCancelledToken_Throws() Task task = Dns.GetHostAddressesAsync(TestSettings.UncachedHost, cts.Token); - // This test might flake if the cancellation token takes too long to trigger: + // 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. cts.Cancel(); - OperationCanceledException oce = await Assert.ThrowsAnyAsync(() => task); - Assert.Equal(cts.Token, oce.CancellationToken); + 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 From 7e3fdeb3aa69bafc88626e1f8819e6876dc0a36f Mon Sep 17 00:00:00 2001 From: Radek Zikmund Date: Tue, 31 May 2022 21:23:01 +0200 Subject: [PATCH 2/2] fixup! Fix flakyness of DnsGetHostAddresses_PostCancelledToken_Throws --- .../tests/FunctionalTests/GetHostAddressesTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs index 3914e61bcc2c20..02cb60e610080e 100644 --- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs +++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs @@ -184,11 +184,11 @@ public async Task DnsGetHostAddresses_PostCancelledToken_Throws() using var cts = new CancellationTokenSource(); Task task = Dns.GetHostAddressesAsync(TestSettings.UncachedHost, cts.Token); + cts.Cancel(); // 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. - cts.Cancel(); - + // since the host does not exist, both cases throw an exception. Exception ex = await Assert.ThrowsAnyAsync(() => task); if (ex is OperationCanceledException oce) {