Path: redb-route/tests/redb.Route.Tests/Processors/PipelineProcessorTests.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/PipelineProcessorTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Tests.Processors;
/// Tests for .
public class PipelineProcessorTests
{
/// Processors execute in order.
[Fact]
public async Task Process_ExecutesInOrder()
{
var order = new List();
var pipeline = new PipelineProcessor();
pipeline.Add(new DelegateProcessor(_ => order.Add(1)));
pipeline.Add(new DelegateProcessor(_ => order.Add(2)));
pipeline.Add(new DelegateProcessor(_ => order.Add(3)));
await pipeline.Process(new Exchange());
order.Should().Equal(1, 2, 3);
}
/// Empty pipeline completes without error.
[Fact]
public async Task Process_EmptyPipeline_Succeeds()
{
var pipeline = new PipelineProcessor();
var exchange = new Exchange();
await pipeline.Process(exchange);
exchange.IsStopped.Should().BeFalse();
}
/// Stops when exchange.Stop() is called.
[Fact]
public async Task Process_StoppedExchange_BreaksEarly()
{
var order = new List();
var pipeline = new PipelineProcessor();
pipeline.Add(new DelegateProcessor(ex => { order.Add(1); ex.Stop(); }));
pipeline.Add(new DelegateProcessor(_ => order.Add(2)));
await pipeline.Process(new Exchange());
order.Should().Equal(1);
}
/// Respects CancellationToken — throws on cancellation.
[Fact]
public async Task Process_CancellationRequested_Throws()
{
var pipeline = new PipelineProcessor();
pipeline.Add(new DelegateProcessor(_ => { }));
using var cts = new CancellationTokenSource();
cts.Cancel();
var act = () => pipeline.Process(new Exchange(), cts.Token);
await act.Should().ThrowAsync();
}
/// AddRange adds multiple processors at once.
[Fact]
public async Task AddRange_AddsMultiple()
{
var order = new List();
var pipeline = new PipelineProcessor();
pipeline.AddRange([
new DelegateProcessor(_ => order.Add(1)),
new DelegateProcessor(_ => order.Add(2))
]);
await pipeline.Process(new Exchange());
order.Should().Equal(1, 2);
pipeline.Processors.Should().HaveCount(2);
}
/// Exception in a processor propagates.
[Fact]
public async Task Process_ExceptionPropagates()
{
var pipeline = new PipelineProcessor();
pipeline.Add(new DelegateProcessor(_ => throw new InvalidOperationException("boom")));
pipeline.Add(new DelegateProcessor(_ => { }));
var act = () => pipeline.Process(new Exchange());
await act.Should().ThrowAsync().WithMessage("boom");
}
/// Out→In merge copies ContentType.
[Fact]
public async Task Process_OutToInMerge_CopiesContentType()
{
var pipeline = new PipelineProcessor();
pipeline.Add(new DelegateProcessor(ex =>
{
var outMsg = new Message("response") { ContentType = "application/xml" };
outMsg.Headers["extra"] = "val";
ex.Out = outMsg;
}));
// Second processor sees merged In
pipeline.Add(new DelegateProcessor(ex =>
{
ex.In.Body.Should().Be("response");
ex.In.ContentType.Should().Be("application/xml");
ex.In.Headers["extra"].Should().Be("val");
ex.HasOut.Should().BeFalse();
}));
var exchange = new Exchange(new Message("original") { ContentType = "text/plain" });
await pipeline.Process(exchange);
exchange.In.ContentType.Should().Be("application/xml");
}
/// Out→In merge keeps In's ContentType when the Out message carries none.
[Fact]
public async Task Process_OutToInMerge_KeepsContentTypeWhenOutHasNone()
{
var pipeline = new PipelineProcessor();
pipeline.Add(new DelegateProcessor(ex => ex.Out = new Message("response")));
pipeline.Add(new DelegateProcessor(ex =>
{
ex.In.Body.Should().Be("response");
ex.In.ContentType.Should().Be("text/plain");
}));
var exchange = new Exchange(new Message("original") { ContentType = "text/plain" });
await pipeline.Process(exchange);
exchange.In.ContentType.Should().Be("text/plain");
}
}