Path: redb-route/tests/redb.Route.Tests/Processors/ParallelFanOutTransactionTests.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/ParallelFanOutTransactionTests.cs ---- using System.Collections.Concurrent; using System.Transactions; using AwesomeAssertions; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Transactions; namespace redb.Route.Tests.Processors; /// /// A parallel scatter-gather or recipient list inside .Transacted() follows the rule of every parallel fan-out /// ( for Multicast and Split): each branch runs under a dependent clone /// of the route's transaction, never the transaction itself, and a branch that fails rolls the whole block back, in /// best-effort mode too, so the deferred sends of the other branches never go out. /// public sealed class ParallelFanOutTransactionTests { private sealed record Seen(Transaction? Transaction, string Kind); /// /// Runs a transacted direct:in whose fan-out sends to two direct:branch routes held in flight together. /// private static async Task<(Transaction? Parent, Seen[] Branches)> RunConcurrent( Func fanOut) { await using var ctx = new RouteContext("fanout-tx-" + Guid.NewGuid().ToString("N")); var seen = new ConcurrentQueue(); Transaction? parent = null; using var bothIn = new CountdownEvent(2); ctx.AddRoutes(r => { fanOut(r.From("direct:in").Transacted().Process(_ => parent = Transaction.Current)).EndTransaction(); r.From("direct:branch").Process(async (_, ct) => { var current = Transaction.Current; seen.Enqueue(new Seen(current, current?.GetType().Name ?? "(none)")); bothIn.Signal(); // Both branches are inside their step at once: the fan-out really runs them in parallel. await Task.Run(() => bothIn.Wait(TimeSpan.FromSeconds(10)), ct); }); }); await ctx.Start(); using var producer = new ProducerTemplate(ctx); producer.Start(); await producer.SendAsync("direct:in", "x"); return (parent, seen.ToArray()); } private static void EachBranchUnderADependentClone(Transaction? parent, Seen[] branches) { parent.Should().NotBeNull(); branches.Should().HaveCount(2); branches.Should().OnlyContain(b => b.Kind == nameof(DependentTransaction), "concurrent branches never share one Transaction.Current"); branches.Should().OnlyContain(b => !ReferenceEquals(b.Transaction, parent)); } [Fact] public async Task Parallel_scatter_gather_branches_run_under_dependent_clones() { var (parent, branches) = await RunConcurrent(r => r.ScatterGather(sg => sg .Recipients("direct:branch", "direct:branch") .AggregationStrategy((a, b) => b) .ParallelProcessing())); EachBranchUnderADependentClone(parent, branches); } [Fact] public async Task Parallel_recipient_list_branches_run_under_dependent_clones() { var (parent, branches) = await RunConcurrent(r => r.RecipientList(_ => ["direct:branch", "direct:branch"], parallelProcessing: true)); EachBranchUnderADependentClone(parent, branches); } /// /// A transacted direct:in fanning out in best-effort mode to direct:ok, which defers a send to the block, /// and direct:fail, which throws. Returns whether the deferred send went out, the outcome of the route's /// transaction and what the caller saw. /// private static async Task<(bool Sent, TransactionStatus? Outcome, Exception? Failure)> RunWithAFailedBranch( Func fanOut) { await using var ctx = new RouteContext("fanout-tx-" + Guid.NewGuid().ToString("N")); var sent = false; TransactionStatus? outcome = null; ctx.AddRoutes(r => { fanOut(r.From("direct:in").Transacted().Process(_ => Transaction.Current!.TransactionCompleted += (_, e) => outcome = e.Transaction!.TransactionInformation.Status)) .EndTransaction(); r.From("direct:ok").Process(ex => TransactedActions.RegisterSend(ex, "ok-send", _ => { sent = true; return Task.CompletedTask; }, "test")); r.From("direct:fail").Process(_ => throw new InvalidOperationException("branch failed")); }); await ctx.Start(); using var producer = new ProducerTemplate(ctx); producer.Start(); Exception? failure = null; try { await producer.SendAsync("direct:in", "x"); } catch (Exception ex) { failure = ex; } return (sent, outcome, failure); } [Fact] public async Task A_failed_scatter_gather_branch_rolls_the_block_back_in_best_effort_mode() { var (sent, outcome, failure) = await RunWithAFailedBranch(r => r.ScatterGather(sg => sg .Recipients("direct:ok", "direct:fail") .AggregationStrategy((a, b) => b) .ParallelProcessing() .StopOnException(false))); sent.Should().BeFalse("the deferred send of the branch that succeeded is dropped with the block"); outcome.Should().Be(TransactionStatus.Aborted); failure.Should().NotBeNull("the block could not commit"); } [Fact] public async Task A_sequential_fan_out_in_best_effort_mode_still_commits_after_a_failed_branch() { // UPGRADING's way back to the old outcome: sequential branches run on the route's transaction and are not isolated. var (sgSent, sgOutcome, sgFailure) = await RunWithAFailedBranch(r => r.ScatterGather(sg => sg .Recipients("direct:ok", "direct:fail") .AggregationStrategy((a, b) => b) .ParallelProcessing(false) .StopOnException(false))); var (rlSent, rlOutcome, rlFailure) = await RunWithAFailedBranch(r => r.RecipientList(_ => ["direct:ok", "direct:fail"], parallelProcessing: false, stopOnException: false)); (sgSent, sgOutcome, sgFailure).Should().Be((true, TransactionStatus.Committed, (Exception?)null)); (rlSent, rlOutcome, rlFailure).Should().Be((true, TransactionStatus.Committed, (Exception?)null)); } [Fact] public async Task A_failed_recipient_list_branch_rolls_the_block_back_in_best_effort_mode() { var (sent, outcome, failure) = await RunWithAFailedBranch(r => r.RecipientList(_ => ["direct:ok", "direct:fail"], parallelProcessing: true, stopOnException: false)); sent.Should().BeFalse("the deferred send of the branch that succeeded is dropped with the block"); outcome.Should().Be(TransactionStatus.Aborted); failure.Should().NotBeNull("the block could not commit"); } }