Path: redb-route/tests/redb.Route.Tests/Processors/TryCatchProcessorTests.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/TryCatchProcessorTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Tests.Processors;
/// Tests for and .
public class TryCatchProcessorTests
{
/// Body executes normally when no exception.
[Fact]
public async Task Process_NoException_BodyExecutes()
{
var executed = false;
var tryCatch = new TryCatchProcessor(new DelegateProcessor(_ => executed = true));
await tryCatch.Process(new Exchange());
executed.Should().BeTrue();
}
/// Matching catch clause handles exception.
[Fact]
public async Task Process_MatchingCatch_HandlesException()
{
var handled = false;
var tryCatch = new TryCatchProcessor(
new DelegateProcessor(_ => throw new InvalidOperationException("fail")))
.Catch(new DelegateProcessor(ex =>
{
handled = true;
ex.Exception.Should().BeOfType();
}));
var exchange = new Exchange();
await tryCatch.Process(exchange);
handled.Should().BeTrue();
exchange.ExceptionHandled.Should().BeTrue();
exchange.Exception.Should().BeOfType();
}
/// Non-matching clause rethrows.
[Fact]
public async Task Process_NoMatchingCatch_Rethrows()
{
var tryCatch = new TryCatchProcessor(
new DelegateProcessor(_ => throw new InvalidOperationException("fail")))
.Catch(new DelegateProcessor(_ => { }));
var act = () => tryCatch.Process(new Exchange());
await act.Should().ThrowAsync();
}
/// Finally block always executes — on success.
[Fact]
public async Task Process_Finally_ExecutesOnSuccess()
{
var finallyRan = false;
var tryCatch = new TryCatchProcessor(new DelegateProcessor(_ => { }))
.SetFinally(new DelegateProcessor(_ => finallyRan = true));
await tryCatch.Process(new Exchange());
finallyRan.Should().BeTrue();
}
/// Finally block executes on handled exception.
[Fact]
public async Task Process_Finally_ExecutesOnHandledException()
{
var finallyRan = false;
var tryCatch = new TryCatchProcessor(
new DelegateProcessor(_ => throw new InvalidOperationException()))
.Catch(new DelegateProcessor(_ => { }))
.SetFinally(new DelegateProcessor(_ => finallyRan = true));
await tryCatch.Process(new Exchange());
finallyRan.Should().BeTrue();
}
/// Finally block executes on unhandled exception.
[Fact]
public async Task Process_Finally_ExecutesOnUnhandledException()
{
var finallyRan = false;
var tryCatch = new TryCatchProcessor(
new DelegateProcessor(_ => throw new InvalidOperationException()))
.SetFinally(new DelegateProcessor(_ => finallyRan = true));
var act = () => tryCatch.Process(new Exchange());
await act.Should().ThrowAsync();
finallyRan.Should().BeTrue();
}
/// CatchClause.For creates typed clause.
[Fact]
public void CatchClause_For_CreatesTyped()
{
var clause = CatchClause.For(new DelegateProcessor(_ => { }));
clause.ExceptionType.Should().Be(typeof(ArgumentException));
clause.Matches(new ArgumentException()).Should().BeTrue();
clause.Matches(new InvalidOperationException()).Should().BeFalse();
}
/// CatchClause with When predicate filters further.
[Fact]
public void CatchClause_WithWhen_Filters()
{
var clause = CatchClause.For(
new DelegateProcessor(_ => { }),
ex => ex.ParamName == "test");
clause.Matches(new ArgumentException("msg", "test")).Should().BeTrue();
clause.Matches(new ArgumentException("msg", "other")).Should().BeFalse();
}
/// A genuine cancellation is never caught — the caller's token is what makes it one.
[Fact]
public async Task Process_CallerCancellation_NeverCaught()
{
using var cts = new CancellationTokenSource();
cts.Cancel();
var tryCatch = new TryCatchProcessor(
new DelegateProcessor(_ => throw new OperationCanceledException()))
.Catch(new DelegateProcessor(_ => { }));
var act = () => tryCatch.Process(new Exchange(), cts.Token);
await act.Should().ThrowAsync();
}
/// A downstream timeout (an OperationCanceledException with our token still live) is named a
/// TimeoutException and reaches the catch clause.
[Fact]
public async Task Process_Timeout_CaughtAsTimeoutException()
{
Exception? caught = null;
var tryCatch = new TryCatchProcessor(
new DelegateProcessor(_ => throw new TaskCanceledException("downstream deadline")))
.Catch(new DelegateProcessor(ex => caught = ex.Exception));
var exchange = new Exchange();
await tryCatch.Process(exchange); // ct is not cancelled → not a cancellation
caught.Should().BeOfType();
caught!.InnerException.Should().BeOfType(
"the original timeout symptom is kept as the cause");
exchange.ExceptionHandled.Should().BeTrue();
}
/// Invalid exception type in CatchClause throws.
[Fact]
public void CatchClause_InvalidType_Throws()
{
var act = () => new CatchClause(typeof(string), new DelegateProcessor(_ => { }));
act.Should().Throw();
}
}