Path: redb-route/tests/redb.Route.Tests/Processors/DebounceTests.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/DebounceTests.cs
----
using System.Collections.Concurrent;
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Definitions;
using Microsoft.Extensions.Time.Testing;
using redb.Route.Processors;
namespace redb.Route.Tests.Processors;
///
/// Unit tests for DebounceProcessor, DSL, and step creation.
///
public class DebounceTests
{
private static IExchange CreateExchange(object? body = null, string? key = null)
{
var ex = Exchange.Create(new Message(body), null);
if (key != null) ex.In.Headers["DebounceKey"] = key;
return ex;
}
private static Func KeyFromHeader =>
e => e.In.Headers.TryGetValue("DebounceKey", out var v) ? v?.ToString() ?? "" : "";
// ══════════════════════════════════════════════════════════════
// Constructor validation
// ══════════════════════════════════════════════════════════════
[Fact]
public void Constructor_NullNext_Throws()
{
var act = () => new DebounceProcessor(null!, KeyFromHeader, TimeSpan.FromMilliseconds(100));
act.Should().Throw().WithParameterName("next");
}
[Fact]
public void Constructor_NullKeyExtractor_Throws()
{
var next = new DelegateProcessor(_ => { });
var act = () => new DebounceProcessor(next, null!, TimeSpan.FromMilliseconds(100));
act.Should().Throw().WithParameterName("keyExtractor");
}
[Fact]
public void Constructor_ZeroQuietPeriod_Throws()
{
var next = new DelegateProcessor(_ => { });
var act = () => new DebounceProcessor(next, KeyFromHeader, TimeSpan.Zero);
act.Should().Throw().WithParameterName("quietPeriod");
}
[Fact]
public void Constructor_NegativeQuietPeriod_Throws()
{
var next = new DelegateProcessor(_ => { });
var act = () => new DebounceProcessor(next, KeyFromHeader, TimeSpan.FromMilliseconds(-1));
act.Should().Throw().WithParameterName("quietPeriod");
}
// ══════════════════════════════════════════════════════════════
// Processor behavior. The quiet period runs on a FakeTimeProvider (findings register row 21): time moves only
// when a test advances it, so no assertion depends on how loaded the machine is. A forward runs as a
// continuation on the thread pool after its timer fires, so Forwarded waits for it to be observed.
// ══════════════════════════════════════════════════════════════
private static readonly TimeSpan Quiet = TimeSpan.FromMilliseconds(100);
private static async Task Forwarded(ConcurrentBag forwarded, int count)
{
var deadline = DateTime.UtcNow.AddSeconds(5);
while (forwarded.Count < count && DateTime.UtcNow < deadline)
await Task.Delay(5);
}
[Fact]
public async Task SingleMessage_ForwardedAfterQuietPeriod()
{
var time = new FakeTimeProvider();
var forwarded = new ConcurrentBag