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
9 changes: 1 addition & 8 deletions tests/Nullean.Cdp.Tests/Live/EchoServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class EchoServer : IAsyncDisposable

public EchoServer()
{
Url = $"http://localhost:{FreePort()}/";
Url = $"http://localhost:{TestPort.Free()}/";
_listener.Prefixes.Add(Url);
_listener.Start();
_serving = Task.Run(ServeAsync);
Expand Down Expand Up @@ -57,13 +57,6 @@ await response.OutputStream.WriteAsync(
}
}

private static int FreePort()
{
using var probe = new TcpListener(IPAddress.Loopback, 0);
probe.Start();
return ((IPEndPoint)probe.LocalEndpoint).Port;
}

public async ValueTask DisposeAsync()
{
_listener.Stop();
Expand Down
9 changes: 1 addition & 8 deletions tests/Nullean.Cdp.Tests/Live/SlowServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class SlowServer : IAsyncDisposable

public SlowServer(TimeSpan imageDelay)
{
var port = FreePort();
var port = TestPort.Free();
// localhost, not 127.0.0.1: HttpListener on Windows needs no URL reservation for it.
Url = $"http://localhost:{port}/";
_listener.Prefixes.Add(Url);
Expand Down Expand Up @@ -143,13 +143,6 @@ await response.OutputStream.WriteAsync(
}
}

private static int FreePort()
{
using var probe = new TcpListener(IPAddress.Loopback, 0);
probe.Start();
return ((IPEndPoint)probe.LocalEndpoint).Port;
}

public async ValueTask DisposeAsync()
{
_listener.Stop();
Expand Down
28 changes: 28 additions & 0 deletions tests/Nullean.Cdp.Tests/Live/TestPort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Net;
using System.Net.Sockets;

namespace Nullean.Cdp.Tests.Live;

/// <summary>
/// Free local ports for the test servers. The probe is closed before a server binds, and a server may start late, so the
/// operating system can hand the same port to two servers that run in parallel: this never hands one out twice.
/// </summary>
internal static class TestPort
{
private static readonly HashSet<int> Taken = [];

public static int Free()
{
while (true)
{
using var probe = new TcpListener(IPAddress.Loopback, 0);
probe.Start();
var port = ((IPEndPoint)probe.LocalEndpoint).Port;
lock (Taken)
{
if (Taken.Add(port))
return port;
}
}
}
}
20 changes: 11 additions & 9 deletions tests/Nullean.Cdp.Tests/Live/VersionServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class VersionServer : IAsyncDisposable
public VersionServer(Func<int, string> build, TimeSpan? startAfter = null)
{
_build = build;
var port = FreePort();
var port = TestPort.Free();
Url = $"http://localhost:{port}/";
_listener.Prefixes.Add(Url);
_serving = Task.Run(async () =>
Expand All @@ -27,7 +27,16 @@ public VersionServer(Func<int, string> build, TimeSpan? startAfter = null)
return;
if (startAfter is { } wait)
await Task.Delay(wait);
_listener.Start();
try
{
_listener.Start();
}
catch (ObjectDisposedException)
{
// Disposed before the delayed start got to run: nothing to serve.
return;
}

await ServeAsync();
});
}
Expand Down Expand Up @@ -82,13 +91,6 @@ await context
catch (Exception e) when (e is HttpListenerException or ObjectDisposedException or IOException) { }
}

private static int FreePort()
{
using var probe = new TcpListener(IPAddress.Loopback, 0);
probe.Start();
return ((IPEndPoint)probe.LocalEndpoint).Port;
}

public async ValueTask DisposeAsync()
{
if (_listener.IsListening)
Expand Down
18 changes: 18 additions & 0 deletions tests/Nullean.Cdp.Tests/Live/VersionServerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using AwesomeAssertions;

namespace Nullean.Cdp.Tests.Live;

public class VersionServerTests
{
[Test]
public async Task DisposingBeforeTheServerHasStartedDoesNotThrow()
{
// The listener starts on a pool thread: a dispose that wins that race used to surface an ObjectDisposedException.
for (var i = 0; i < 500; i++)
{
var server = new VersionServer(_ => "1.0.0");
var dispose = async () => await server.DisposeAsync();
await dispose.Should().NotThrowAsync();
}
}
}
9 changes: 1 addition & 8 deletions tests/Nullean.Cdp.Tests/Live/WriteServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal sealed class WriteServer : IAsyncDisposable

public WriteServer()
{
Url = $"http://localhost:{FreePort()}/";
Url = $"http://localhost:{TestPort.Free()}/";
_listener.Prefixes.Add(Url);
_listener.Start();
_serving = Task.Run(ServeAsync);
Expand Down Expand Up @@ -66,13 +66,6 @@ private async Task ServeAsync()
}
}

private static int FreePort()
{
using var probe = new TcpListener(IPAddress.Loopback, 0);
probe.Start();
return ((IPEndPoint)probe.LocalEndpoint).Port;
}

public async ValueTask DisposeAsync()
{
_listener.Stop();
Expand Down
Loading