Path: redb-route/tests/redb.Route.Tests.WebSocket/WsStatisticsOwnershipTests.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.WebSocket/WsStatisticsOwnershipTests.cs ---- using System.Net; using System.Net.Sockets; using System.Net.WebSockets; using System.Text; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.WebSocket; namespace redb.Route.Tests.WebSocket; /// /// The statistics-ownership audit (plan KAFKA_HARDENING_AND_OPTIONS_SWEEP_PLAN, question 0). /// In a ROUTED consumer the core's StatisticsProcessor already counts MessagesIn/BytesIn/Errors, /// and a routed producer is counted by ToProcessor - the connector self-recording the same /// numbers double-counted every one of them. One owner: the core counts the pipeline, the /// connector counts only what the core cannot see. /// public sealed class WsStatisticsOwnershipTests : IAsyncLifetime { private RouteContext? _context; public Task InitializeAsync() => Task.CompletedTask; public async Task 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_CountsEachFrameOnce() { var port = GetFreePort(); var uri = $"ws://127.0.0.1:{port}/count"; var processed = 0; _context = new RouteContext(); _context.AddComponent(new WsComponent()); _context.AddRoutes(r => r.From(uri).Process(_ => Interlocked.Increment(ref processed))); await _context.Start(); using var client = new ClientWebSocket(); await client.ConnectAsync(new Uri($"ws://127.0.0.1:{port}/count"), CancellationToken.None); await client.SendAsync(Encoding.UTF8.GetBytes("hello"), WebSocketMessageType.Text, true, CancellationToken.None); var deadline = DateTime.UtcNow.AddSeconds(10); while (Volatile.Read(ref processed) < 1 && DateTime.UtcNow < deadline) await Task.Delay(100); processed.Should().Be(1); var stats = (IEndpointStatistics)_context.GetEndpoint(uri); await TestWait.Until(() => stats.MessagesIn >= 1 && stats.BytesIn >= 5); // A double count would land a moment later: give it the time before the exact check. await Task.Delay(300); stats.MessagesIn.Should().Be(1, "one frame - the core already counts MessagesIn, the connector's own write doubled it"); stats.BytesIn.Should().Be(5, "and the bytes were doubled too"); } [Fact] public async Task RoutedProducer_CountsEachSendOnce() { var serverPort = GetFreePort(); var serverUri = $"ws://127.0.0.1:{serverPort}/sink"; var producerUri = $"ws://127.0.0.1:{serverPort}/sink?messageType=Text"; _context = new RouteContext(); _context.AddComponent(new WsComponent()); _context.AddRoutes(r => r.From(serverUri).Process(_ => { })); _context.AddRoutes(r => r.From("direct:out").To(producerUri)); await _context.Start(); using var template = new ProducerTemplate(_context); template.Start(); await template.SendAsync("direct:out", "payload"); var stats = (IEndpointStatistics)_context.GetEndpoint(producerUri); await TestWait.Until(() => stats.MessagesOut >= 1 && stats.BytesOut >= 7); // A double count would land a moment later: give it the time before the exact check. await Task.Delay(300); stats.MessagesOut.Should().Be(1, "one send - the route's .To() already counts through ToProcessor, the own write doubled it"); stats.BytesOut.Should().Be(7, "wire bytes stay with the connector and are written once"); } }