Skip to content

[tests] Improve LinkSdk.DllImportTest.PingSend to try to ping multiple hosts.#25147

Open
rolfbjarne wants to merge 1 commit intomainfrom
dev/rolf/tests-ping-localhost
Open

[tests] Improve LinkSdk.DllImportTest.PingSend to try to ping multiple hosts.#25147
rolfbjarne wants to merge 1 commit intomainfrom
dev/rolf/tests-ping-localhost

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

Pinging localhost doesn't work for me, so try pinging a few other hosts as
well, and pass the test if any ping succeeds.

Also add a timeout, so the entire test run doesn't fail if a ping blocks for
some reason.

…e hosts.

Pinging localhost doesn't work for me, so try pinging a few other hosts as
well, and pass the test if any ping succeeds.

Also add a timeout, so the entire test run doesn't fail if a ping blocks for
some reason.
@rolfbjarne rolfbjarne enabled auto-merge (squash) April 14, 2026 12:42
@rolfbjarne rolfbjarne requested a review from Copilot April 14, 2026 12:42
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
2 pipeline(s) require an authorized user to comment /azp run to run.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the LinkSdk.DllImportTest.PingSend linker test to be more resilient by trying multiple ping targets and adding explicit timeouts, aiming to avoid hangs and reduce false failures in environments where localhost ping is unreliable.

Changes:

  • Replace single-host Ping.Send("localhost") with multiple concurrent SendPingAsync requests.
  • Add a timeout to bound how long the test can block.
  • Add assertions that at least one host ping succeeds.

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");
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Task.WaitAll (tasks, ...) waits for all pings to complete, and will also throw if any SendPingAsync task faults (e.g., DNS failure/ICMP blocked). This contradicts the intent to “pass if any ping succeeds” and can make the test fail even when one host replies. Consider waiting for completion in a way that tolerates per-host failures/timeouts (e.g., observe each task’s status/exception and only require at least one IPStatus.Success).

Suggested change
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 uses AI. Check for mistakes.
Comment on lines +86 to +88
"localhost",
"www.microsoft.com",
"microsoft.com",
Copy link

Copilot AI Apr 14, 2026

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.com introduces 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.1 and ::1) or other local-only targets that don’t require DNS/external connectivity.

Suggested change
"localhost",
"www.microsoft.com",
"microsoft.com",
"127.0.0.1",
"::1",

Copilot uses AI. Check for mistakes.
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");
Copy link

Copilot AI Apr 14, 2026

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.

Suggested change
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 ();
}

Copilot uses AI. Check for mistakes.
@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #5a2209d] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 5a2209d856d96ea827868f5bdc965a485740e5bf [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #5a2209d] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 5a2209d856d96ea827868f5bdc965a485740e5bf [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 5a2209d856d96ea827868f5bdc965a485740e5bf [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #5a2209d] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 5a2209d856d96ea827868f5bdc965a485740e5bf [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🔥 [CI Build #5a2209d] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 1 tests failed, 155 tests passed.

Failures

❌ monotouch tests (tvOS)

1 tests failed, 10 tests passed.

Failed tests

  • monotouch-test/tvOS - simulator/Release (managed static registrar, all optimizations): BuildFailure

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 12 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: 5a2209d856d96ea827868f5bdc965a485740e5bf [PR build]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants