Path: redb-route/tests/redb.Route.Tests.SignalR/SignalRStatisticsOwnershipTests.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/SignalRStatisticsOwnershipTests.cs
----
using System.Net;
using System.Net.Sockets;
using Microsoft.AspNetCore.SignalR.Client;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.SignalR;
namespace redb.Route.Tests.SignalR;
///
/// The statistics-ownership audit: in a ROUTED consumer the core's StatisticsProcessor counts
/// MessagesIn/Errors - the connector self-recording the same numbers double-counted them.
///
public sealed class SignalRStatisticsOwnershipTests : IAsyncLifetime
{
private RouteContext? _context;
private HubConnection? _connection;
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
if (_connection is not null) await _connection.DisposeAsync();
if (_context is not null) await _context.DisposeAsync();
}
private static int GetFreePort() => global::redb.Route.Tests.Shared.TestPorts.Next();
[Fact]
public async Task RoutedConsumer_CountsEachInvocationOnce()
{
var port = GetFreePort();
var uri = $"signalr://127.0.0.1:{port}/owned?inOut=true";
var invocations = 0;
_context = new RouteContext();
_context.AddComponent(new SignalRComponent());
_context.AddRoutes(r => r.From(uri).Process(e =>
{
// The Connected lifecycle event is an exchange too - count invocations only.
if (e.In.Body is not null) Interlocked.Increment(ref invocations);
e.Out = new Message("ok");
}));
await _context.Start();
_connection = new HubConnectionBuilder().WithUrl($"http://127.0.0.1:{port}/owned").Build();
await _connection.StartAsync();
await _connection.InvokeAsync