Repository: dotnet/runtime
File: src/libraries/System.Net.Ping/tests/FunctionalTests/PingTest.cs (C#)

Code before:
            Ping sender = new Ping();
            PingReply reply = await sendPing(sender, TestSettings.UnreachableAddress);
            if (reply.Status == IPStatus.DestinationNetworkUnreachable)
            {
                // A network middleware has dropped the packed and replied with DestinationNetworkUnreachable. Repeat the PING attempt on another address.
                reply = await sendPing(sender, TestSettings.UnreachableAddress2);
            }

            if (reply.Status == IPStatus.DestinationNetworkUnreachable)
            {
                // Do yet another attempt.
                reply = await sendPing(sender, TestSettings.UnreachableAddress3);
            }

            Assert.Equal(IPStatus.TimedOut, reply.Status);
        }

        [Fact]
        [OuterLoop]
        public Task Ping_TimedOut_Sync_Success()
            => Ping_TimedOut_Core((sender, address) => Task.Run(() => sender.Send(address)));

        [Fact]
        [OuterLoop]
        public Task Ping_TimedOut_EAP_Success()
            => Ping_TimedOut_Core(async (sender, address) =>
            {
                static void PingCompleted(object sender, PingCompletedEventArgs e)
                {
                    var tcs = (TaskCompletionSource<PingReply>)e.UserState;

                    if (e.Cancelled)
                    {
                        tcs.TrySetCanceled();
                    }
                    else if (e.Error != null)
                    {
                        tcs.TrySetException(e.Error);
                    }
                    else
                    {
                        tcs.TrySetResult(e.Reply);
                    }
                }
                sender.PingCompleted += PingCompleted;
                var tcs = new TaskCompletionSource<PingReply>();
                sender.SendAsync(address, tcs);
                PingReply reply = await tcs.Task;
                sender.PingCompleted -= PingCompleted;
                return reply;
            });

Review comment (suggestion):
The ConditionalFact attribute should include a condition for threading support since this test uses Task.Run which requires threading. The pattern used throughout this file for async tests is ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported)). This ensures the test is skipped on platforms without threading support.

Code after:
            Ping sender = new Ping();
            PingReply reply = await sendPing(sender, TestSettings.UnreachableAddress);
            if (reply.Status == IPStatus.DestinationNetworkUnreachable)
            {
                // A network middleware has dropped the packed and replied with DestinationNetworkUnreachable. Repeat the PING attempt on another address.
                reply = await sendPing(sender, TestSettings.UnreachableAddress2);
            }

            if (reply.Status == IPStatus.DestinationNetworkUnreachable)
            {
                // Do yet another attempt.
                reply = await sendPing(sender, TestSettings.UnreachableAddress3);
            }

            if (reply.Status == IPStatus.DestinationNetworkUnreachable)
            {
                throw new SkipTestException("Unable to verify timeouts. Skipping test.");
            }

            Assert.Equal(IPStatus.TimedOut, reply.Status);
        }

        [ConditionalFact]
        [OuterLoop]
        public Task Ping_TimedOut_Sync_Success()
            => Ping_TimedOut_Core((sender, address) => Task.Run(() => sender.Send(address)));

        [ConditionalFact]
        [OuterLoop]
        public Task Ping_TimedOut_EAP_Success()
            => Ping_TimedOut_Core(async (sender, address) =>
            {
                static void PingCompleted(object sender, PingCompletedEventArgs e)
                {
                    var tcs = (TaskCompletionSource<PingReply>)e.UserState;

                    if (e.Cancelled)
                    {
                        tcs.TrySetCanceled();
                    }
                    else if (e.Error != null)
                    {
                        tcs.TrySetException(e.Error);
                    }
                    else
                    {
                        tcs.TrySetResult(e.Reply);
                    }
                }
                sender.PingCompleted += PingCompleted;
                var tcs = new TaskCompletionSource<PingReply>();