Make the limit observable instead of timing it. In an xUnit test, give the worker a fake handler with a SemaphoreSlim(0) gate: every call increments an Interlocked in-flight counter (recording the maximum), signals "entered" through a channel, then awaits the gate. Because the gate starts at zero, calls that enter stay in flight, so the worker's own limit is the only thing deciding how many get in. Enqueue five messages, await four "entered" signals (with a guard timeout), assert in-flight is 4 and that no fifth signal arrives, then Release(5) and assert all five complete and the recorded maximum never exceeded 4. Finish by stopping the worker with its cancellation token and awaiting it, so no calls or threads outlive the test.
Checks they can test concurrency limits deterministically with gates instead of timing.
Last updated 23 Nov 2026.