Path: redb-route/tests/redb.Route.Tests/Idempotent/IdempotentUnitOfWorkTests.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/Idempotent/IdempotentUnitOfWorkTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Tests.Idempotent;
///
/// When the idempotent consumer confirms or removes its key (Apache Camel's defaults, completionEager=false and
/// removeOnFailure=true): at the end of the exchange's unit of work, not at the end of the block. A failure anywhere
/// in the exchange takes the key back, so the redelivery is processed again; a key claimed inside a transaction follows
/// the transaction instead.
///
public class IdempotentUnitOfWorkTests
{
private static Exchange Delivery(string id) => new(new Message("order") { Headers = { ["messageId"] = id } });
private static string MessageId(IExchange ex) => ex.In.GetHeader("messageId")!;
private static async Task Start(RouteContext context, string uri)
{
await context.Start();
var producer = context.GetEndpoint(uri).CreateProducer();
await producer.Start();
return producer;
}
[Fact]
public async Task A_failure_after_the_block_takes_the_key_back()
{
var repo = new InMemoryIdempotentRepository();
var work = 0;
var failNext = true;
await using var context = new RouteContext();
context.AddRoutes(r => r
.From("direct://uow-after")
.IdempotentConsumer(repo, MessageId)
.Process(_ => work++)
.EndIdempotentConsumer()
.Process(_ =>
{
if (!failNext) return;
failNext = false;
throw new InvalidOperationException("a step after the block");
}));
var producer = await Start(context, "direct://uow-after");
var first = () => producer.Process(Delivery("m-1"));
await first.Should().ThrowAsync();
(await repo.Contains("m-1")).Should().BeFalse("the exchange failed, so its message must be processed again");
await producer.Process(Delivery("m-1"));
work.Should().Be(2, "the redelivery is not a duplicate: the first attempt did not complete");
(await repo.Contains("m-1")).Should().BeTrue();
}
[Fact]
public async Task RollbackAll_in_the_block_takes_the_key_back()
{
var repo = new InMemoryIdempotentRepository();
var work = 0;
await using var context = new RouteContext();
context.AddRoutes(r => r
.From("direct://uow-rollback")
.IdempotentConsumer(repo, MessageId)
.Process(_ => work++)
.RollbackAll()
.EndIdempotentConsumer());
var producer = await Start(context, "direct://uow-rollback");
await producer.Process(Delivery("m-2"));
work.Should().Be(1);
(await repo.Contains("m-2")).Should().BeFalse(
"a rollback-only exchange is not acknowledged; a key left behind would skip its redelivery as a duplicate");
}
[Fact]
public async Task A_route_called_with_the_exchange_shares_the_callers_unit_of_work()
{
var repo = new InMemoryIdempotentRepository();
await using var context = new RouteContext();
context.AddRoutes(r =>
{
r.From("direct://uow-caller")
.To("direct://uow-callee")
.Process(_ => throw new InvalidOperationException("the caller fails after the callee returned"));
r.From("direct://uow-callee")
.IdempotentConsumer(repo, MessageId)
.Process(_ => { })
.EndIdempotentConsumer();
});
var producer = await Start(context, "direct://uow-caller");
var send = () => producer.Process(Delivery("m-3"));
await send.Should().ThrowAsync();
(await repo.Contains("m-3")).Should().BeFalse("the unit of work is the caller's, and the caller failed");
}
[Fact]
public async Task The_key_is_back_before_the_exchange_is_reported_failed()
{
var repo = new InMemoryIdempotentRepository();
var listener = new KeyAtFailure(repo, "m-4");
await using var context = new RouteContext();
context.AddLifecycleListener(listener);
context.AddRoutes(r => r
.From("direct://uow-order")
.IdempotentConsumer(repo, MessageId)
.Process(_ => { })
.EndIdempotentConsumer()
.Process(_ => throw new InvalidOperationException("after the block")));
var producer = await Start(context, "direct://uow-order");
var send = () => producer.Process(Delivery("m-4"));
await send.Should().ThrowAsync();
// The consumer settles the message after this point; a key still present here could skip a redelivery that
// another consumer picks up at once.
listener.KeyPresentAtFailure.Should().BeFalse();
}
[Fact]
public async Task A_redelivery_of_the_route_runs_a_block_that_failed_again()
{
var repo = new InMemoryIdempotentRepository();
var attempts = 0;
IExchange? seen = null;
await using var context = new RouteContext();
context.AddRoutes(r => r
.From("direct://uow-redeliver-block")
.OnException()
.MaximumRedeliveries(1)
.RedeliveryDelay(TimeSpan.FromMilliseconds(1))
.End()
.IdempotentConsumer(repo, MessageId)
.Process(ex =>
{
seen = ex;
if (++attempts == 1)
throw new InvalidOperationException("the block fails once");
})
.EndIdempotentConsumer());
var producer = await Start(context, "direct://uow-redeliver-block");
await producer.Process(Delivery("m-5"));
attempts.Should().Be(2, "the exchange holding the key is not a duplicate of itself");
seen!.Properties.Should().NotContainKey(IdempotentConsumerProcessor.DuplicatePropertyKey);
(await repo.Contains("m-5")).Should().BeTrue();
}
[Fact]
public async Task A_redelivery_of_the_route_does_not_redo_a_block_that_succeeded()
{
var repo = new InMemoryIdempotentRepository();
var work = 0;
var tail = 0;
IExchange? seen = null;
await using var context = new RouteContext();
context.AddRoutes(r => r
.From("direct://uow-redeliver-tail")
.OnException()
.MaximumRedeliveries(1)
.RedeliveryDelay(TimeSpan.FromMilliseconds(1))
.End()
.IdempotentConsumer(repo, MessageId)
.Process(_ => work++)
.EndIdempotentConsumer()
.Process(ex =>
{
seen = ex;
if (++tail == 1)
throw new InvalidOperationException("a step after the block fails once");
}));
var producer = await Start(context, "direct://uow-redeliver-tail");
await producer.Process(Delivery("m-6"));
work.Should().Be(1, "Camel redelivers the failed step, not the block before it");
tail.Should().Be(2);
seen!.Properties.Should().NotContainKey(IdempotentConsumerProcessor.DuplicatePropertyKey,
"the redelivery is the same exchange, not a duplicate message");
(await repo.Contains("m-6")).Should().BeTrue();
}
[Fact]
public async Task A_split_branch_has_its_own_unit_of_work()
{
var repo = new InMemoryIdempotentRepository();
await using var context = new RouteContext();
context.AddRoutes(r => r
.From("direct://uow-split")
.Split(e => (IEnumerable