Files
compras/scripts/standalone-configuration-race.mjs
T
nala 30bfb1a773
tests / ci (push) Canceled after 0s
commit compras3 2911
2026-11-29 16:03:11 +02:00

51 lines
3.4 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { spawn, spawnSync } from 'node:child_process';
export async function verifyConfigurationConcurrency(workspace, root, database, environment) {
if (environment.COMPRAS_RUNTIME_ENABLED !== 'true' || environment.COMPRAS_RUNTIME_PREPARE_ONLY !== 'false') throw Error('Verified standalone configuration required');
const worker = (mode, round) => {
const result = spawnSync('php', ['tests/Support/standalone-runtime-worker.php', database, mode, round], {
cwd: workspace, windowsHide: true, encoding: 'utf8', env: environment,
});
if (result.status !== 0) throw Error('Standalone configuration fixture/read worker failed');
return JSON.parse(result.stdout.trim());
};
for (const round of ['same', 'different']) {
if (worker('prepare-configuration-race', round).prepared !== true) throw Error('Race fixture not prepared');
const children = [0, 1].map(index => {
const child = spawn('php', ['tests/Support/standalone-runtime-worker.php', database, 'run-configuration-race', round, String(index)], {
cwd: workspace, windowsHide: true, env: environment,
});
let output = '';
child.stdout.on('data', chunk => { output += chunk.toString(); });
child.stderr.on('data', () => {});
const complete = new Promise(resolve => child.on('close', code => resolve({ code, output })));
return { child, complete };
});
try {
const until = Date.now() + 15000;
while (![0, 1].every(index => fs.existsSync(path.join(root, `configuration-${round}-${index}.ready`)))) {
if (Date.now() > until || children.some(({ child }) => child.exitCode !== null)) throw Error('Concurrent candidate workers did not reach the gate');
await new Promise(resolve => setTimeout(resolve, 20));
}
fs.writeFileSync(path.join(root, `configuration-${round}.go`), 'go', { flag: 'wx' });
const results = await Promise.race([
Promise.all(children.map(({ complete }) => complete)),
new Promise((_, reject) => { const timer = setTimeout(() => reject(Error('Concurrent candidate writers timed out')), 20000); timer.unref(); }),
]);
if (results.some(result => result.code !== 0)) throw Error('Concurrent candidate writer failed');
const outcomes = results.map(result => JSON.parse(result.output.trim()));
if (outcomes.filter(result => result.state === 'saved' && result.replayed === false).length !== 1
|| (round === 'same' ? outcomes.filter(result => result.state === 'saved' && result.replayed === true).length !== 1
: outcomes.filter(result => result.state === 'stale').length !== 1)) throw Error('Unexpected concurrent configuration outcome');
const proof = worker('read-configuration-race', round);
if (proof.verified !== true) throw Error('Concurrent configuration conservation missing');
console.log(JSON.stringify({ standalone_configuration_concurrency: true, ...proof, outcomes }));
} finally {
for (const { child } of children) if (child.exitCode === null) child.kill();
await Promise.all(children.map(({ complete }) => complete));
}
}
}