diff --git a/tests/worker.test.js b/tests/worker.test.js index 235139a..443f912 100644 --- a/tests/worker.test.js +++ b/tests/worker.test.js @@ -126,7 +126,9 @@ describe('cancel_command (#8)', () => { test('cancelling stops the command and everything it started', async () => { const w = makeWorker(); const pidFile = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'pulse-cancel-')), 'pid'); - const pending = w.executeCommand({ command: `sleep 30 & echo $! > ${pidFile}; wait`, execution_id: 'ex-c', command_id: 'cmd-c' }); + // Write then rename: the shell creates a redirect target before writing to it, + // so reading ${pidFile} directly could see an empty file (pid 0). + const pending = w.executeCommand({ command: `sleep 30 & echo $! > ${pidFile}.tmp && mv ${pidFile}.tmp ${pidFile}; wait`, execution_id: 'ex-c', command_id: 'cmd-c' }); for (let i = 0; i < 50 && !fs.existsSync(pidFile); i++) await new Promise(r => setTimeout(r, 50)); const childPid = Number(fs.readFileSync(pidFile, 'utf8')); expect(alive(childPid)).toBe(true);