Path: redb-route/tests/redb.Route.Tests/Core/CorrelatedCopyTests.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/Core/CorrelatedCopyTests.cs ---- using System.Collections.Concurrent; using System.Diagnostics; using AwesomeAssertions; using Microsoft.Extensions.DependencyInjection; using redb.Route.Abstractions; using redb.Route.Components; using redb.Route.Core; using redb.Route.Tests.Telemetry; namespace redb.Route.Tests.Core; /// /// The id of an exchange that crosses to another unit of work, by Apache Camel's table: seda, vm, multicast, recipient /// list, scatter-gather, enrich and on-completion send a correlated copy (a new id, CamelCorrelationId = the /// immediate parent's id, as ExchangeHelper.createCorrelatedCopy); wire tap and poll enrich a copy with a new /// id and no correlation id; direct-vm, threads and loop keep the exchange's id. The original keeps its own id and /// its own correlation id after a fan-out merges back. /// public sealed class CorrelatedCopyTests { private sealed record Seen(string Id, string? Correlation, string? Route); /// A context that can reach vm and direct-vm endpoints (they share a process-wide registry from DI). private static RouteContext NewContext() { var services = new ServiceCollection(); services.AddSingleton(new SharedVmRegistry()); return new RouteContext(services.BuildServiceProvider(), "corr-" + Guid.NewGuid().ToString("N")); } private static Seen Record(IExchange ex) => new(ex.ExchangeId, ex.GetProperty(ExchangePropertyKeys.CorrelationId), ex.RouteId); private static async Task WaitUntil(Func condition, string what) { var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(10); while (DateTime.UtcNow < deadline) { if (condition()) return; await Task.Delay(20); } throw new TimeoutException("Timed out waiting for " + what); } /// /// Runs with a direct:in route that records the original first, sends one message /// and returns what the original and the direct:capture route saw. /// private static async Task<(Seen Origin, Seen[] Captured, IExchange After)> Run( Func define, int expected, string routeIdPrefix = "corr") { await using var ctx = NewContext(); Seen? origin = null; IExchange? after = null; var captured = new ConcurrentQueue(); ctx.AddRoutes(r => { define(r.From("direct:in").RouteId(routeIdPrefix + "-in").Process(ex => origin = Record(ex))) .Process(ex => after = ex); r.From("direct:capture").RouteId(routeIdPrefix + "-capture").Process(ex => captured.Enqueue(Record(ex))); }); await ctx.Start(); using var producer = new ProducerTemplate(ctx); producer.Start(); await producer.SendAsync("direct:in", "payload"); await WaitUntil(() => captured.Count >= expected, $"{expected} captured exchanges"); return (origin!, captured.ToArray(), after!); } // ── The copy itself ── [Fact] public void A_correlated_copy_has_a_new_id_and_the_parent_s_id_as_its_correlation_id() { var original = new Exchange(new Message("x")); var copy = original.CorrelatedCopy(); var linked = original.CorrelatedCopyLinked(); copy.ExchangeId.Should().NotBe(original.ExchangeId); linked.ExchangeId.Should().NotBe(original.ExchangeId).And.NotBe(copy.ExchangeId); copy.GetProperty(ExchangePropertyKeys.CorrelationId).Should().Be(original.ExchangeId); linked.GetProperty(ExchangePropertyKeys.CorrelationId).Should().Be(original.ExchangeId); original.Properties.Should().NotContainKey(ExchangePropertyKeys.CorrelationId); original.Clone().ExchangeId.Should().Be(original.ExchangeId, "Clone stays the identity copy"); } [Fact] public void After_two_hops_the_correlation_id_is_the_immediate_parent_s_as_in_camel() { var root = new Exchange(new Message("x")); var first = root.CorrelatedCopy(); var second = first.CorrelatedCopy(); second.GetProperty(ExchangePropertyKeys.CorrelationId).Should().Be(first.ExchangeId); first.GetProperty(ExchangePropertyKeys.CorrelationId).Should().Be(root.ExchangeId, "the root stays reachable by walking the chain"); } // ── Hops that send a correlated copy ── [Theory] [InlineData("seda:corr-q")] [InlineData("vm:corr-q")] public async Task A_seda_or_vm_hop_sends_a_correlated_copy(string queue) { await using var ctx = NewContext(); Seen? origin = null; var captured = new ConcurrentQueue(); ctx.AddRoutes(r => { r.From("direct:in").RouteId("in").Process(ex => origin = Record(ex)).To(queue); r.From(queue).RouteId("worker").Process(ex => captured.Enqueue(Record(ex))); }); await ctx.Start(); using var producer = new ProducerTemplate(ctx); producer.Start(); await producer.SendAsync("direct:in", "payload"); await WaitUntil(() => !captured.IsEmpty, "the queued copy"); var copy = captured.Single(); copy.Id.Should().NotBe(origin!.Id); copy.Correlation.Should().Be(origin.Id); } [Theory] [InlineData(false)] [InlineData(true)] public async Task Multicast_branches_are_correlated_copies_and_the_original_keeps_its_own_id(bool parallel) { var (origin, captured, after) = await Run( r => { var multicast = r.Multicast().AggregationStrategy((a, b) => b); if (parallel) multicast.ParallelProcessing(); return multicast.To("direct:capture").To("direct:capture").End(); }, expected: 2); captured.Select(c => c.Id).Should().OnlyHaveUniqueItems().And.NotContain(origin.Id); captured.Should().OnlyContain(c => c.Correlation == origin.Id); after.ExchangeId.Should().Be(origin.Id); after.Properties.Should().NotContainKey(ExchangePropertyKeys.CorrelationId, "the merge does not hand the branch's correlation id to the original"); } [Theory] [InlineData(false)] [InlineData(true)] public async Task Recipient_list_copies_are_correlated(bool parallel) { var (origin, captured, _) = await Run( r => r.RecipientList(_ => ["direct:capture", "direct:capture"], parallelProcessing: parallel), expected: 2); captured.Select(c => c.Id).Should().OnlyHaveUniqueItems().And.NotContain(origin.Id); captured.Should().OnlyContain(c => c.Correlation == origin.Id); } [Theory] [InlineData(false)] [InlineData(true)] public async Task Scatter_gather_copies_are_correlated_and_the_original_keeps_its_own_correlation_id(bool parallel) { var (origin, captured, after) = await Run( r => r.Process(ex => ex.Properties[ExchangePropertyKeys.CorrelationId] = "upstream") .ScatterGather(sg => sg.Recipients("direct:capture", "direct:capture") .AggregationStrategy((a, b) => b).ParallelProcessing(parallel)), expected: 2); captured.Should().OnlyContain(c => c.Correlation == origin.Id && c.Id != origin.Id); after.GetProperty(ExchangePropertyKeys.CorrelationId).Should().Be("upstream"); } [Fact] public async Task An_enrich_resource_exchange_is_correlated() { var (origin, captured, after) = await Run(r => r.Enrich("direct:capture"), expected: 1); captured.Single().Id.Should().NotBe(origin.Id); captured.Single().Correlation.Should().Be(origin.Id); after.ExchangeId.Should().Be(origin.Id); } [Fact] public async Task An_on_completion_copy_is_correlated() { var (origin, captured, _) = await Run( r => { r.OnCompletion().To("direct:capture").EndOnCompletion(); return r; }, expected: 1); captured.Single().Id.Should().NotBe(origin.Id); captured.Single().Correlation.Should().Be(origin.Id); } [Fact] public async Task A_split_part_is_correlated_and_the_merge_leaves_the_original_s_correlation_alone() { var (origin, captured, after) = await Run( r => r.Split(_ => new object?[] { "a", "b" }).AggregationStrategy((a, b) => b).To("direct:capture").End(), expected: 2); captured.Should().OnlyContain(c => c.Correlation == origin.Id && c.Id != origin.Id); after.Properties.Should().NotContainKey(ExchangePropertyKeys.CorrelationId); } // ── Hops that send a copy with a new id and no correlation id ── [Fact] public async Task A_wire_tap_copy_has_a_new_id_and_no_correlation_id() { var (origin, captured, _) = await Run( r => r.Process(ex => ex.Properties[ExchangePropertyKeys.CorrelationId] = "upstream").WireTap("direct:capture"), expected: 1); captured.Single().Id.Should().NotBe(origin.Id); captured.Single().Correlation.Should().BeNull("Camel's wire tap removes the correlation id from its copy"); } [Fact] public async Task A_poll_enrich_exchange_has_a_new_id_and_no_correlation_id() { var (origin, captured, _) = await Run(r => r.PollEnrich("direct:capture"), expected: 1); captured.Single().Id.Should().NotBe(origin.Id); captured.Single().Correlation.Should().BeNull(); } // ── Hops that keep the id ── [Fact] public async Task A_direct_vm_hop_keeps_the_exchange_id_as_camel_s_direct() { var queue = "direct-vm:corr-dvm-" + Guid.NewGuid().ToString("N")[..8]; await using var ctx = NewContext(); Seen? origin = null; Seen? seen = null; ctx.AddRoutes(r => { r.From("direct:in").RouteId("in").Process(ex => origin = Record(ex)).To(queue); r.From(queue).RouteId("dvm").Process(ex => seen = Record(ex)); }); await ctx.Start(); using var producer = new ProducerTemplate(ctx); producer.Start(); await producer.SendAsync("direct:in", "payload"); seen!.Id.Should().Be(origin!.Id); } [Fact] public async Task A_loop_with_copy_keeps_the_exchange_id_as_camel_s_createCopy() { var (origin, captured, _) = await Run(r => r.Loop(2, copy: true).To("direct:capture").End(), expected: 2); captured.Should().OnlyContain(c => c.Id == origin.Id); } [Fact] public async Task A_threads_hand_off_keeps_the_exchange_id_as_camel_s_threads() { var (origin, captured, _) = await Run(r => r.Threads(2).To("direct:capture").End(), expected: 1); captured.Single().Id.Should().Be(origin.Id); } // ── What operators see ── [Fact] public async Task A_seda_hop_is_its_own_inflight_entry_in_its_own_route() { await using var ctx = NewContext(); using var held = new SemaphoreSlim(0); using var release = new SemaphoreSlim(0); ctx.AddRoutes(r => { r.From("direct:in").RouteId("origin-route").To("seda:corr-inflight") .Process(async (_, ct) => { held.Release(); await release.WaitAsync(ct); }); r.From("seda:corr-inflight").RouteId("seda-route") .Process(async (_, ct) => { held.Release(); await release.WaitAsync(ct); }); }); await ctx.Start(); using var producer = new ProducerTemplate(ctx); producer.Start(); var send = producer.SendAsync("direct:in", "payload"); (await held.WaitAsync(TimeSpan.FromSeconds(10))).Should().BeTrue(); (await held.WaitAsync(TimeSpan.FromSeconds(10))).Should().BeTrue(); try { var inflight = ctx.InflightRepository.Browse(); inflight.Select(e => e.RouteId).Should().BeEquivalentTo("origin-route", "seda-route"); inflight.Select(e => e.ExchangeId).Should().OnlyHaveUniqueItems(); } finally { release.Release(2); await send; } } [Fact] public async Task Across_a_seda_hop_the_span_parentage_is_unchanged_and_the_span_carries_the_correlation_id() { var prefix = "corr-trace-" + Guid.NewGuid().ToString("N")[..8]; using var probe = RouteTelemetryProbe.ForRouteIdPrefix(prefix); await using var ctx = NewContext(); Seen? origin = null; var done = new TaskCompletionSource(); ctx.AddRoutes(r => { r.From("direct:in").RouteId(prefix + "-in").Process(ex => origin = Record(ex)).To($"seda:{prefix}"); r.From($"seda:{prefix}").RouteId(prefix + "-worker").Process(_ => done.TrySetResult()); }); await ctx.Start(); using var producer = new ProducerTemplate(ctx); producer.Start(); using (var root = new Activity("corr-test-root").Start()) { await producer.SendAsync("direct:in", "payload"); await done.Task.WaitAsync(TimeSpan.FromSeconds(10)); } await WaitUntil(() => probe.Activities.Any(a => RouteTelemetryProbe.Tag(a, RouteTelemetryProbe.RouteIdTag) == prefix + "-worker"), "the worker's span"); var originSpans = probe.Activities.Where(a => RouteTelemetryProbe.Tag(a, RouteTelemetryProbe.RouteIdTag) == prefix + "-in").ToList(); var workerSpans = probe.Activities.Where(a => RouteTelemetryProbe.Tag(a, RouteTelemetryProbe.RouteIdTag) == prefix + "-worker").ToList(); originSpans.Should().NotBeEmpty(); // As before the correlated copy (checked on develop 2026-11-19): the seda worker carries no trace context over // the queue, so its route span is the root of a trace of its own. The copy's new id does not change that; the // correlation id tag is what joins it to the sender now. var workerRoute = workerSpans.Single(s => s.OperationName.StartsWith("route:", StringComparison.Ordinal)); workerRoute.ParentSpanId.Should().Be(default(ActivitySpanId)); workerRoute.TraceId.Should().NotBe(originSpans[0].TraceId); workerSpans.Should().Contain(s => RouteTelemetryProbe.Tag(s, "redb.correlation.id") == origin!.Id); } }