Path: redb-route/tests/redb.Route.Tests/Processors/RoutingSlipProcessorTests.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/Processors/RoutingSlipProcessorTests.cs ---- using AwesomeAssertions; using NSubstitute; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Processors; namespace redb.Route.Tests.Processors; /// Tests for . public class RoutingSlipProcessorTests { private static (IRouteContext context, List visitOrder) SetupContext(params string[] uris) { var visitOrder = new List(); var context = Substitute.For(); foreach (var uri in uris) { var capturedUri = uri; var producer = Substitute.For(); producer.When(p => p.Process(Arg.Any(), Arg.Any())) .Do(_ => visitOrder.Add(capturedUri)); var endpoint = Substitute.For(); endpoint.CreateProducer().Returns(producer); context.GetEndpoint(uri).Returns(endpoint); } return (context, visitOrder); } [Fact] public async Task Process_VisitsEachEndpoint_InSlipOrder() { var (context, order) = SetupContext("direct://a", "direct://b", "direct://c"); var slip = new RoutingSlipProcessor(context, _ => new[] { "direct://a", "direct://b", "direct://c" }); await slip.Process(new Exchange(new Message("data"))); order.Should().Equal("direct://a", "direct://b", "direct://c"); } [Fact] public async Task Process_SlipComputedOnce_NotPerHop() { // The defining property of Routing Slip vs Dynamic Router: the slip factory runs ONCE. var (context, _) = SetupContext("direct://a", "direct://b", "direct://c"); var factoryCalls = 0; var slip = new RoutingSlipProcessor(context, _ => { factoryCalls++; return new[] { "direct://a", "direct://b", "direct://c" }; }); await slip.Process(new Exchange(new Message("data"))); factoryCalls.Should().Be(1); } [Fact] public async Task Process_PipelinesOutToIn_BetweenHops() { var seenByB = new List(); var context = Substitute.For(); // "a" produces an Out; the next hop must see it as In. var producerA = Substitute.For(); producerA.When(p => p.Process(Arg.Any(), Arg.Any())) .Do(ci => ci.Arg().Out = new Message("from-a")); var endpointA = Substitute.For(); endpointA.CreateProducer().Returns(producerA); context.GetEndpoint("direct://a").Returns(endpointA); var producerB = Substitute.For(); producerB.When(p => p.Process(Arg.Any(), Arg.Any())) .Do(ci => seenByB.Add(ci.Arg().In.Body)); var endpointB = Substitute.For(); endpointB.CreateProducer().Returns(producerB); context.GetEndpoint("direct://b").Returns(endpointB); var slip = new RoutingSlipProcessor(context, _ => new[] { "direct://a", "direct://b" }); await slip.Process(new Exchange(new Message("original"))); seenByB.Should().ContainSingle().Which.Should().Be("from-a"); } [Fact] public async Task Process_FinalHopOut_LeftIntactForInOutCaller() { var context = Substitute.For(); var producer = Substitute.For(); producer.When(p => p.Process(Arg.Any(), Arg.Any())) .Do(ci => ci.Arg().Out = new Message("reply")); var endpoint = Substitute.For(); endpoint.CreateProducer().Returns(producer); context.GetEndpoint("direct://only").Returns(endpoint); var slip = new RoutingSlipProcessor(context, _ => new[] { "direct://only" }); var exchange = new Exchange(new Message("q")); await slip.Process(exchange); exchange.HasOut.Should().BeTrue(); exchange.Out!.Body.Should().Be("reply"); } [Fact] public async Task Process_SetsSlipEndpointProperty() { var (context, _) = SetupContext("direct://a", "direct://b"); var slip = new RoutingSlipProcessor(context, _ => new[] { "direct://a", "direct://b" }); var exchange = new Exchange(new Message("data")); await slip.Process(exchange); // Property carries the last endpoint processed (Camel: CamelSlipEndpoint). exchange.Properties[RoutingSlipProcessor.SlipEndpointProperty].Should().Be("direct://b"); } [Fact] public async Task Process_IgnoreInvalidEndpoints_SkipsAndContinues() { var (context, order) = SetupContext("direct://a", "direct://c"); context.GetEndpoint("direct://bad").Returns(_ => throw new InvalidOperationException("no such endpoint")); var slip = new RoutingSlipProcessor(context, _ => new[] { "direct://a", "direct://bad", "direct://c" }, ignoreInvalidEndpoints: true); await slip.Process(new Exchange(new Message("data"))); order.Should().Equal("direct://a", "direct://c"); } [Fact] public async Task Process_InvalidEndpoint_ThrowsWhenNotIgnored() { var context = Substitute.For(); context.GetEndpoint("direct://bad").Returns(_ => throw new InvalidOperationException("no such endpoint")); var slip = new RoutingSlipProcessor(context, _ => new[] { "direct://bad" }); var act = () => slip.Process(new Exchange()); await act.Should().ThrowAsync(); } [Fact] public async Task Process_StoppedExchange_BreaksSlip() { var (context, order) = SetupContext("direct://b"); var producerA = Substitute.For(); producerA.When(p => p.Process(Arg.Any(), Arg.Any())) .Do(ci => ci.Arg().Stop()); var endpointA = Substitute.For(); endpointA.CreateProducer().Returns(producerA); context.GetEndpoint("direct://a").Returns(endpointA); var slip = new RoutingSlipProcessor(context, _ => new[] { "direct://a", "direct://b" }); await slip.Process(new Exchange()); order.Should().BeEmpty(); // "b" not reached after "a" stopped the exchange } [Fact] public async Task Process_EmptySlip_DoesNothing() { var context = Substitute.For(); var slip = new RoutingSlipProcessor(context, _ => Array.Empty()); await slip.Process(new Exchange()); context.DidNotReceive().GetEndpoint(Arg.Any()); } [Fact] public async Task Process_CachesProducerPerUri() { var producer = Substitute.For(); var endpoint = Substitute.For(); endpoint.CreateProducer().Returns(producer); var context = Substitute.For(); context.GetEndpoint("direct://x").Returns(endpoint); // Same URI twice in one slip. var slip = new RoutingSlipProcessor(context, _ => new[] { "direct://x", "direct://x" }); await slip.Process(new Exchange()); endpoint.Received(1).CreateProducer(); } [Fact] public void Constructor_NullContext_Throws() { var act = () => new RoutingSlipProcessor(null!, _ => Array.Empty()); act.Should().Throw().WithParameterName("context"); } [Fact] public void Constructor_NullSlipFactory_Throws() { var act = () => new RoutingSlipProcessor(Substitute.For(), null!); act.Should().Throw().WithParameterName("slipFactory"); } }