-
Notifications
You must be signed in to change notification settings - Fork 565
[tests] Improve LinkSdk.DllImportTest.PingSend to try to ping multiple hosts. #25147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,9 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Net.NetworkInformation; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Linq; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Threading.Tasks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if !__MACOS__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using UIKit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -78,10 +81,17 @@ public void Sqlite3 () | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [Test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void PingSend () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var p = new Ping (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // support was implemented with https://github.com/dotnet/runtime/pull/52240 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var reply = p.Send ("localhost"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assert.That (reply.Status, Is.EqualTo (IPStatus.Success), "Pong"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var hosts = new string [] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "localhost", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "www.microsoft.com", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "microsoft.com", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var timeout = TimeSpan.FromSeconds (10); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var tasks = hosts.Select (host => (new Ping ()).SendPingAsync (host, timeout)).ToArray (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assert.IsTrue (Task.WaitAll (tasks, timeout.Add (TimeSpan.FromSeconds (10))), "One or more of the ping requests timed out."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var results = tasks.Select (task => task.Result.Status).ToArray (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assert.That (results, Has.Some.EqualTo (IPStatus.Success), "Pong any host"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+91
to
+94
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var tasks = hosts.Select (host => (new Ping ()).SendPingAsync (host, timeout)).ToArray (); | |
| Assert.IsTrue (Task.WaitAll (tasks, timeout.Add (TimeSpan.FromSeconds (10))), "One or more of the ping requests timed out."); | |
| var results = tasks.Select (task => task.Result.Status).ToArray (); | |
| Assert.That (results, Has.Some.EqualTo (IPStatus.Success), "Pong any host"); | |
| var tasks = hosts.Select (host => (new Ping ()).SendPingAsync (host, timeout)).ToList (); | |
| var overallTimeout = timeout.Add (TimeSpan.FromSeconds (10)); | |
| var deadline = System.DateTime.UtcNow + overallTimeout; | |
| while (tasks.Count > 0) { | |
| var remaining = deadline - System.DateTime.UtcNow; | |
| if (remaining <= TimeSpan.Zero) | |
| break; | |
| var completed = Task.WhenAny (tasks).GetAwaiter ().GetResult (); | |
| if (!completed.Wait (remaining)) | |
| break; | |
| tasks.Remove (completed); | |
| if (completed.Status != TaskStatus.RanToCompletion) | |
| continue; | |
| if (completed.Result.Status == IPStatus.Success) | |
| return; | |
| } | |
| Assert.Fail ("Pong any host"); |
Copilot
AI
Apr 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping implements IDisposable, but the new code creates multiple instances and never disposes them. Please ensure the Ping instances are disposed after the ping tasks finish (even on failures/timeouts) to avoid leaking native resources during the test run.
| var tasks = hosts.Select (host => (new Ping ()).SendPingAsync (host, timeout)).ToArray (); | |
| Assert.IsTrue (Task.WaitAll (tasks, timeout.Add (TimeSpan.FromSeconds (10))), "One or more of the ping requests timed out."); | |
| var results = tasks.Select (task => task.Result.Status).ToArray (); | |
| Assert.That (results, Has.Some.EqualTo (IPStatus.Success), "Pong any host"); | |
| var pings = hosts.Select (host => new Ping ()).ToArray (); | |
| var tasks = pings.Zip (hosts, (ping, host) => ping.SendPingAsync (host, timeout)).ToArray (); | |
| try { | |
| Assert.IsTrue (Task.WaitAll (tasks, timeout.Add (TimeSpan.FromSeconds (10))), "One or more of the ping requests timed out."); | |
| var results = tasks.Select (task => task.Result.Status).ToArray (); | |
| Assert.That (results, Has.Some.EqualTo (IPStatus.Success), "Pong any host"); | |
| } finally { | |
| foreach (var ping in pings) | |
| ping.Dispose (); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using external hosts like
www.microsoft.com/microsoft.comintroduces a dependency on outbound network + ICMP availability (often blocked in CI, captive networks, or sandboxed environments), which can make this test flaky. If the goal is to validate Ping support, prefer loopback IPs (e.g.,127.0.0.1and::1) or other local-only targets that don’t require DNS/external connectivity.