Path: redb-route/tests/redb.Route.Tests.Http/HttpConsumerStartRetryTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Http/HttpConsumerStartRetryTests.cs
----
using System.Net;
using System.Net.Sockets;
using redb.Route.Abstractions;
using redb.Route.Configuration;
using redb.Route.Core;
using redb.Route.Http;
namespace redb.Route.Tests.Http;
///
/// An HTTP consumer whose port is still taken when the context starts (an old process not gone yet) is started again
/// by the context's consumer start retry: the route is Errored while the port is taken, Started once it is free, and
/// then answers. The failed starts leave nothing behind on the shared listener: after the context stops the port is
/// free and one request reaches the route once.
///
[Collection("HttpServer")]
public sealed class HttpConsumerStartRetryTests : IAsyncLifetime
{
private readonly SharedHttpServerManager _servers = new();
private readonly HttpClient _client = new() { Timeout = TimeSpan.FromSeconds(10) };
private readonly int _port = global::redb.Route.Tests.Shared.TestPorts.Next();
private readonly RouteContext _ctx = new(options: new RouteEngineOptions
{
ShutdownTimeout = TimeSpan.FromSeconds(2),
ConsumerStartRetry = new ConsumerStartRetryOptions
{
Delay = TimeSpan.FromMilliseconds(100),
Multiplier = 1.0,
MaxDelay = TimeSpan.FromMilliseconds(100),
},
});
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
_client.Dispose();
await _ctx.DisposeAsync();
await _servers.DisposeAsync();
}
[Fact]
public async Task A_listener_whose_port_was_taken_starts_once_the_port_is_free()
{
var calls = 0;
_ctx.AddComponent(new HttpComponent { ServerManager = _servers });
_ctx.AddRoutes(r => r.From($"http://127.0.0.1:{_port}/ping?inOut=true").RouteId("late-port").Process(e =>
{
Interlocked.Increment(ref calls);
e.In.Body = "pong";
}));
var squatter = new TcpListener(IPAddress.Loopback, _port);
squatter.Start();
try
{
await _ctx.Start();
_ctx.GetRoute("late-port")!.Status.Should().Be(RouteStatus.Errored, "the port is taken");
// A few failed retries while the port stays taken.
await Task.Delay(400);
_ctx.GetRoute("late-port")!.Status.Should().Be(RouteStatus.Errored);
}
finally
{
squatter.Stop();
}
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(10);
while (_ctx.GetRoute("late-port")!.Status != RouteStatus.Started)
{
DateTime.UtcNow.Should().BeBefore(deadline, "the retry starts the listener once the port is free");
await Task.Delay(50);
}
(await _client.GetStringAsync($"http://127.0.0.1:{_port}/ping")).Should().Be("pong");
Volatile.Read(ref calls).Should().Be(1, "one registration of the route, not one per failed start");
await _ctx.Stop();
var stopped = () => _client.GetAsync($"http://127.0.0.1:{_port}/ping");
await stopped.Should().ThrowAsync("the stopped context gives the port back");
}
}