Plan: Single-service API tests run in-process

Status

Done (2026-11-20) — the handshake crates are built optimised (an in-process test went from about 535 to 400 ms on the runner, and the whole battery’s summed test time fell 8 to 10 %); 30 API test files (205 tests) of craig-cases, craig-financial and craig-placement moved in-process, far fewer than surveyed, and a moved test runs about 0.37 s slower than it did on the deployed stack.

Closes: #2302, on the last step.
Tree: written against main.
Ruling (maintainer, 2026-11-18, on #2302): single-service API tests move in-process in bulk; the devstack tier keeps cross-service, broker and worker tests. First profile the slowdown and fix what can be fixed, then port.
Ruling (maintainer, 2026-11-20, in conversation): this order: the speed step, a measurement on a car, then the port one service at a time, craig-cases first; the database drop is left alone.

Erratum (2026-11-20): the shared-database census reads free functions only. A function in an impl or trait block is never an opener, and a method or Type::name(..) call is not followed: matching new, connect or default by name marked every file. An unqualified call of a function the calling file defines resolves to that file’s function, not by name across the tree.

Erratum (2026-11-20): the context survey missed two reasons a craig-cases file cannot move: it mints a service principal’s token from the realm, or it drives a route that consults the rules service (screening, conversion, safety assessment, IV-E), which answers 503 in-process. With those counted, 25 of craig-cases' 75 files (124 of 562 call sites) have no blocker, not 65. The first craig-cases step moves seven of them.

Erratum (2026-11-20): a file that uploads or downloads an attachment also stays. The adapters store objects in a local scratch directory, the devstack in its S3 store, so moving such a file would drop the object store from what it covers. In place of base_url() and token(role), the next step adds a second trait, craig_test_lib::harness::Principals (the endpoint table, a user token, a service token), for TestHarness and each adapter, so a helper that names its own principal serves both.

Erratum (2026-11-20): outcome. Moved: 20 craig-cases files (127 tests), nine craig-financial (63), one craig-placement (15). In craig-placement 345 of 464 tests first build a real case through craig-cases, so one file moved. payments.rs (craig-financial) stayed because an empty database would satisfy its empty-page assertion. On the runner the 205 moved tests went from a median of 89 ms to 459 ms (summed 30 s to 101 s) while the 1,889 unmoved tests of the same suites held at about 300 ms; nextest’s wall time went from 481 s to 492 s. No max-threads = 1 group lost a test: 13 before and after. No file needed outbox_events or a nextest change.

Context

  1. Plan plans/archive/inproc-tests.adoc (#3119) built craig_test_lib::inproc and one adapter per service (services/craig-{cases,placement,financial}/tests/api/inproc_*.rs), and moved three pilot files (71 tests). On the car they ran about 0.45 s slower per test than on the deployed stack.

  2. Six profiling rounds are on #2302 (2026-11-19 and 2026-11-20, craig-cases' 18 pilot tests, on luchs). What they show:

    • The cost is fixed per test, because nextest runs each test in its own process: every test clones a database, boots the service and drops the database.

    • A PostgreSQL connection costs 20 to 26 ms, of which the SCRAM password hashing in debug-built code is all but 3 ms. A test opens five.

    • With six hash crates and sqlx-postgres built at opt-level = 3, the median test goes from 571–632 ms to 419–472 ms in parallel (six alternated pairs), and from 501–536 ms to 364–404 ms serially.

    • The DROP DATABASE statement is a constant 104 ms inside PostgreSQL; the OpenAPI document costs 43 ms per router.

  3. Not measured: the CI runner, any service but craig-cases, and the build-time cost of the setting.

  4. How much can move, by a source survey of the files that call TestHarness::new( (a file counts as outside its service when it names a peer’s client or URL, another service’s database, an event- or job-completion wait, the mock server or the broker):

    Service Files Call sites In files with no outside signal

    craig-cases

    79

    599

    451 in 65 files (75%)

    craig-placement

    60

    492

    360 in 48 files (73%)

    craig-financial

    47

    332

    190 in 30 files (57%)

    The survey is by pattern and is an upper bound: a file that reads rows the devstack seed put there has no signal and still cannot move, because a throwaway database holds the migrated schema and no seed.

Design

D1. The handshake crates are built optimised in the dev profile

Cargo.toml already carries [profile.dev.package.<crate>] opt-level = 3 for the parser stack (#2676). It gains the same entry for sha2, hmac, digest, block-buffer, crypto-common, generic-array and sqlx-postgres, with a comment giving the measurement. The car’s devstack profile inherits the dev profile’s package overrides (checked on 2026-11-20 by building memchr and sha2 under both), so the car’s test build and the devstack’s service binaries both get it. No release profile is touched.

D2. The measurement, on a car

The car’s junit holds each test’s time. For the 71 pilot tests (cases::, foster_homes::, request_claims_financial::), the median and the sum are read from a train car on main before D1 and from D1’s own train car, with each car’s car job duration beside them (the build-time cost). Posted on #2302. One car each side, so run-to-run noise is stated, not hidden.

The port goes ahead whatever D2 shows: it is ruled. D2 records what the port costs per test on the runner.

D3. What moves, and what a moved file may do

A file moves whole from TestHarness to its service’s adapter when every test in it reads only that service’s responses and that service’s database, and needs no seeded row. Every other file stays on TestHarness unchanged and is listed, with its reason, in the step’s merge request. No file is split and no test is rewritten to make it movable.

In a moved file:

  • TestHarness::new().await.unwrap() becomes the adapter’s boot().await; client calls are unchanged (D3a).

  • A direct pool on the service’s shared database (craig_test_lib::postgres_test_db_url("craig_<svc>"), or a cases_pool() / placement_pool() / financial_pool() helper that opens it) becomes the adapter’s pool(), the throwaway database the router serves.

  • A register_cleanup / register_delete call is deleted: the database is dropped with the adapter, so there is nothing to clean. This is the one body change allowed beyond the harness and pool lines.

  • An assertion on a published event reads the staged envelope with inproc::outbox_events.

  • The test keeps #[ignore = "requires devstack"]: the throwaway database lives on the devstack’s postgres-test instance.

  • A test that leaves a max-threads = 1 nextest group because its data is now its own is removed from that group’s filter in .config/nextest.toml in the same merge request. A group left empty is deleted.

Authorization stays the shipped engine: inproc offers no allow-all engine, and the existing census (crates/craig-test-lib/src/inproc/allow_all_census.rs) covers every file that names an adapter.

D3a. The seams the port needs (step 3 builds them, before any file moves)

  • One client surface for both harnesses. crates/craig-test-lib/src/harness.rs generates TestHarness’s per-role constructors with the `harness_clients! macro: pub async fn caseworker_cases_persons_client(&self) → Result<CasesPersonsClient> and so on, each a call of client_for_role::<C>(role). The macro additionally emits a trait, craig_test_lib::harness::RoleClients, with one required method, async fn client_for_role<C: TypedClient>(&self, role: Role) → Result<C>, and the same per-role constructors as provided methods (async, returning Result, same names). TestHarness keeps its inherent methods and also implements the trait; each adapter implements the one required method. A moved file imports the trait, and harness.caseworker_cases_persons_client().await.unwrap() compiles unchanged.

  • How an adapter answers client_for_role. TypedClient::base_url reads a TestConfig. Each adapter holds a TestConfig whose own service URL is its loopback origin and whose every other service URL is inproc::refusing_peer(), and builds C::from_parts(shared_http_client(), C::base_url(&config), &issuer.role_token(role)). A client of another service therefore builds and then fails to connect, loudly. The adapters' existing Role-taking client methods (cases_client(role) and the like) are deleted and the three pilot files move to the trait, so there is one way to get a client.

  • pool(), base_url() and token(role) on every adapter. InprocCases keeps its database handle and gains pool() (the placement and financial adapters have it). All three gain base_url() and token(role), for the raw untyped clients some files build (claims_common::raw(base_url, token)).

  • Shared fixture modules. A helper used by both moved and staying files changes as follows, and nothing else in it changes:

    • a helper that takes &TestHarness only to obtain clients (for example claims_common::seed_person) takes &impl RoleClients, which both harnesses satisfy;

    • a helper that opens the shared database itself (for example payment_fixtures::owned_pending_payment, which also registers a cleanup) keeps its TestHarness form for the files that stay, and its pool-taking core (insert_pending_payment(pool, spec)) is what a moved file calls with the adapter’s pool;

    • a *_pool() helper that opens the shared database is not called from a moved file.

D4. Decided against, with the numbers

  • Not waiting on the database drop (104 ms a test). The drop is what keeps postgres-test, which lives on tmpfs, from holding a thousand databases at once. Left alone by ruling.

  • Building the OpenAPI document once (43 ms a test). Each test is its own process and builds one router, so there is nothing to share; skipping the document would make the in-process router differ from the served one.

  • Skipping the per-test template check (14 to 35 ms a test). The check is also what resumes a half-built template; a faster path would need its own proof for about 5 s of car time.

Scope and order

One merge request per step, each green alone.

  1. D1. The seven Cargo.toml entries. This plan page and its nav entry land here.

  2. D2. No code: the two cars' numbers posted on #2302, and the result written into this page’s Status line at close.

  3. craig-cases, first third: D3a’s seams (the trait, the adapters' config, pool(), base_url(), token(role), the census), the three pilot files moved onto the trait, then the request-claim and replay files and the ledger files (about 20 files).

  4. craig-cases, the rest.

  5. craig-placement.

  6. craig-financial.

  7. Close. Posted on #2302: per service, files and tests moved, files left and why; the count of max-threads = 1 groups before and after; the median time of the moved tests before and after from car junit. testing-reference.adoc updated. The plan archived.

Out of scope: the other services; a database-only CI lane; a new ignore reason; `KeyedHarness’s remaining allow-all files (they use an authorization double by design).

Tests

Category Test

Regression (unchanged, must stay green)

Every moved test, with its assertions unchanged. A moved file’s diff is its imports, its harness line, its pool line and deleted cleanup registrations; the merge request states any other change line by line.

Census (new)

craig-test-lib no_inproc_suite_opens_a_shared_service_database, over each of the three services' tests/ trees. It first marks every function whose body calls postgres_test_db_url, then every function that calls a marked one by name, to a fixed point; a file that names an in-process adapter fails when it calls postgres_test_db_url or any marked function. Matching is by function name across the tree, so it over-reports (two helpers of one name are both marked) and never under-reports a named call; a call through a function pointer or a macro is not followed. Red proofs: a pilot file given a direct call fails; a pilot file given a call to a helper that calls cases_pool() fails.

Unit (new)

craig-test-lib: the trait’s provided constructors build the client client_for_role returns for that role (one constructor per role against a recording implementation), so the macro’s two outputs cannot drift.

Census (existing)

no_inproc_suite_uses_an_allow_all_engine, over the larger set of files that now name an adapter.

Contract (existing)

the_test_issuer_mints_the_claims_the_realm_issues (devstack): the adapter’s tokens against the realm’s.

Devstack-gated (car only)

All moved tests. Each step’s car is the proof that they pass in-process; no local full run is claimed.

Measurement

D2 and the close step, from car junit, posted on #2302.

No concurrency test is added: a moved concurrency test still races real connections against one database, now its own.

Risks

Risk Mode Mitigation

A moved test still opens the shared service database, directly or through a shared helper, sees none of its router’s rows, and passes an assertion that expects absence

silent

no_inproc_suite_opens_a_shared_service_database follows helper calls by name to a fixed point and fails the file; every moved file’s pool is the adapter’s. Its limit (a function pointer or macro) is stated in § Tests.

A moved test builds a client of another service and gets a connection error where it used to get an answer

fail-closed

The adapter’s config points every peer at an address that refuses, so the test fails on its car; the file moves back and is listed.

A moved test depended on a seeded row

fail-closed

The test fails on its car: the throwaway database is empty. The file is moved back and listed; no seed is added to the adapter.

A moved test stops covering a cross-service effect it used to cover

silent

D3’s whole-file rule: a file with any test that reads a peer, the broker, a job’s completion or another service’s database stays. Each merge request lists what stayed and why, and the survey’s signals are re-run on the moved set.

The adapter’s method for a client differs from `TestHarness’s (another role or audience)

silent

The adapter builds each client from the same typed client constructor with a token from TestIssuer, whose claims the realm contract test holds to the realm’s.

A test taken out of a serialized group still shares state with a test that stayed

fail-closed

It would fail intermittently on a car. A test leaves a group only when everything the group’s comment names as shared is now in its own database; otherwise it stays in the group.

The optimised crates lengthen the build

fail-closed

D2 reads the car job’s duration on both sides; a build cost above the test saving is reported on #2302 before the port continues.

The moved tests add wall time to every car

fail-closed

Stated up front: about 0.3 s a test after D1 on luchs, so roughly 40 s of wall time for a thousand tests at eight threads. D2 and the close step measure it on the runner.

An optimised hash crate behaves differently from the debug build

fail-closed

The same crates are optimised in every release build; a difference would fail authentication in every devstack test.

Delivery

  • Doc page: docs/modules/ROOT/pages/testing-reference.adoc: which tests run in-process and which stay on the deployed stack, the rule for a new test, and the measured cost.

  • CHANGELOG entry: none. Every step is tests or a dev-profile build setting; changelog.d/README.adoc asks a fragment only for a change a deployment or a client sees.

  • Gates before each push: fmt, clippy and nextest for the touched crates, cargo xtask validate --profile fast.

  • Close: Closes #2302 on the last step; the plan archived; a note with the merges and the measurements.

Edit this page · latest