Async tests should control the event being awaited rather than guess how long it takes. Explicit signals make race scenarios reproducible.
Before you start
You should understand inputs, expected outputs and the boundaries of the component under test. State the risk a test should detect before choosing a tool or mock. Distinguish controlled dependencies from real integrations so the result does not imply more coverage than the test actually provides.
The practical goal is to reason through this situation: Resolve the second request before the first to verify stale results cannot overwrite current data. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Control pending work
Use deferred completion rather than an arbitrary sleep.
Step 2: Choose response order
Resolve the newer request before the older one deliberately.
Step 3: Assert ownership
Only the current result should update visible state.
Worked scenario
Resolve the second request before the first to verify stale results cannot overwrite current data.
Request A starts, then B starts. The test resolves B and checks B’s result, then resolves A and checks that the display remains B. This reproduces the stale-response race without depending on machine speed. The dependency double should also model whether cancellation is cooperative or ignored.
Common mistake
Sleeping for a fixed interval introduces environment-dependent failures.
Verify the behavior
Repeat with success, rejection and ignored cancellation for the obsolete request.
Interview exercise
Test a timeout path.
Answer and reasoning
Use a controlled clock and pending dependency, advance to the deadline and assert cancellation and the reported outcome.
Continue learning
Compare the scenario with the Software Testing interview questions and test your understanding with the Software Testing MCQs. For terminology and implementation details, consult the reference material.