📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How would you test that a worker never processes more than four messages at the same time?

Mid-Level Coding Intermediate

🎯 Key points

  • SemaphoreSlim(0) in the fake holds calls in flight, so the worker's own limit decides how many enter
  • Interlocked counter tracks current and maximum concurrent calls
  • Await an 'entered' signal per call (Channel), with a guard timeout
  • Assert exactly four in flight and no fifth entry, then release the gate
  • Assert all messages complete and the maximum never exceeded the limit
  • Stop the worker via its CancellationToken and await it for clean teardown

⏱ 30-second answer

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.

🔍 What the interviewer is checking

Checks they can test concurrency limits deterministically with gates instead of timing.

↩ Usually asked after

More in Senior Testing Topics

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me