Why are fixed sleeps a weak synchronization strategy, and what should replace them?
Answer
A fixed delay is either unnecessarily slow or still too short under load. Wait for an observable condition such as a user-visible state, network completion or domain event, with a bounded timeout and diagnostics for the unmet condition.
Strong answer includes
- condition-based synchronization
- bounded timeout
- diagnosable failure
Practical example
A test with 'sleep(3000)' before clicking a button passes locally but flakes 1 in 20 times on a slower CI runner; replacing it with a wait for the button to become enabled, bounded by a 10-second timeout that dumps the button's actual state on failure, eliminates the flakiness and makes any real failure immediately debuggable.