Path: redb-route/tests/redb.Route.Tests/Processors/LoopProcessorTests.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/LoopProcessorTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Tests.Processors;
/// Tests for .
public class LoopProcessorTests
{
/// Count-based loop executes N times.
[Fact]
public async Task Process_CountBased_ExecutesNTimes()
{
var count = 0;
var body = new DelegateProcessor(_ => count++);
var loop = new LoopProcessor(body, 5);
await loop.Process(new Exchange());
count.Should().Be(5);
}
/// Count zero executes zero times.
[Fact]
public async Task Process_CountZero_NoExecution()
{
var count = 0;
var body = new DelegateProcessor(_ => count++);
var loop = new LoopProcessor(body, 0);
await loop.Process(new Exchange());
count.Should().Be(0);
}
/// Predicate-based loop stops when false.
[Fact]
public async Task Process_PredicateBased_StopsWhenFalse()
{
var iterations = 0;
var body = new DelegateProcessor(ex =>
{
iterations++;
var current = (int)(ex.In.Body ?? 0);
ex.In.Body = current + 1;
});
var loop = new LoopProcessor(body, ex => (int)(ex.In.Body ?? 0) < 3);
var exchange = new Exchange(new Message(0));
await loop.Process(exchange);
iterations.Should().Be(3);
exchange.In.Body.Should().Be(3);
}
/// Exchange.Stop() breaks the loop.
[Fact]
public async Task Process_ExchangeStopped_BreaksLoop()
{
var count = 0;
var body = new DelegateProcessor(ex =>
{
count++;
if (count >= 2) ex.Stop();
});
var loop = new LoopProcessor(body, 100);
await loop.Process(new Exchange());
count.Should().Be(2);
}
/// CancellationToken is respected.
[Fact]
public async Task Process_CancellationRespected()
{
using var cts = new CancellationTokenSource();
var count = 0;
var body = new DelegateProcessor((ex, ct) =>
{
count++;
if (count >= 3) cts.Cancel();
return Task.CompletedTask;
});
var loop = new LoopProcessor(body, 1000);
var act = () => loop.Process(new Exchange(), cts.Token);
await act.Should().ThrowAsync();
count.Should().BeGreaterThanOrEqualTo(3);
}
/// Negative maxIterations throws.
[Fact]
public void Constructor_NegativeCount_Throws()
{
var act = () => new LoopProcessor(new DelegateProcessor(_ => { }), -1);
act.Should().Throw();
}
// ── Copy Mode ──
/// Copy mode clones exchange each iteration — mutations don't accumulate.
[Fact]
public async Task Process_CopyMode_CountBased_IsolatesIterations()
{
var bodies = new List();
var body = new DelegateProcessor(ex =>
{
var val = (int)(ex.In.Body ?? 0);
bodies.Add(val);
ex.In.Body = val + 100; // mutate — should NOT carry to next iteration
});
var loop = new LoopProcessor(body, 3, copy: true);
var exchange = new Exchange(new Message(1));
await loop.Process(exchange);
// Each iteration should see original body = 1
bodies.Should().AllBeEquivalentTo(1);
// Final exchange body = last iteration result (1 + 100)
((int)exchange.In.Body!).Should().Be(101);
}
/// Copy mode with predicate loop.
[Fact]
public async Task Process_CopyMode_PredicateBased_IsolatesIterations()
{
var iteration = 0;
var body = new DelegateProcessor(ex =>
{
iteration++;
ex.In.Body = iteration; // mutate
});
// Predicate checks original exchange body (always 0 < 3),
// but iteration counter grows externally
var loop = new LoopProcessor(body, ex => iteration < 3, copy: true);
var exchange = new Exchange(new Message(0));
await loop.Process(exchange);
iteration.Should().Be(3);
// Last iteration wrote 3 to copy → merged back
((int)exchange.In.Body!).Should().Be(3);
}
/// Non-copy mode accumulates mutations across iterations.
[Fact]
public async Task Process_NoCopy_MutationsAccumulate()
{
var bodies = new List();
var body = new DelegateProcessor(ex =>
{
var val = (int)(ex.In.Body ?? 0);
bodies.Add(val);
ex.In.Body = val + 1;
});
var loop = new LoopProcessor(body, 3, copy: false);
var exchange = new Exchange(new Message(0));
await loop.Process(exchange);
bodies.Should().BeEquivalentTo(new[] { 0, 1, 2 });
((int)exchange.In.Body!).Should().Be(3);
}
/// Each iteration carries its index, and a counted loop its size, as Camel's loop properties.
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task Process_CountBased_PublishesLoopIndexAndSize(bool copy)
{
var seen = new List<(int Index, int Size)>();
var body = new DelegateProcessor(ex => seen.Add(
(ex.GetProperty(ExchangePropertyKeys.LoopIndex), ex.GetProperty(ExchangePropertyKeys.LoopSize))));
await new LoopProcessor(body, 3, copy).Process(new Exchange(new Message("x")));
seen.Should().Equal((0, 3), (1, 3), (2, 3));
}
/// A while loop carries its index and no size: the count is not known in advance.
[Fact]
public async Task Process_PredicateBased_PublishesLoopIndexOnly()
{
var seen = new List<(int Index, bool HasSize)>();
var body = new DelegateProcessor(ex =>
{
seen.Add((ex.GetProperty(ExchangePropertyKeys.LoopIndex), ex.Properties.ContainsKey(ExchangePropertyKeys.LoopSize)));
ex.In.Body = (int)ex.In.Body! + 1;
});
await new LoopProcessor(body, ex => (int)ex.In.Body! < 2).Process(new Exchange(new Message(0)));
seen.Should().Equal((0, false), (1, false));
}
}