Path: redb-route/tests/redb.Route.Tests/Processors/AggregatorOwnershipTests.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/AggregatorOwnershipTests.cs
----
using System.Collections.Concurrent;
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Abstractions;
using redb.Route.Components;
using redb.Route.Core;
namespace redb.Route.Tests.Processors;
///
/// An aggregation group outlives the calls that fed it, so it holds exchanges of its own, as Camel copies an exchange
/// into its aggregation repository: the body of a message waiting in a group stays open until the group is done, the
/// completed group runs on in a DI scope of its own, and everything the group held is released once, after it has gone
/// on down the route. The aggregator used to keep the caller's exchange itself, which the caller disposes as soon as
/// the aggregator returns: a group completing later went on with closed streams and a released scope.
///
public class AggregatorOwnershipTests
{
private sealed class TrackedStream() : MemoryStream("payload"u8.ToArray())
{
public int Disposals;
protected override void Dispose(bool disposing)
{
if (disposing) Interlocked.Increment(ref Disposals);
base.Dispose(disposing);
}
}
private sealed class ScopedProbe
{
public bool Used;
}
private sealed record Outcome(int FirstBodyDisposalsWhenCompleted, bool ScopeWorked);
private static async Task<(RouteContext Context, ConcurrentQueue Completed)> Route()
{
var services = new ServiceCollection();
services.AddScoped();
var provider = services.BuildServiceProvider();
var context = new RouteContext($"aggregator-owner-{Guid.NewGuid():N}");
context.SetServiceProvider(provider);
context.AddComponent(new DirectComponent());
var completed = new ConcurrentQueue();
context.AddRoutes(r => r.From("direct://owner-in")
.Aggregate(_ => "g", static (acc, next) =>
{
var bodies = acc.In.Body as List