Files
wekan/tests/playwright/helpers/sharedSettingLock.js
T
Lauri Ojansivu a4d2313a68 Password policy specs no longer run side by side or leave a minimum enforced.
In the full Chromium run (two workers) password-length-report.e2e.js and
password-min-length-signup-login.e2e.js ran at the same time. Both write
settings.loginOptions.passwordMinLength and passwordMinLengthEnforce, one
document for every worker, so each turned enforcement on or off under the
other and one test of each failed. Both also restored the loginOptions they
had seen before each test, so once a test was interrupted the snapshot
already held an enforced minimum, and every later test put it back: the
test database was left with a minimum of 10 enforced.

tests/playwright/helpers/sharedSettingLock.js is a lock across workers (an
atomic mkdir under TMPDIR, counted per process, and taken over from a
worker that died); both specs hold 'password-policy' for the whole file,
and put the policy back to its default with $unset after each test. Run
together on two workers both files pass, 6 of 6.
tests/playwrightSharedSettingLock.test.cjs requires the lock and the
$unset in every spec that writes the policy, refuses a restored snapshot,
and drives the lock: shared within a process, released by the last holder,
taken over from a dead one, and waited for from a live one.

Thanks to xet7 !
2026-11-06 22:12:49 +02:00

96 lines
3.5 KiB
JavaScript

'use strict';
// A lock for spec files that change the SAME site-wide setting.
//
// Playwright runs the tests of one file in order (fullyParallel: false) but
// two files at once with two workers. A site-wide setting is one document for
// every worker, so two files that write it undo each other:
// password-length-report.e2e.js and password-min-length-signup-login.e2e.js
// both set, and after every test restore, `loginOptions` (the password
// policy). In the full Chromium run they ran side by side and one test of
// each failed - the other file had switched enforcement back on, or off.
//
// holdSharedSetting(test, 'password-policy') at the top of each such file
// makes it hold the lock for all its tests: the second file waits until the
// first is finished. The lock is a directory - mkdir is atomic - under
// TMPDIR, which build.sh sets to .tools/tmp for every worker of a run, and
// holds the owning process id, so a lock left by a worker that died is taken
// over instead of blocking the run.
const fs = require('fs');
const os = require('os');
const path = require('path');
const WAIT_MS = 15 * 60 * 1000;
function lockDir(name) {
if (!/^[a-z0-9-]+$/.test(name)) throw new Error(`shared setting lock name must be kebab-case: ${name}`);
return path.join(process.env.TMPDIR || os.tmpdir(), `wekan-e2e-setting-lock-${name}`);
}
function alive(pid) {
if (!pid) return true; // being written: treat as held for now
try { process.kill(pid, 0); return true; } catch (e) { return e.code === 'EPERM'; }
}
// How many holders this process has per name: one worker can run the same
// file again (--repeat-each, a retry) before the earlier run's afterAll, and
// the lock must stay taken until the LAST holder releases it.
const held = new Map();
async function acquire(name, opts = {}) {
const count = held.get(name) || 0;
if (count > 0) { held.set(name, count + 1); return releaser(name); }
await take(name, opts);
held.set(name, 1);
return releaser(name);
}
function releaser(name) {
let done = false;
return () => {
if (done) return;
done = true;
const count = (held.get(name) || 1) - 1;
if (count > 0) { held.set(name, count); return; }
held.delete(name);
const dir = lockDir(name);
try {
if (Number(fs.readFileSync(path.join(dir, 'pid'), 'utf8')) === process.pid) fs.rmSync(dir, { recursive: true, force: true });
} catch (e) { /* already gone */ }
};
}
async function take(name, { waitMs = WAIT_MS } = {}) {
const dir = lockDir(name);
const owner = path.join(dir, 'pid');
const started = Date.now();
for (;;) {
try {
fs.mkdirSync(dir);
fs.writeFileSync(owner, String(process.pid));
return;
} catch (e) {
if (e.code !== 'EEXIST') throw e;
let pid = 0;
try { pid = Number(fs.readFileSync(owner, 'utf8')) || 0; } catch (_) { /* not written yet */ }
if (pid === process.pid) return; // left by this process
if (!alive(pid)) { fs.rmSync(dir, { recursive: true, force: true }); continue; }
if (Date.now() - started > waitMs) throw new Error(`shared setting "${name}" still held by process ${pid}`);
await new Promise(resolve => setTimeout(resolve, 250));
}
}
}
function holdSharedSetting(test, name) {
let release = null;
test.beforeAll(async ({}, testInfo) => {
testInfo.setTimeout(WAIT_MS + 60 * 1000);
release = await acquire(name);
});
test.afterAll(() => {
if (release) release();
release = null;
});
}
module.exports = { holdSharedSetting, acquire };