D.12 Determinism, resume and the guard: the tests that protect the promises
What you will learn
- Read the test that proves results do not depend on the thread count, and how it removes the one column that legitimately differs.
- Prove that a resumed run equals an uninterrupted one.
- Test the internal-error guard, and compare numbers with a tolerance rather than equality when equality is the wrong question.
Page D.5 stated three promises. This page shows how each is tested.
Promise: the same numbers whatever the thread count
The test of the simulation–estimation power analysis is the model. In prose:
- Run the power command twice into two output folders, with the same seed and settings but with one thread in the first and three in the second.
- Read the replicate tables written by each run.
- Remove the one column that holds the run's duration, since timings legitimately differ, and compare the remaining text of the two tables for equality.
- Assert that they are equal, with a message that says exactly what broke: "the results depend on the threads".
It is a strong test and a cheap one: three replicates, one sample size. It would fail if any code path used a stream shared between threads, or summed in an order that depends on scheduling. Notice that it compares text, not numbers within a tolerance: when the claim is "bit-identical", the test says so.
Promise: a resumed run equals an uninterrupted one
The same test continues. After the first run is complete:
- run the same command again with
--resumeand a third thread count; nothing is left to simulate, so the run reads the stored replicates and writes its summary; - assert that the replicate table is identical to the original (again ignoring duration);
- assert that the summary file exists and holds the expected content (the name of the hypothesis).
The scientific form of this check appears twice in Chapter 41: page 41.34, where a resumed power run found its ten stored replicates and wrote the summary without simulating again, and page 41.23, where an interrupted covariate run, resumed, was compared with an uninterrupted control. The development test above is the miniature; a real interruption (stopping a long run part-way) is tested with a longer run, because the interesting case is "some replicates done, some not".
A subtlety for maintainers: resume is bound to the settings. A checkpoint whose settings differ from those of the new run is refused with exit code 11, as is one written under a different derivation of the random streams (page D.5). A test of the refusal is as important as a test of the success.
Promise: a bug is an exit code
The end-to-end suite contains a test whose only purpose is to make the program panic. An environment variable, read in the guarded part of the start-up, raises a deliberate panic. The test runs a harmless command (schema) with the variable set and asserts:
| Assertion | Protects against |
|---|---|
| exit code is 12 | a crash that exits with the language's own code |
| the output contains "internal error (a bug in nlmx)" and "exit code 12" | a failure that is not reported through the normal logging |
| the output does not contain the raw panic text | the user seeing an unformatted trace |
A guard that is never exercised is an assumption; this test turns it into a fact, and it runs on every change.
When equality is the wrong question
Not every numerical claim is "bit-identical". Three cases.
| Claim | Test | Why |
|---|---|---|
| The same program gives the same file on every run | byte equality (after removing timings) | it is the promise of determinism |
| The result agrees with an independent calculation | tolerance, stated with the test (absolute for values near zero, relative otherwise) | two correct algorithms differ in the last digits |
| A fast path equals a slow path | tolerance, or equality where both are exact | an optimisation must not change the answer; the cases where it is meant to differ (a different solver) are documented |
The tolerance policy of the validation package (Chapter 42, 42.6) is the book's statement of the second kind. In development tests the tolerance sits beside the assertion, with the reason, so that a reviewer can see whether it is loose.
Flaky tests
An end-to-end test that passes and fails on the same code has one of three causes, in decreasing order of likelihood: a shared random stream (determinism defect: fix the program), a shared file or folder between tests (isolate with a scratch folder), or timing (a time limit set too tight). The cure is never to retry. Retrying hides the first, which is a scientific defect.
Review checklist
- If the change adds a random consumer, does a test compare one thread against several?
- If it adds a checkpoint, does a test cover resume, and refusal of a mismatched resume?
- Is the tolerance of every approximate comparison stated, with its reason?
- Do you treat an intermittent failure as a defect and not as noise?
Navigation
See the previous page, the next page and the developer reference.