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
17 changes: 9 additions & 8 deletions tests/Nullean.Cdp.Tests/Live/PreconditionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,26 @@ public async Task AMissingHeaderAndAMissingElementAreSeenAsNothing()
public async Task LooksThatFailAreRetriedAndGiveUpAfterTheRetries()
{
_ = chrome.Browser;
await using var flaky = new VersionServer(_ => "1.2.0", hangFirst: 2);
await using var down = new VersionServer(_ => "1.2.0", hangFirst: 100);
// Until it starts, the port refuses connections: a look fails at once, whatever the machine's speed. How many looks failed before
// it came up depends on how fast the machine starts a context, so only the outcome is asserted for that server.
await using var comingUp = new VersionServer(_ => "1.2.0", startAfter: TimeSpan.FromMilliseconds(800));
await using var down = new VersionServer(_ => "1.2.0", startAfter: Timeout.InfiniteTimeSpan);
var precondition = new JourneyPrecondition
{
Path = "/",
Header = "x-build",
Expected = "1.2.0",
Timeout = TimeSpan.FromSeconds(30),
Interval = TimeSpan.FromMilliseconds(100),
Retries = 2
Interval = TimeSpan.FromMilliseconds(300),
Retries = 5,
};

var recovered = await JourneyPreconditions.WaitAsync(chrome.Browser, precondition, Options(flaky, TimeSpan.FromMilliseconds(400)));
var gaveUp = await JourneyPreconditions.WaitAsync(chrome.Browser, precondition, Options(down, TimeSpan.FromMilliseconds(400)));
var recovered = await JourneyPreconditions.WaitAsync(chrome.Browser, precondition, Options(comingUp));
var gaveUp = await JourneyPreconditions.WaitAsync(chrome.Browser, precondition with { Retries = 2 }, Options(down));

recovered.Passed.Should().BeTrue(recovered.Message);
recovered.Attempts.Should().Be(3);
gaveUp.Passed.Should().BeFalse();
gaveUp.Message.Should().Contain("3 looks failed, the last with:");
gaveUp.Message.Should().Contain("3 looks failed, the last with:").And.Contain("ERR_CONNECTION_REFUSED");
gaveUp.Attempts.Should().Be(3);
}

Expand Down
22 changes: 13 additions & 9 deletions tests/Nullean.Cdp.Tests/Live/VersionServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@ namespace Nullean.Cdp.Tests.Live;

/// <summary>
/// A local HTTP server whose page names a build in a header and in a meta tag, and whose build changes as requests come in: a
/// deploy that lands a moment after the run starts. The first requests can hang, as a struggling origin does.
/// deploy that lands a moment after the run starts. The server can start late, or never: until it does, the port refuses connections, as an origin that is still coming up does.
/// </summary>
internal sealed class VersionServer : IAsyncDisposable
{
private readonly HttpListener _listener = new();
private readonly Task _serving;
private readonly Func<int, string> _build;
private readonly int _hangFirst;
private int _requests;

public VersionServer(Func<int, string> build, int hangFirst = 0)
public VersionServer(Func<int, string> build, TimeSpan? startAfter = null)
{
_build = build;
_hangFirst = hangFirst;
var port = FreePort();
Url = $"http://localhost:{port}/";
_listener.Prefixes.Add(Url);
_listener.Start();
_serving = Task.Run(ServeAsync);
_serving = Task.Run(async () =>
{
if (startAfter == Timeout.InfiniteTimeSpan)
return;
if (startAfter is { } wait)
await Task.Delay(wait);
_listener.Start();
await ServeAsync();
});
}

public string Url { get; }
Expand Down Expand Up @@ -61,8 +66,6 @@ private async Task RespondAsync(HttpListenerContext context, int number)
{
try
{
if (number <= _hangFirst)
await Task.Delay(TimeSpan.FromSeconds(3));
var build = _build(number);
context.Response.ContentType = "text/html; charset=utf-8";
context.Response.AddHeader("x-build", build);
Expand All @@ -88,7 +91,8 @@ private static int FreePort()

public async ValueTask DisposeAsync()
{
_listener.Stop();
if (_listener.IsListening)
_listener.Stop();
_listener.Close();
await _serving;
}
Expand Down
Loading