Path: redb-route/redb.Route.TestKit/NotifyMatcher.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/NotifyMatcher.cs ---- using System.Diagnostics; using redb.Route.Abstractions; using redb.Route.Core; namespace redb.Route.TestKit; /// /// Counts exchange events (received / completed / failed) for the scope chosen in /// and reports when the configured conditions hold. /// Registered as an ; events arrive from the route's outermost /// wrapper, so "completed" and "failed" reflect the final outcome after error handling. /// public sealed class NotifyMatcher : IRouteLifecycleListener, IDisposable { private readonly IRouteContext _context; private readonly string[] _routeIds; private readonly string[] _fromMasks; private readonly Func>[] _filters; private readonly int? _received, _completed, _failed, _done; private int _receivedCount, _completedCount, _failedCount; private TaskCompletionSource _changed = NewSignal(); private volatile bool _disposed; internal NotifyMatcher(IRouteContext context, string[] routeIds, string[] fromMasks, Func>[] filters, int? received, int? completed, int? failed, int? done) { _context = context; _routeIds = routeIds; _fromMasks = fromMasks; _filters = filters; _received = received; _completed = completed; _failed = failed; _done = done; } /// Exchanges that entered the scoped routes. public int ReceivedCount => Volatile.Read(ref _receivedCount); /// Exchanges that completed without a propagated exception. public int CompletedCount => Volatile.Read(ref _completedCount); /// Exchanges that escaped their route with an exception. public int FailedCount => Volatile.Read(ref _failedCount); /// Completed plus failed. public int DoneCount => CompletedCount + FailedCount; /// true when every configured condition currently holds. public bool Matches() => (_received is null || ReceivedCount >= _received) && (_completed is null || CompletedCount >= _completed) && (_failed is null || FailedCount >= _failed) && (_done is null || DoneCount >= _done); /// Waits up to for the conditions to hold; false on timeout. public async Task MatchesAsync(TimeSpan timeout, CancellationToken ct = default) { var clock = Stopwatch.StartNew(); while (true) { var signal = Volatile.Read(ref _changed).Task; if (Matches()) return true; var remaining = timeout - clock.Elapsed; if (remaining <= TimeSpan.Zero) return false; await Task.WhenAny(signal, Task.Delay(remaining, ct)).ConfigureAwait(false); ct.ThrowIfCancellationRequested(); } } /// Zeroes the counters so the same matcher can be reused for the next batch. public void Reset() { Interlocked.Exchange(ref _receivedCount, 0); Interlocked.Exchange(ref _completedCount, 0); Interlocked.Exchange(ref _failedCount, 0); } /// Stops counting. The listener registration stays with the context but ignores further events. public void Dispose() => _disposed = true; // ── IRouteLifecycleListener ────────────────────────────────────────────── async Task IRouteLifecycleListener.OnExchangeReceived(string routeId, IExchange exchange, CancellationToken ct) { if (await InScope(routeId, exchange).ConfigureAwait(false)) Bump(ref _receivedCount); } async Task IRouteLifecycleListener.OnExchangeCompleted(string routeId, IExchange exchange, CancellationToken ct) { if (await InScope(routeId, exchange).ConfigureAwait(false)) Bump(ref _completedCount); } async Task IRouteLifecycleListener.OnExchangeFailed(string routeId, IExchange exchange, Exception exception, CancellationToken ct) { if (await InScope(routeId, exchange).ConfigureAwait(false)) Bump(ref _failedCount); } private async Task InScope(string routeId, IExchange exchange) { if (_disposed) return false; if (_routeIds.Length > 0 && !_routeIds.Contains(routeId, StringComparer.OrdinalIgnoreCase)) return false; if (_fromMasks.Length > 0) { var route = _context.Routes.FirstOrDefault(r => string.Equals(r.RouteId, routeId, StringComparison.OrdinalIgnoreCase)); if (route is null || !_fromMasks.Any(mask => UriMask.IsMatch(mask, route.FromUri))) return false; } foreach (var filter in _filters) if (!await filter(exchange).ConfigureAwait(false)) return false; return true; } private void Bump(ref int counter) { Interlocked.Increment(ref counter); Interlocked.Exchange(ref _changed, NewSignal()).TrySetResult(true); } private static TaskCompletionSource NewSignal() => new(TaskCreationOptions.RunContinuationsAsynchronously); }