Path: redb-route/tests/redb.Route.Tests/StatisticsOwnershipTests.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/StatisticsOwnershipTests.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Tests;
///
/// The statistics-ownership audit (plan KAFKA_HARDENING_AND_OPTIONS_SWEEP_PLAN, owner question 0).
/// One owner for pipeline statistics: the core. StatisticsProcessor wraps every From(), ToProcessor
/// counts every routed To(), and the ProducerTemplate counts its sends the same way - it used to
/// bypass statistics entirely, so a template send was invisible unless the connector
/// self-recorded, and a self-recording connector then double-counted in routes.
///
public sealed class StatisticsOwnershipTests
{
[Fact]
public async Task TemplateSend_CountsMessagesOut_OnTheEndpoint()
{
await using var context = new RouteContext();
await context.Start();
// seda: buffers without a consumer, and no route touches the endpoint - so the ONLY
// possible writer of its statistics is the template itself.
using var template = new ProducerTemplate(context);
template.Start();
await template.SendAsync("seda:solo", "one");
((IEndpointStatistics)context.GetEndpoint("seda:solo")).MessagesOut.Should().Be(1,
"template-otpravka must be counted like a routed .To()");
}
[Fact]
public async Task TemplateSend_Failure_CountsBothLegs_OfAnInMemoryBridge()
{
await using var context = new RouteContext();
context.AddRoutes(r => r.From("direct:boom").Process(_ => throw new InvalidOperationException("no")));
await context.Start();
using var template = new ProducerTemplate(context);
template.Start();
Func act = () => template.SendAsync("direct:boom", "one");
await act.Should().ThrowAsync();
// direct: is one endpoint object playing both roles, and each leg records ITS error:
// the send leg (CountedSend) and the pipeline leg (StatisticsProcessor). Exactly two -
// a third writer would mean a self-recording regression.
((IEndpointStatistics)context.GetEndpoint("direct:boom")).Errors.Should().Be(2,
"exactly two legs: the send error + the pipeline error, there must be no third writer");
}
[Fact]
public async Task RoutedToAnInMemoryBridge_CountsMessagesOutOnce()
{
// direct:mid is a producer target for the first route AND the From() of the second -
// the same endpoint object. MessagesOut is producer-side only: ToProcessor writes 1,
// and the consumer wrapper must not add its own on pipeline success.
await using var context = new RouteContext();
context.AddRoutes(r =>
{
r.From("direct:in").To("direct:mid");
r.From("direct:mid").To("mock:out");
});
await context.Start();
using var template = new ProducerTemplate(context);
template.Start();
await template.SendAsync("direct:in", "one");
var mid = (IEndpointStatistics)context.GetEndpoint("direct:mid");
mid.MessagesOut.Should().Be(1, "MessagesOut is written by the producer side only");
mid.MessagesIn.Should().Be(1, "MessagesIn is written by the consumer wrapper only");
}
[Fact]
public async Task RecipientListSend_CountsMessagesOut_LikeARoutedTo()
{
// The sending EIPs drive a producer directly (no ToProcessor). Their sends must land in
// the same producer-side counters - they used to bypass statistics entirely.
await using var context = new RouteContext();
context.AddRoutes(r => r.From("direct:rl").RecipientList(_ => new[] { "seda:rl-target" }));
await context.Start();
using var template = new ProducerTemplate(context);
template.Start();
await template.SendAsync("direct:rl", "one");
((IEndpointStatistics)context.GetEndpoint("seda:rl-target")).MessagesOut.Should().Be(1,
"a send through RecipientList counts as the route's .To()");
}
[Fact]
public async Task EnrichSend_CountsMessagesOut_LikeARoutedTo()
{
await using var context = new RouteContext();
context.AddRoutes(r => r.From("direct:enr").Enrich("mock:enr-svc").To("mock:enr-out"));
await context.Start();
using var template = new ProducerTemplate(context);
template.Start();
await template.SendAsync("direct:enr", "one");
((IEndpointStatistics)context.GetEndpoint("mock:enr-svc")).MessagesOut.Should().Be(1,
"an enrichment request is a send through the producer too");
}
}