Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System.Text.Json;
using Microsoft.Playwright;
using TUnit.Playwright;
Expand Down Expand Up @@ -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<IResponse?> GoToAsync(string path) => Page.GotoAsync(new Uri(fixture.WebUrl, path).ToString());

/// <summary>
/// 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.
/// </summary>
private async Task<IResponse?> 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<string>();

for (int attempt = 1; ; attempt++)
{
var attemptFailures = new ConcurrentQueue<string>();
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)
{
Expand Down
36 changes: 24 additions & 12 deletions dev/EssentialCSharp.Web.AppHost.Tests/StandaloneWebSmokeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

/// <summary>
/// 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.
/// </summary>
[Category("Docker")]
[RequiresLinuxContainers]
[ParallelGroup(nameof(SecondAppHostTests))]
[ClassDataSource<Journeys.JourneyFixture>(Shared = SharedType.PerTestSession)]
public class SecondAppHostTests(Journeys.JourneyFixture fixture)
{
[Test]
public async Task SecondAppHost_DynamicPorts_RunsAlongsideFirst()
{
Expand All @@ -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");
}
}
Loading