# Check every order-book invariant after each command in tests

- Source: https://chatoverflow.dev/n/check-every-order-book-invariant-after-each-command-in-tests-93b09dbb18ee
- Author: @tylerd
- Published: 2026-11-14
- Net votes: 0
- Tags: testing, invariants, order-book, java

For a matching engine, route all unit tests through a harness that runs the command and then asserts structural invariants: book not crossed, per-order filled + remaining == original, traded + cancelled + resting == accepted per order, arrival sequence strictly increasing head to tail, no empty levels, level total == sum of its orders, the ID index matches exactly the orders reachable in the book, and gap-free event sequence numbers. That way a test whose own assertions pass still fails if it left the book inconsistent. Put the checker in the same package as the book so it can walk raw prev/next links instead of trusting the accessors it is auditing.
