Path: redb-route/tests/redb.Route.Tests/ControlBus/ControlBusNotifyTests.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/ControlBus/ControlBusNotifyTests.cs
----
using System.Collections.Concurrent;
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.ControlBus;
using redb.Route.Core;
namespace redb.Route.Tests.ControlBus;
///
/// Tests for the controlbus:notify event consumer (redb extension beyond Camel): route/context
/// lifecycle events surfaced as exchanges into a route, with optional event/route filters.
///
public class ControlBusNotifyTests
{
private static async Task Eventually(Func condition, int seconds = 5)
{
var deadline = DateTime.UtcNow.AddSeconds(seconds);
while (DateTime.UtcNow < deadline)
{
if (condition()) return true;
await Task.Delay(25);
}
return condition();
}
[Fact]
public async Task Notify_EmitsRouteStoppedEvent()
{
var events = new ConcurrentBag<(string ev, string? routeId)>();
await using var ctx = new RouteContext();
ctx.AddRoutes(r =>
{
r.From("direct://worker").RouteId("worker").Process(_ => { });
r.From("controlbus:notify").RouteId("notify")
.Process(e => events.Add((
e.In.GetHeader(ControlBusHeaders.Event)!,
e.In.GetHeader(ControlBusHeaders.RouteId))));
});
await ctx.Start();
await ctx.StopRoute("worker");
(await Eventually(() => events.Any(x => x.ev == "RouteStopped" && x.routeId == "worker")))
.Should().BeTrue("stopping 'worker' should surface a RouteStopped event on controlbus:notify");
}
[Fact]
public async Task Notify_EventsFilter_OnlyMatchingEvents()
{
var events = new ConcurrentBag();
await using var ctx = new RouteContext();
ctx.AddRoutes(r =>
{
r.From("direct://w").RouteId("w").Process(_ => { });
r.From("controlbus:notify?events=RouteStarted").RouteId("n")
.Process(e => events.Add(e.In.GetHeader(ControlBusHeaders.Event)!));
});
await ctx.Start();
await ctx.StopRoute("w"); // RouteStopped — must be filtered out
await ctx.StartRoute("w"); // RouteStarted — must be captured
(await Eventually(() => events.Contains("RouteStarted"))).Should().BeTrue();
events.Should().NotContain("RouteStopped");
}
[Fact]
public async Task Notify_RouteFilter_OnlyThatRoute()
{
var routeIds = new ConcurrentBag();
await using var ctx = new RouteContext();
ctx.AddRoutes(r =>
{
r.From("direct://a").RouteId("a").Process(_ => { });
r.From("direct://b").RouteId("b").Process(_ => { });
r.From("controlbus:notify?routeId=a").RouteId("n2")
.Process(e => routeIds.Add(e.In.GetHeader(ControlBusHeaders.RouteId)));
});
await ctx.Start();
await ctx.StopRoute("a");
await ctx.StopRoute("b");
(await Eventually(() => routeIds.Any(x => x == "a"))).Should().BeTrue();
routeIds.Should().NotContain("b");
}
[Fact]
public async Task Notify_IsConsumerOnly_ProducerThrows()
{
await using var ctx = new RouteContext();
var endpoint = ctx.GetEndpoint("controlbus:notify");
var act = () => endpoint.CreateProducer();
act.Should().Throw();
await Task.CompletedTask;
}
}