Path: redb-route/tests/redb.Route.Tests/Core/DeadLetterErrorHandlerTests.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/Core/DeadLetterErrorHandlerTests.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.ErrorHandling;
using redb.Route.TestKit;
namespace redb.Route.Tests.Core;
///
/// The context's dead letter channel — Apache Camel's context-level errorHandler(deadLetterChannel(...)): every
/// route sends to it a failure no OnException took, stamped with the CamelDeadLetter* headers, and the
/// failure is handled; a failure an OnException took never reaches it; a lost dead letter lets the failure go on.
///
public class DeadLetterErrorHandlerTests
{
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(2);
[Fact]
public async Task AFailureNoOnExceptionTook_GoesToTheDeadLetterRoute_AndIsHandled()
{
await using var ctx = new RouteContext();
ctx.ErrorHandler = new DeadLetterErrorHandler(ctx, "mock://dlc-errors");
ctx.AddRoutes(r => r.From("direct://dlc-orders").Process(_ => throw new InvalidOperationException("boom")));
await ctx.Start();
var exchange = await Send(ctx, "direct://dlc-orders");
exchange.ExceptionHandled.Should().BeTrue("a dead letter channel takes the failure, as in Camel");
var dead = ctx.Mock("mock://dlc-errors");
await dead.ExpectBodies("x").AssertIsSatisfiedAsync(Wait);
var headers = dead.ReceivedExchanges.Single().In.Headers;
headers["CamelDeadLetterReason"].Should().Be("boom");
headers["CamelDeadLetterExceptionType"].Should().Be(typeof(InvalidOperationException).FullName);
}
[Fact]
public async Task AFailureAnOnExceptionTook_NeverReachesIt()
{
await using var ctx = new RouteContext();
ctx.ErrorHandler = new DeadLetterErrorHandler(ctx, "mock://dlc-taken-errors");
ctx.AddRoutes(r => r
.From("direct://dlc-taken")
.OnException().Handled().End()
.Process(_ => throw new InvalidOperationException("boom")));
await ctx.Start();
await Send(ctx, "direct://dlc-taken");
ctx.Mock("mock://dlc-taken-errors").ReceivedExchanges.Should().BeEmpty();
}
[Fact]
public async Task ALostDeadLetter_LetsTheFailureGoOn()
{
await using var ctx = new RouteContext();
ctx.ErrorHandler = new DeadLetterErrorHandler(ctx, "direct://dlc-nobody-listens");
ctx.AddRoutes(r => r.From("direct://dlc-lost").Process(_ => throw new InvalidOperationException("boom")));
await ctx.Start();
var exchange = await Send(ctx, "direct://dlc-lost");
exchange.ExceptionHandled.Should().BeFalse("the dead letter was not delivered, so the failure is not swallowed");
exchange.Exception.Should().BeOfType();
}
private static async Task Send(IRouteContext ctx, string uri)
{
var producer = ctx.GetEndpoint(uri).CreateProducer();
await producer.Start();
var exchange = new Exchange(new Message { Body = "x" });
try { await producer.Process(exchange); }
catch (Exception ex) { exchange.Exception ??= ex; }
return exchange;
}
}