Path: redb-route/tests/redb.Route.Tests/Core/CancellationHandlerValidationTests.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/CancellationHandlerValidationTests.cs
----
using AwesomeAssertions;
using Microsoft.Extensions.Logging;
using redb.Route.Core;
namespace redb.Route.Tests.Core;
///
/// A Catch<T> / OnException<T> on a type that is an
/// can never fire — a genuine cancellation is never handled and a
/// timeout arrives as — so the route load warns about it instead of leaving
/// the dead branch to be discovered by a red test.
///
public class CancellationHandlerValidationTests
{
private static (RouteContext Context, List Warnings) New()
{
var warnings = new List();
var loggerFactory = LoggerFactory.Create(b => b.AddProvider(new CapturingLoggerProvider(warnings)));
return (new RouteContext("cancellation-handler", loggerFactory), warnings);
}
[Fact]
public async Task TryCatch_Catch_OperationCanceled_Warns()
{
var (ctx, warnings) = New();
ctx.AddRoutes(r => r
.From("direct:c").RouteId("c")
.TryCatch()
.Process(_ => { })
.Catch()
.Process(_ => { })
.EndTryCatch()
.To("direct:sink"));
await ctx.Start(); // the route still builds — the branch is dead, not illegal
warnings.Should().Contain(w => w.Contains("TaskCanceledException") && w.Contains("can never fire"));
await ctx.DisposeAsync();
}
[Fact]
public async Task TryCatch_Catch_TimeoutException_DoesNotWarn()
{
var (ctx, warnings) = New();
ctx.AddRoutes(r => r
.From("direct:t").RouteId("t")
.TryCatch()
.Process(_ => { })
.Catch()
.Process(_ => { })
.EndTryCatch()
.To("direct:sink"));
await ctx.Start();
warnings.Should().NotContain(w => w.Contains("can never fire"));
await ctx.DisposeAsync();
}
[Fact]
public async Task OnException_OperationCanceled_Warns()
{
var (ctx, warnings) = New();
ctx.AddRoutes(r =>
{
r.OnException().Handled();
r.From("direct:o").RouteId("o").Process(_ => { }).To("direct:sink");
});
await ctx.Start();
warnings.Should().Contain(w => w.Contains("OperationCanceledException") && w.Contains("can never fire"));
await ctx.DisposeAsync();
}
private sealed class CapturingLoggerProvider(List sink) : ILoggerProvider
{
public ILogger CreateLogger(string categoryName) => new CapturingLogger(sink);
public void Dispose() { }
private sealed class CapturingLogger(List sink) : ILogger
{
public IDisposable? BeginScope(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func formatter)
{
if (logLevel == LogLevel.Warning) sink.Add(formatter(state, exception));
}
}
}
}