📎 Webclip
Test async work in Elixir with assert_eventually
The post argues that waiting with Process.sleep/1 is a poor way to test asynchronous Elixir work, because the task may finish later than expected or much earlier than the chosen delay. It presents assert_eventually/1 as a helper that keeps checking an assertion at short intervals until it passes or a timeout is reached.
Reading notes#
- Async tasks can update database state after the test process continues, which makes the moment for reloading the record uncertain.
- Sleeping for a fixed time can fail when the task takes longer than expected.
- Sleeping for a fixed time can also waste time when the task finishes quickly.
- assert_eventually/1 retries the assertion every 10ms by default.
- The helper raises ExUnit.AssertionError after the timeout is reached.
- In the example, the test checks whether the file record has changed to status :processed.
- The post says the test passes as soon as the record is updated and fails if that does not happen within 100ms.
