Path: redb-route/tests/redb.Route.Tests.SignalR/SignalRSharedListenerTests.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/SignalRSharedListenerTests.cs
----
using System.Collections.Concurrent;
using System.Net;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Http;
using redb.Route.SignalR;
namespace redb.Route.Tests.SignalR;
///
/// A hub on a listener it shares with other route contexts, as on a Tsak worker: every hub has a container of its
/// own and joins or leaves the running listener without touching anything else on it. The S1 to S5 scenarios are:
/// one for all contexts, an HTTP context next to the hub context, the hub context
/// with or without a broadcast endpoint (mode=server), and a restart that disposes the hub context and starts a
/// new one with the same routes.
///
public sealed class SignalRSharedListenerTests : IAsyncLifetime
{
private readonly SharedHttpServerManager _shared = new();
private readonly ErrorLog _log = new();
private readonly ILoggerFactory _loggers;
private readonly HttpClient _http = new();
private readonly List _contexts = [];
private readonly List _connections = [];
private readonly List _consumers = [];
private readonly List _producers = [];
public SignalRSharedListenerTests()
=> _loggers = LoggerFactory.Create(b => b.SetMinimumLevel(LogLevel.Warning).AddProvider(_log));
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
foreach (var c in _connections) await c.DisposeAsync();
foreach (var p in _producers) await p.Stop();
foreach (var c in _consumers) await c.Stop();
foreach (var c in _contexts) await c.DisposeAsync();
await _shared.DisposeAsync();
_http.Dispose();
_loggers.Dispose();
}
private static int GetFreePort() => global::redb.Route.Tests.Shared.TestPorts.Next();
// ── Contexts as the report builds them ──
/// The "omni" context: one HTTP route on the shared listener.
private async Task StartHttpContextAsync(int port)
{
var context = new RouteContext("probe.omni", _loggers);
context.AddComponent(new HttpComponent { ServerManager = _shared });
context.AddRoutes(r => r.From($"http://127.0.0.1:{port}/ping?inOut=true").Process(e => e.In.Body = "pong"));
await context.Start();
_contexts.Add(context);
return context;
}
/// The "api" context: the hub, answering with its tag, and optionally the broadcast into it.
private async Task StartHubContextAsync(int port, bool broadcast, string tag = "api",
string hubPath = "/hub/probe")
{
var context = new RouteContext($"probe.{tag}", _loggers);
context.AddComponent(new SignalRComponent { ServerManager = _shared });
var hub = $"signalr://127.0.0.1:{port}{hubPath}";
context.AddRoutes(r => r.From(hub + "?inOut=true").Process(e => e.Out = new Message($"{tag}:{e.In.Body}")));
if (broadcast)
context.AddRoutes(r => r.From($"direct://{tag}-broadcast").To(hub + "?mode=server&method=uiChanged"));
await context.Start();
_contexts.Add(context);
return context;
}
private async Task RestartAsync(RouteContext context)
{
_contexts.Remove(context);
await context.DisposeAsync();
}
private static async Task BroadcastAsync(RouteContext context, string tag, string body)
{
using var template = new ProducerTemplate(context);
template.Start();
await template.SendAsync($"direct://{tag}-broadcast", body);
}
private async Task NegotiateAsync(int port, string hubPath = "/hub/probe")
{
using var response = await _http.PostAsync(
$"http://127.0.0.1:{port}{hubPath}/negotiate?negotiateVersion=1", null);
return response.StatusCode;
}
private async Task ConnectAsync(int port, string hubPath = "/hub/probe",
HttpTransportType? transport = null, string? accessToken = null, Action? configure = null)
{
var builder = new HubConnectionBuilder().WithUrl($"http://127.0.0.1:{port}{hubPath}", o =>
{
if (transport is { } only) o.Transports = only;
if (accessToken is not null) o.AccessTokenProvider = () => Task.FromResult(accessToken);
});
configure?.Invoke(builder);
var connection = builder.Build();
_connections.Add(connection);
await connection.StartAsync();
return connection;
}
private static async Task InvokeAsync(HubConnection connection, string body = "x")
=> (await connection.InvokeAsync