Tests must not sleep — sleeping is the number one source of flaky, slow suites.
A Process.sleep/1 in a test guesses how long the system under test needs. The
guess is either too short (flaky under load) or too long (slow suite), and it is
usually both over the life of the test. Synchronize on the event itself instead:
assert_receive/refute_receive with a timeout, Task.await/2, or a polling
helper that re-checks a condition until a deadline.
# BAD — guesses that 100ms is enough for the broadcast to arrive
test "broadcasts the update" do
Orders.update_status(order, :shipped)
Process.sleep(100)
assert_received {:order_updated, %{status: :shipped}}
end
# GOOD — waits exactly as long as needed, up to a timeout
test "broadcasts the update" do
Orders.update_status(order, :shipped)
assert_receive {:order_updated, %{status: :shipped}}, 500
endBoth the Elixir and the erlang spelling are caught:
Process.sleep(100) # caught
:timer.sleep(100) # caughtThe check only runs on test files, identified by filename via the :test_files
param — a sleep in lib/ code (backoff, rate limiting) is outside this rule.