0032. Isolate test runs by run token¶
Status: Accepted. Date: 2026-11-26.
Context¶
Several agents work in their own git worktrees at once and run the tests together. The PHPUnit suites share one MariaDB container and keep apart by table prefix, but some runs still clashed:
- Mutation runs in two checkouts used the same prefixes: Infection sets
TEST_TOKENto its worker number, so every checkout's worker 1 usedwptests_1_, and the initial run usedwptests_. - Two runs of one suite in one checkout used one prefix and dropped each other's tables.
make test-distdropped and created a fixed database,wordpress_dist.- Only
make teststarted the database first, so two first runs at once raced on the container name. make downandmake cleanstopped the test database under every running agent, as it was in the project of the local site.
Decision¶
- A run token. The Makefile passes a base,
WPUN_RUN_TOKEN=<checkout token><suite letter>(ssingle site,mmultisite,ccoverage,umutation,dtest-dist,pplugin-check; the known-bug and slowest targets uses).docker/wp-tests-entrypoint.shappends_and a random part of six hex digits and exports the result, so every process of the run sees one value: Infection's workers, WordPress's install script. With no base (a hand-run) the base isx.TEST_TOKENstays Infection's worker number and the Makefile no longer sets it. - One rule for the prefix.
docker/wp-tests-prefix.phpbuildswptests_<run>_<worker>_, orwptests_<run>_without a worker. Each part keeps only[0-9a-z_], so nothing in a token can reach a table name or a query. A worker of0is a worker: only an unset or emptyTEST_TOKENmeans none. The same file makes theLIKEpattern that selects a run's tables, with_escaped, as it is a wildcard. - Drop on exit. The entrypoint runs the command as a child, keeps its exit code, and drops every table of the run (
docker/wp-tests-drop.php) when it ends. A child started with&ignores SIGINT, so on INT or TERM the entrypoint sends SIGTERM to every other process of the container and exits with the signal's code: Ctrl-C still stops a run. A failed drop is reported and never changes the exit code. This covers every PHPUnit target the same way, mutation workers included. A killed run leaves its tables until the database container restarts, as its data is in memory. - Its own database for test-dist and plugin-check.
bin/fresh-wordpress.shcreateswordpress_dist_<run token>and drops it, and its temporary directory, when the script exits. - The test database in its own project.
docker/compose.testdb.ymlis the projectwpun-testdbwith thedbservice and a network of the same name. The rootdocker-compose.ymljoins that network as an external one, anddbis still the host name.make downandmake cleanact on the root project only. - Started by every target that needs it.
make testdbstarts it, and the PHPUnit, coverage, mutation, test-dist and plugin-check targets depend on it. It tries a second time when the first fails: two first runs at once race on the container and network name, and the second try finds them there.
The run token deviates from the proposal in issue #594, where the Makefile passed the random part. It is made in the entrypoint, where the tables are dropped, so the prefix and the pattern for dropping always agree.
Consequences¶
- Good: any two runs, in any checkouts, of any suites, use different tables and databases.
- Good: no test tables pile up, and no agent has to start the database first.
- Good:
make downandmake cleanno longer stop another agent's tests. - Bad: the database container of an older checkout (
<project>-db-1) stays until someone removes it.make cleanremoves it as an orphan. - Bad: the entrypoint is now a parent process of the run, so a run killed with SIGKILL leaves its tables.
- Bad: a second compose file and an external network to know about.
Considered options¶
- One database per run. Rejected: WordPress's test bootstrap and the shared container make a table prefix the simpler isolation, and creating databases would need a privilege step in every run.
- A random part from the Makefile. Rejected: the Makefile and the entrypoint would each need to know it, and the tables dropped could differ from those made.
- A container per run. Rejected: slow to start and heavy with many agents.
References¶
- Issue #594: isolate PHPUnit runs across worktrees.