Path: redb-route/tests/redb.Route.Tests.SignalR/SignalRLifecycleTests.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.SignalR/SignalRLifecycleTests.cs
----
using System.Net;
using System.Net.Sockets;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR.Client;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Http;
using redb.Route.SignalR;
namespace redb.Route.Tests.SignalR;
///
/// Findings of the review of the shared-host work: a hub is an occupant of the shared
/// listener like any HTTP route, and it has to survive its neighbours coming and going, survive a
/// restart of its own, and be told apart from a hub with the same path on another port.
///
public sealed class SignalRLifecycleTests : IAsyncLifetime
{
private readonly SharedHttpServerManager _manager = new();
private readonly HttpClient _http = new();
private readonly List _consumers = [];
private readonly List _connections = [];
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
foreach (var c in _connections) await c.DisposeAsync();
foreach (var c in _consumers) await c.Stop();
_http.Dispose();
await _manager.DisposeAsync();
}
private static int GetFreePort() => global::redb.Route.Tests.Shared.TestPorts.Next();
private static SignalREndpoint Endpoint(SignalRComponent component, string path)
=> (SignalREndpoint)component.CreateEndpoint(EndpointUriParser.Parse($"signalr://{path}?inOut=true"));
private async Task ConnectAsync(int port, string hubPath)
{
var connection = new HubConnectionBuilder().WithUrl($"http://127.0.0.1:{port}{hubPath}").Build();
_connections.Add(connection);
await connection.StartAsync();
return connection;
}
[Fact]
public async Task StoppingTheLastHttpRoute_LeavesALiveHubAlone()
{
var port = GetFreePort();
var registration = _manager.RegisterRoute("127.0.0.1", port, "/api/health", "GET",
ctx => ctx.Response.WriteAsync("healthy"));
var component = new SignalRComponent { ServerManager = _manager };
var endpoint = Endpoint(component, $"127.0.0.1:{port}/hub");
await endpoint.Start();
var consumer = endpoint.CreateConsumer(new EchoProcessor());
_consumers.Add(consumer);
await consumer.Start();
// The HTTP consumer on this port shuts down; the hub is still serving.
_manager.UnregisterRoute(registration);
await _manager.StopIfEmpty("127.0.0.1", port);
var connection = await ConnectAsync(port, "/hub");
var reply = await connection.InvokeAsync