diff --git a/src/libraries/System.Net.Ping/tests/FunctionalTests/UnixPingUtilityTests.cs b/src/libraries/System.Net.Ping/tests/FunctionalTests/UnixPingUtilityTests.cs index cafc675f80722..b34f45bdd881f 100644 --- a/src/libraries/System.Net.Ping/tests/FunctionalTests/UnixPingUtilityTests.cs +++ b/src/libraries/System.Net.Ping/tests/FunctionalTests/UnixPingUtilityTests.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Xunit; +using Microsoft.DotNet.XUnitExtensions; namespace System.Net.NetworkInformation.Tests { @@ -19,7 +20,7 @@ public class UnixPingUtilityTests { private const int IcmpHeaderLengthInBytes = 8; - [Theory] + [ConditionalTheory] [InlineData(0)] [InlineData(100)] [InlineData(1000)] @@ -32,11 +33,24 @@ public static void TimeoutIsRespected(int timeout) p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardOutput = true; + bool destinationNetUnreachable = false; + p.OutputDataReceived += delegate (object sendingProcess, DataReceivedEventArgs outputLine) + { + if (outputLine.Data?.Contains("Destination Net Unreachable", StringComparison.OrdinalIgnoreCase) == true) + destinationNetUnreachable = true; + }; + Stopwatch stopWatch = Stopwatch.StartNew(); p.Start(); + p.BeginOutputReadLine(); p.WaitForExit(); + if (destinationNetUnreachable) + { + throw new SkipTestException($"Network doesn't route {TestSettings.UnreachableAddress}, skipping test."); + } + //ensure that the process takes longer than or within 10ms of 'timeout', with a 5s maximum Assert.InRange(stopWatch.ElapsedMilliseconds, timeout - 10, 5000); }