Skip to content

Commit 520dd41

Browse files
fix(tests): harden journeys against container network changes (#1690)
## Why Main's [failed Ubuntu run](https://github.com/IntelliTect/EssentialCSharp.Web/actions/runs/44303044493/job/134797768441) dropped same-origin scripts with `net::ERR_NETWORK_CHANGED`. Without `hcaptcha-form.js`, login posted without a captcha token and the journeys timed out waiting for `#logout`. The same failure occurred before the Aspire package update in #1688. ## Approach - Move the second-AppHost smoke test into its own `ParallelGroup`, isolating its container-network creation and teardown from browser journeys without serializing the journeys themselves. - Allow journey navigation up to three attempts when same-origin resources fail at the network level. Log retries and report accumulated failures when recovery is exhausted. Intentional `ERR_ABORTED` cancellations are excluded; HTTP errors are not grounds for retrying. The second AppHost is a likely trigger, not a proven explanation for every CI occurrence. Isolation applies within this test assembly. Bounded reloads address transient failures, not continuous network churn. ## Local evidence - All 28 AppHost tests passed with no skips on Windows with Linux containers. TRX timestamps show the second AppHost starting after every journey finished. - Linux Chromium in Podman: no-churn baseline had 0/10 broken loads. Bursty container-network changes caused 3/20 broken loads without retries and 0/20 with retries. Continuous churn still exhausted retries in 15/15 loads. - Temporary fault injection reproduced the captcha rejection without recovery and authenticated successfully with recovery. Persistent script failures produced bounded diagnostics. The recovery probe's request-count assertion failed (3 requests rather than 2), despite successful login; the temporary probes were removed. The real Linux CI suite still needs to confirm the improvement; the Windows test run does not reproduce Chromium's Linux network-change behavior. ## References - [TUnit parallel-group semantics](https://tunit.dev/docs/execution/parallelism/) - [microsoft/aspire#8346](microsoft/aspire#8346), which added page reloads for the same browser error
1 parent f299dd1 commit 520dd41

2 files changed

Lines changed: 78 additions & 13 deletions

File tree

‎dev/EssentialCSharp.Web.AppHost.Tests/Journeys/ChatJourneyTests.cs‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Concurrent;
12
using System.Text.Json;
23
using Microsoft.Playwright;
34
using TUnit.Playwright;
@@ -105,7 +106,59 @@ await Page.RouteAsync("**/api/chat/stream", route =>
105106

106107
private static string ServerSentEvent(object payload) => $"data: {JsonSerializer.Serialize(payload)}\n\n";
107108

108-
private Task<IResponse?> GoToAsync(string path) => Page.GotoAsync(new Uri(fixture.WebUrl, path).ToString());
109+
110+
/// <summary>
111+
/// Reloads when the app's own resources fail at the network level, e.g. net::ERR_NETWORK_CHANGED when
112+
/// container networks change on a Linux host. HTTP error responses still fail the test.
113+
/// </summary>
114+
private async Task<IResponse?> GoToAsync(string path)
115+
{
116+
const int maxAttempts = 3;
117+
string url = new Uri(fixture.WebUrl, path).ToString();
118+
string origin = fixture.WebUrl.GetLeftPart(UriPartial.Authority);
119+
var failures = new List<string>();
120+
121+
for (int attempt = 1; ; attempt++)
122+
{
123+
var attemptFailures = new ConcurrentQueue<string>();
124+
void OnRequestFailed(object? sender, IRequest request)
125+
{
126+
if (request.Failure != "net::ERR_ABORTED" && request.Url.StartsWith(origin, StringComparison.OrdinalIgnoreCase))
127+
{
128+
attemptFailures.Enqueue($"{request.Failure} {request.Url}");
129+
}
130+
}
131+
132+
Page.RequestFailed += OnRequestFailed;
133+
IResponse? response = null;
134+
try
135+
{
136+
response = await Page.GotoAsync(url);
137+
}
138+
catch (PlaywrightException ex) when (attempt < maxAttempts && ex.Message.Contains("net::ERR_", StringComparison.Ordinal))
139+
{
140+
attemptFailures.Enqueue(ex.Message);
141+
}
142+
finally
143+
{
144+
Page.RequestFailed -= OnRequestFailed;
145+
}
146+
147+
if (attemptFailures.IsEmpty)
148+
{
149+
return response;
150+
}
151+
152+
failures.AddRange(attemptFailures.Select(failure => $"attempt {attempt}: {failure}"));
153+
if (attempt == maxAttempts)
154+
{
155+
throw new InvalidOperationException(
156+
$"'{path}' kept failing to load after {maxAttempts} attempts:{Environment.NewLine}{string.Join(Environment.NewLine, failures)}");
157+
}
158+
159+
Console.WriteLine($"Reloading '{path}' after network failures: {string.Join("; ", attemptFailures)}");
160+
}
161+
}
109162

110163
private async Task LogInAsync(TestUser user)
111164
{

‎dev/EssentialCSharp.Web.AppHost.Tests/StandaloneWebSmokeTests.cs‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ public async Task HealthEndpoint_FreshStart_ReturnsOk(string path)
5252
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
5353
}
5454

55+
[Test]
56+
public async Task PlaceholderChapter_FreshStart_RendersRepositoryContent()
57+
{
58+
using var client = fixture.CreateHttpClient("essentialcsharp-web", "http");
59+
60+
using var response = await client.GetAsync("/hello-world");
61+
string body = await response.Content.ReadAsStringAsync();
62+
63+
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
64+
await Assert.That(body).Contains("Hello, World");
65+
}
66+
}
67+
68+
/// <summary>
69+
/// Starting a second AppHost creates container networks, and on Linux that makes Chromium abort in-flight
70+
/// requests with net::ERR_NETWORK_CHANGED. A parallel group runs after all ungrouped tests, so this
71+
/// can't break the browser journeys.
72+
/// </summary>
73+
[Category("Docker")]
74+
[RequiresLinuxContainers]
75+
[ParallelGroup(nameof(SecondAppHostTests))]
76+
[ClassDataSource<Journeys.JourneyFixture>(Shared = SharedType.PerTestSession)]
77+
public class SecondAppHostTests(Journeys.JourneyFixture fixture)
78+
{
5579
[Test]
5680
public async Task SecondAppHost_DynamicPorts_RunsAlongsideFirst()
5781
{
@@ -66,16 +90,4 @@ public async Task SecondAppHost_DynamicPorts_RunsAlongsideFirst()
6690
await Assert.That(firstResponse.StatusCode).IsEqualTo(HttpStatusCode.OK);
6791
await Assert.That(secondResponse.StatusCode).IsEqualTo(HttpStatusCode.OK);
6892
}
69-
70-
[Test]
71-
public async Task PlaceholderChapter_FreshStart_RendersRepositoryContent()
72-
{
73-
using var client = fixture.CreateHttpClient("essentialcsharp-web", "http");
74-
75-
using var response = await client.GetAsync("/hello-world");
76-
string body = await response.Content.ReadAsStringAsync();
77-
78-
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
79-
await Assert.That(body).Contains("Hello, World");
80-
}
8193
}

0 commit comments

Comments
 (0)