Path: redb-route/redb.Route.TestKit/NotifyBuilder.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/redb.Route.TestKit/NotifyBuilder.cs
----
using redb.Route.Abstractions;
using redb.Route.Predicates;
namespace redb.Route.TestKit;
///
/// Builds a that waits for route events instead of a mock at the end of
/// the route (Apache Camel NotifyBuilder): "route orders has processed 3 exchanges
/// without failure" is ctx.Notify().FromRoute("orders").WhenDone(3).Create().
///
/// Scope (, , ) narrows which exchanges
/// are counted; the When* conditions are cumulative — the matcher matches when all of them hold.
///
///
public sealed class NotifyBuilder
{
private readonly IRouteContext _context;
private readonly List _routeIds = [];
private readonly List _fromMasks = [];
private readonly List>> _filters = [];
private int? _received, _completed, _failed, _done;
internal NotifyBuilder(IRouteContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
/// Counts only exchanges of the route with this id (repeatable).
public NotifyBuilder FromRoute(string routeId)
{
ArgumentException.ThrowIfNullOrWhiteSpace(routeId);
_routeIds.Add(routeId);
return this;
}
/// Counts only exchanges of routes whose From URI matches the mask (kafka://*, regex:...).
public NotifyBuilder From(string uriMask)
{
ArgumentException.ThrowIfNullOrWhiteSpace(uriMask);
_fromMasks.Add(uriMask);
return this;
}
/// Counts only exchanges for which holds.
public NotifyBuilder Filter(Func predicate)
{
ArgumentNullException.ThrowIfNull(predicate);
_filters.Add(e => Task.FromResult(predicate(e)));
return this;
}
/// Counts only exchanges for which holds (awaited).
public NotifyBuilder Filter(IPredicate predicate)
{
ArgumentNullException.ThrowIfNull(predicate);
_filters.Add(predicate.MatchesAsync);
return this;
}
/// Counts only exchanges for which the route-language condition holds (same compiler as Filter(string) in routes).
public NotifyBuilder Filter(string condition)
{
ArgumentException.ThrowIfNullOrWhiteSpace(condition);
_filters.Add(PredicateFactory.FromString(condition).MatchesAsync);
return this;
}
/// Matches once at least exchanges have entered the scoped routes.
public NotifyBuilder WhenReceived(int count) { _received = Positive(count); return this; }
/// Matches once at least exchanges completed without a propagated exception.
public NotifyBuilder WhenCompleted(int count) { _completed = Positive(count); return this; }
/// Matches once at least exchanges escaped their route with an exception.
public NotifyBuilder WhenFailed(int count) { _failed = Positive(count); return this; }
/// Matches once at least exchanges finished, completed or failed.
public NotifyBuilder WhenDone(int count) { _done = Positive(count); return this; }
/// Registers the matcher as a lifecycle listener on the context and starts counting.
public NotifyMatcher Create()
{
if (_received is null && _completed is null && _failed is null && _done is null)
throw new InvalidOperationException("NotifyBuilder needs at least one When* condition (WhenReceived / WhenCompleted / WhenFailed / WhenDone).");
var matcher = new NotifyMatcher(_context, _routeIds.ToArray(), _fromMasks.ToArray(), _filters.ToArray(),
_received, _completed, _failed, _done);
_context.AddLifecycleListener(matcher);
return matcher;
}
private static int Positive(int count) => count >= 1 ? count : throw new ArgumentOutOfRangeException(nameof(count), "Counts start at 1.");
}