Path: redb-route/tests/redb.Route.Tests/Core/RouteErrorScopeTests.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/RouteErrorScopeTests.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Tests.Core;
///
/// One error scope per route, as in Apache Camel: the route's OnException, its builder's, then the context's
/// error handling (AddLocalExceptionHandler → AddGlobalExceptionHandler → ErrorHandler) for a
/// failure no OnException took. The same scope wraps the detached body of .Threads(), so a failure there
/// gets the full handler configuration. A context handler that does not mark the exchange handled lets the failure go
/// on to the caller, as Camel's DefaultErrorHandler does.
///
public class RouteErrorScopeTests
{
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(5);
// ── The detached .Threads() body gets the full OnException configuration ──
[Fact]
public async Task Threads_OnWhenFalse_DoesNotTakeTheFailure()
{
var hits = 0;
var errors = new RecordingErrorHandler();
await using var ctx = new RouteContext { ErrorHandler = errors };
ctx.AddRoutes(r => r
.From("direct://scope-threads-onwhen")
.OnException()
.OnWhen(_ => false)
.Handled()
.Process(_ => Interlocked.Increment(ref hits))
.End()
.Threads(2)
.Process((IExchange _, CancellationToken _) => throw new InvalidOperationException("boom"))
.End());
await ctx.Start();
await Send(ctx, "direct://scope-threads-onwhen");
await WaitFor(() => errors.Count == 1);
Volatile.Read(ref hits).Should().Be(0, "OnWhen is false, so the handler does not take the failure");
}
[Fact]
public async Task Threads_Redeliveries_RerunTheThreadsBody()
{
var runs = 0;
var handled = 0;
await using var ctx = new RouteContext();
ctx.AddRoutes(r => r
.From("direct://scope-threads-redelivery")
.OnException()
.MaximumRedeliveries(2)
.RedeliveryDelay(TimeSpan.FromMilliseconds(1))
.Handled()
.Process(_ => Interlocked.Increment(ref handled))
.End()
.Threads(2)
.Process((IExchange _, CancellationToken _) =>
{
Interlocked.Increment(ref runs);
throw new InvalidOperationException("boom");
})
.End());
await ctx.Start();
await Send(ctx, "direct://scope-threads-redelivery");
await WaitFor(() => Volatile.Read(ref handled) == 1);
Volatile.Read(ref runs).Should().Be(3, "the first run and two redeliveries of the Threads body");
}
[Fact]
public async Task Threads_OnRedeliveryAndUseOriginalBody_Apply()
{
var redeliveries = 0;
var bodies = new ConcurrentQueue