Path: redb-route/tests/redb.Route.Tests/Transactions/TransactedProcessorTerminalStateTests.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/Transactions/TransactedProcessorTerminalStateTests.cs
----
using System.Collections.Concurrent;
using AwesomeAssertions;
using NSubstitute;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
using redb.Route.Transactions;
using Xunit;
namespace redb.Route.Tests.Transactions;
///
/// The transaction must decide commit vs. rollback from the exchange's terminal state, not only from
/// whether the inner processor threw: an OnException without Handled(true) leaves the
/// exception on the exchange and returns normally, and that is still a failure.
///
public class TransactedProcessorTerminalStateTests
{
private sealed class TrackingAction : ITransactedAction
{
public bool Committed { get; private set; }
public bool RolledBack { get; private set; }
public Task Commit(CancellationToken ct = default) { Committed = true; return Task.CompletedTask; }
public Task Rollback(CancellationToken ct = default) { RolledBack = true; return Task.CompletedTask; }
}
/// A transport step that defers inside the block, then the rest of the route.
private static IProcessor Sending(ITransactedAction action, IProcessor rest) => new DelegateProcessor(async (ex, ct) =>
{
TransactedActions.Register(ex, "send-1", action, "test-transport");
await rest.Process(ex, ct);
});
[Fact]
public async Task UnhandledExceptionLeftOnExchange_RollsBack_DoesNotCommit()
{
var inner = Substitute.For();
inner.Process(Arg.Any(), Arg.Any())
.Returns(ci => { ci.Arg().Exception = new InvalidOperationException("boom"); return Task.CompletedTask; });
var action = new TrackingAction();
var processor = new TransactedProcessor(Sending(action, inner), TransactionPolicy.Default);
var exchange = new Exchange(new Message { Body = "x" });
await processor.Process(exchange);
action.RolledBack.Should().BeTrue("an unhandled failure on the exchange is a failed transaction");
action.Committed.Should().BeFalse("nothing must be committed on a failed exchange");
exchange.Exception.Should().NotBeNull("the failure stays on the exchange for the consumer to settle");
}
[Fact]
public async Task HandledExceptionOnExchange_Commits()
{
var inner = Substitute.For();
inner.Process(Arg.Any(), Arg.Any())
.Returns(ci =>
{
var ex = ci.Arg();
ex.Exception = new InvalidOperationException("handled");
ex.ExceptionHandled = true;
return Task.CompletedTask;
});
var action = new TrackingAction();
var processor = new TransactedProcessor(Sending(action, inner), TransactionPolicy.Default);
var exchange = new Exchange(new Message { Body = "x" });
await processor.Process(exchange);
action.Committed.Should().BeTrue("a handled exception is a completed exchange");
action.RolledBack.Should().BeFalse();
}
}