From 78eced2b6c45ad581ae2c6c70245efccd45b0d5b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 26 Nov 2025 10:36:40 +0000 Subject: [PATCH 1/3] Initial plan From bbbd4615709bce4dbd8fbf8b73eb63b81c11d83b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 26 Nov 2025 10:51:23 +0000 Subject: [PATCH 2/3] Fix flaky test by using dynamic port allocation Co-authored-by: tg123 <170430+tg123@users.noreply.github.com> --- tests/KubernetesClient.Tests/PodExecTests.cs | 2 -- .../WebSocketTestBase.cs | 27 ++++++++++++------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/KubernetesClient.Tests/PodExecTests.cs b/tests/KubernetesClient.Tests/PodExecTests.cs index 597be477b..d37e098e8 100644 --- a/tests/KubernetesClient.Tests/PodExecTests.cs +++ b/tests/KubernetesClient.Tests/PodExecTests.cs @@ -52,8 +52,6 @@ public async Task ExecDefaultContainerStdOut() TimeSpan.FromSeconds(5)); } - await Host.StartAsync(TestCancellation).ConfigureAwait(true); - using (Kubernetes client = CreateTestClient()) { testOutput.WriteLine("Invoking exec operation..."); diff --git a/tests/KubernetesClient.Tests/WebSocketTestBase.cs b/tests/KubernetesClient.Tests/WebSocketTestBase.cs index 9c943c1b0..5a95f13c2 100644 --- a/tests/KubernetesClient.Tests/WebSocketTestBase.cs +++ b/tests/KubernetesClient.Tests/WebSocketTestBase.cs @@ -3,10 +3,12 @@ using k8s.Tests.Mock.Server; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.IO; +using System.Linq; using System.Net.WebSockets; using System.Text; using System.Threading; @@ -21,10 +23,6 @@ namespace k8s.Tests /// public abstract class WebSocketTestBase : IDisposable { - /// - /// The next server port to use. - /// - private static int nextPort = 13255; private bool disposedValue; private readonly ITestOutputHelper testOutput; @@ -39,21 +37,30 @@ protected WebSocketTestBase(ITestOutputHelper testOutput) { this.testOutput = testOutput; - int port = Interlocked.Increment(ref nextPort); - // Useful to diagnose test timeouts. TestCancellation.Register( () => testOutput.WriteLine("Test-level cancellation token has been canceled.")); - ServerBaseAddress = new Uri($"http://localhost:{port}"); - WebSocketBaseAddress = new Uri($"ws://localhost:{port}"); - + // Use port 0 to let the OS assign a free port dynamically Host = WebHost.CreateDefaultBuilder() .UseStartup() .ConfigureServices(ConfigureTestServerServices) .ConfigureLogging(ConfigureTestServerLogging) - .UseUrls(ServerBaseAddress.AbsoluteUri) + .UseUrls("http://127.0.0.1:0") .Build(); + + // Start the host to get the actual assigned port + Host.Start(); + + // Get the actual server address after binding + var serverAddress = Host.ServerFeatures.Get()?.Addresses.FirstOrDefault(); + if (serverAddress == null) + { + throw new InvalidOperationException("Failed to determine server address"); + } + + ServerBaseAddress = new Uri(serverAddress); + WebSocketBaseAddress = new Uri(serverAddress.Replace("http://", "ws://")); } /// From 7c8b3d86291cafae1fdb8e9a0fb554b8faf29ac6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 26 Nov 2025 10:58:19 +0000 Subject: [PATCH 3/3] Make property setters explicit for clarity Co-authored-by: tg123 <170430+tg123@users.noreply.github.com> --- tests/KubernetesClient.Tests/WebSocketTestBase.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/KubernetesClient.Tests/WebSocketTestBase.cs b/tests/KubernetesClient.Tests/WebSocketTestBase.cs index 5a95f13c2..6766cc585 100644 --- a/tests/KubernetesClient.Tests/WebSocketTestBase.cs +++ b/tests/KubernetesClient.Tests/WebSocketTestBase.cs @@ -66,12 +66,12 @@ protected WebSocketTestBase(ITestOutputHelper testOutput) /// /// The test server's base address (http://). /// - protected Uri ServerBaseAddress { get; } + protected Uri ServerBaseAddress { get; private set; } /// /// The test server's base WebSockets address (ws://). /// - protected Uri WebSocketBaseAddress { get; } + protected Uri WebSocketBaseAddress { get; private set; } /// /// The test server's web host.