diff --git a/docs/dev/rust-conventions.md b/docs/dev/rust-conventions.md index 0bd456d4..e95c71b7 100644 --- a/docs/dev/rust-conventions.md +++ b/docs/dev/rust-conventions.md @@ -386,3 +386,5 @@ Unit tests live in `#[cfg(test)] mod tests;` with a sibling `tests.rs`, so a module with tests is `foo/mod.rs` and `foo/tests.rs`. A test block of 30 lines or fewer may stay inline. A test file that outgrows one read becomes a `tests/` directory: `tests/mod.rs` holds the shared fixtures, and each flow gets a file. +[Testing](testing.md#write-a-focused-test) says which tests to write, and that +they wait for events, not time. diff --git a/docs/dev/testing.md b/docs/dev/testing.md index e93ff4b5..010652a2 100644 --- a/docs/dev/testing.md +++ b/docs/dev/testing.md @@ -157,8 +157,29 @@ that regenerates it. - **Test one behaviour, and name the test for what it proves**, such as `google_key_fetch_is_a_client_span_of_its_request`. Its doc comment says why the behaviour matters. +- **Test it once, at the lowest layer that fails without the change**: not + again in a wrapper, at each call site or on each surface. +- **Extend before you add.** When an existing test or table covers the + behaviour, add a row or an assertion to it; write a new test only when no case + fits. +- **A fix comes with a test that fails without it, written first**, when it + fixes logic, data, state or a protocol. A purely visual fix, such as spacing, + colour or alignment, is shown with before and after screenshots in the pull + request instead, and gets no test. - **Assert the outcome**, not that something ran. A test that only checks a page loads or a command exits 0 proves little and still has to be maintained. +- **Wait for events, not time.** No `thread::sleep` or `tokio::time::sleep` to + let something happen, no assertions on how much real time passed, and no + retries: await the channel, notification or state change that marks the + milestone, such as a `watch` the test's fake sends on, or take a lock with + `Database::exclusive` to wait for its holder. Move tokio's clock with + `#[tokio::test(start_paused = true)]`, or with `pause`, `advance` and `resume` + between database calls, so the clock is never paused across database I/O, + and end an expiry PostgreSQL judges at once, as `SessionRepository::expire` + and `RecoveryRepository::expire` do. When nothing signals the milestone, as for a + real subprocess or network peer, a lock wait PostgreSQL shows only in its + catalog views (`until_blocked`), or production code that reads the system + clock, poll or sleep and say why on the line above: `// wall-clock: `. - **Don't test that a removed feature stays removed.** Once the code is gone, there is nothing left to break. - **Keep browser tests few.** Each spec says at its top what it covers. Auth's