Task: test data for parallel runs
For parallel runs every test or worker gets its own data, because random failures that pass on rerun usually come from shared state. An example is one user for all workers. The fixes are unique data per test, an account pool per worker, a run prefix on entities, or asserting the change instead of an absolute value. A nightly cleanup by prefix is still needed, because worker teardown is not guaranteed.
System design · Data and environments
64 workers, one staging, and the balance in a test is 100, then 70. Who is changing someone else's data?
The condition: 64 parallel workers, a shared staging, tests of transfers between accounts. Tests fail not always and not the same ones: now one, now another, and pass on a rerun. The cause is neither Playwright instability nor a slow environment but shared state. This lesson shows how to recognise it and how to remove it.
Why it fails. Two tests use the same user test_user_1. Test A transfers 30 from a balance of 100, and the balance becomes 70. Test B at that moment checks that the balance is 100 and sees 70. This is a classic race over shared mutable state: while there are few tests they hardly overlap in time, and with 64 workers they overlap constantly, and randomly.
How two tests break one balance
The diagnostic sign: a test fails with a different result from run to run and passes on a rerun, yet the instability has a concrete cause in the data. That sets it apart from timeouts and waiting for elements.
How it is solved. There are four ways, and they combine:
- Unique data per test. A fixture creates a new user and account before the test and deletes them after. This is the cleanest option.
- An account pool per worker if creation is expensive. A worker takes an account from the pool, and a worker-scoped fixture in Playwright creates it once per worker.
- A run prefix. All entities carry a label, for example
run_8f2c_w3_. Cleaning up and sorting out garbage becomes a search by prefix. - Relative checks rather than absolute when state is shared: remember the balance before the action and compare the difference.
export const test = base.extend<{}, { account: Account }>({
account: [
async ({}, use, workerInfo) => {
const tag = 'run_' + RUN_ID + '_w' + workerInfo.workerIndex;
const acc = await createAccount(tag);
await use(acc);
await closeAccount(acc.id);
},
{ scope: 'worker' }, // once per worker
],
});
The difference between the two indexes is worth knowing. workerInfo.workerIndex is unique for each worker process, and after a test failure Playwright starts a new worker with a new index. workerInfo.parallelIndex lies in the range from 0 to the number of workers minus one and repeats for a new worker that replaced the previous one. For an entity label that must not coincide across processes, workerIndex fits. For a fixed pool of accounts by slot, parallelIndex fits, but then an account may be «dirty» after the previous worker.
If the account is shared within a worker, tests must compare a change rather than an absolute value:
const before = await api.balance(account.id);
await transferPage.send({ to: 'savings', amount: 30 });
await expect.poll(() => api.balance(account.id)).toBe(before - 30);
How many accounts to create
New for every test
Maximum isolation. The price: more API requests, setup time, the database grows.
One per worker
Faster. The price: tests of a worker share the account, so they must clean up or compare the change.
Cleanup and limits. The closing part of a fixture runs when the worker ends normally. If the process is killed (a timeout of the whole job, a machine failure), cleanup does not happen. So a separate nightly «janitor» deletes entities with a run prefix older than a few hours. A per-worker pool does not suit tests that need a clean starting state (the first transfer, a zero balance); they need their own accounts. Relative checks do not suit tests of exact sums and record order.
Trade-offs. New data per test gives maximum isolation but loads the API and grows the database. A pool is faster, but tests within a worker must clean up their state or check the change. Prefixes give manageability at the price of discipline: someone has to watch that every entity has one.
Metrics. The number of failures caused by data (tests that fail and pass on a rerun with no code change), the amount of garbage in the DB after a run, data preparation time in a test as a share of the test time.
Common mistakes.
- Treating the failure with retries: the test passes on a rerun and the cause stays.
- One user per worker, but tests rely on the starting balance: the state depends on test order within the worker.
- A run label without uniqueness (only the worker index): two consecutive runs share entities.
- Having no cleanup for the crash case: garbage accumulates and the shared environment gets cluttered.
- A relative check without waiting: the balance changes asynchronously while the comparison runs now.
Check yourself
Why does a test with a shared test_user_1 fail only sometimes, not always?
Answer
In a parallel run the order and overlap of tests in time are undefined. A failure happens only when test B reads the balance between test A's change and the return of the state. With another arrangement the tests do not overlap and pass. This is a sign of shared state, not an unstable environment.
Why is parallelIndex used for a fixed pool of accounts and workerIndex for an entity label?
Answer
parallelIndex lies in the range from 0 to the number of workers minus one and repeats when a new worker replaces a stopped one, so it fits as a slot number in a pool. workerIndex is unique for each worker process, so a label does not coincide across processes. The drawback of a pool by slot: a new worker inherits the account in the state the previous one left.
parallelIndex and workerIndex. With shared state I compare the change, not the absolute value. I add a nightly cleanup by prefix, because a worker's shutdown is not guaranteed. Metrics: failures caused by data and garbage in the DB.