From 2c91e749d31f1d3cf355e276ddec278d679948bf Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Wed, 18 Nov 2026 10:43:43 -0800 Subject: [PATCH] fix(tests): harden journeys against container network changes --- .../Journeys/ChatJourneyTests.cs | 55 ++++++++++++++++++- .../StandaloneWebSmokeTests.cs | 36 ++++++++---- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/dev/EssentialCSharp.Web.AppHost.Tests/Journeys/ChatJourneyTests.cs b/dev/EssentialCSharp.Web.AppHost.Tests/Journeys/ChatJourneyTests.cs index d4dbb505..ec523f4e 100644 --- a/dev/EssentialCSharp.Web.AppHost.Tests/Journeys/ChatJourneyTests.cs +++ b/dev/EssentialCSharp.Web.AppHost.Tests/Journeys/ChatJourneyTests.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using System.Text.Json; using Microsoft.Playwright; using TUnit.Playwright; @@ -105,7 +106,59 @@ await Page.RouteAsync("**/api/chat/stream", route => private static string ServerSentEvent(object payload) => $"data: {JsonSerializer.Serialize(payload)}\n\n"; - private Task GoToAsync(string path) => Page.GotoAsync(new Uri(fixture.WebUrl, path).ToString()); + + /// + /// Reloads when the app's own resources fail at the network level, e.g. net::ERR_NETWORK_CHANGED when + /// container networks change on a Linux host. HTTP error responses still fail the test. + /// + private async Task GoToAsync(string path) + { + const int maxAttempts = 3; + string url = new Uri(fixture.WebUrl, path).ToString(); + string origin = fixture.WebUrl.GetLeftPart(UriPartial.Authority); + var failures = new List(); + + for (int attempt = 1; ; attempt++) + { + var attemptFailures = new ConcurrentQueue(); + void OnRequestFailed(object? sender, IRequest request) + { + if (request.Failure != "net::ERR_ABORTED" && request.Url.StartsWith(origin, StringComparison.OrdinalIgnoreCase)) + { + attemptFailures.Enqueue($"{request.Failure} {request.Url}"); + } + } + + Page.RequestFailed += OnRequestFailed; + IResponse? response = null; + try + { + response = await Page.GotoAsync(url); + } + catch (PlaywrightException ex) when (attempt < maxAttempts && ex.Message.Contains("net::ERR_", StringComparison.Ordinal)) + { + attemptFailures.Enqueue(ex.Message); + } + finally + { + Page.RequestFailed -= OnRequestFailed; + } + + if (attemptFailures.IsEmpty) + { + return response; + } + + failures.AddRange(attemptFailures.Select(failure => $"attempt {attempt}: {failure}")); + if (attempt == maxAttempts) + { + throw new InvalidOperationException( + $"'{path}' kept failing to load after {maxAttempts} attempts:{Environment.NewLine}{string.Join(Environment.NewLine, failures)}"); + } + + Console.WriteLine($"Reloading '{path}' after network failures: {string.Join("; ", attemptFailures)}"); + } + } private async Task LogInAsync(TestUser user) { diff --git a/dev/EssentialCSharp.Web.AppHost.Tests/StandaloneWebSmokeTests.cs b/dev/EssentialCSharp.Web.AppHost.Tests/StandaloneWebSmokeTests.cs index 9b201690..f9b9cf52 100644 --- a/dev/EssentialCSharp.Web.AppHost.Tests/StandaloneWebSmokeTests.cs +++ b/dev/EssentialCSharp.Web.AppHost.Tests/StandaloneWebSmokeTests.cs @@ -52,6 +52,30 @@ public async Task HealthEndpoint_FreshStart_ReturnsOk(string path) await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); } + [Test] + public async Task PlaceholderChapter_FreshStart_RendersRepositoryContent() + { + using var client = fixture.CreateHttpClient("essentialcsharp-web", "http"); + + using var response = await client.GetAsync("/hello-world"); + string body = await response.Content.ReadAsStringAsync(); + + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); + await Assert.That(body).Contains("Hello, World"); + } +} + +/// +/// Starting a second AppHost creates container networks, and on Linux that makes Chromium abort in-flight +/// requests with net::ERR_NETWORK_CHANGED. A parallel group runs after all ungrouped tests, so this +/// can't break the browser journeys. +/// +[Category("Docker")] +[RequiresLinuxContainers] +[ParallelGroup(nameof(SecondAppHostTests))] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class SecondAppHostTests(Journeys.JourneyFixture fixture) +{ [Test] public async Task SecondAppHost_DynamicPorts_RunsAlongsideFirst() { @@ -66,16 +90,4 @@ public async Task SecondAppHost_DynamicPorts_RunsAlongsideFirst() await Assert.That(firstResponse.StatusCode).IsEqualTo(HttpStatusCode.OK); await Assert.That(secondResponse.StatusCode).IsEqualTo(HttpStatusCode.OK); } - - [Test] - public async Task PlaceholderChapter_FreshStart_RendersRepositoryContent() - { - using var client = fixture.CreateHttpClient("essentialcsharp-web", "http"); - - using var response = await client.GetAsync("/hello-world"); - string body = await response.Content.ReadAsStringAsync(); - - await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); - await Assert.That(body).Contains("Hello, World"); - } }