From ceebb3f88fcc91b2fd0666ce47850fdd71c675bb Mon Sep 17 00:00:00 2001 From: TheThing Date: Tue, 31 Oct 2023 13:45:30 +0000 Subject: [PATCH] Fix last bug in kill. Add better test to catch them. --- lib/kill.mjs | 9 +++++---- test/kill/kill.test.mjs | 16 ++++++++++++++-- test/kill/runner.mjs | 6 +++++- test/kill/second_runner.mjs | 1 + 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 test/kill/second_runner.mjs diff --git a/lib/kill.mjs b/lib/kill.mjs index b91e6fe..114c604 100644 --- a/lib/kill.mjs +++ b/lib/kill.mjs @@ -4,12 +4,12 @@ import { spawn, exec } from 'child_process' const execPromise = promisify(exec) export default function kill(pid, signal) { - let pids = new Set() + let pids = new Set([pid]) let getSpawn = null switch (process.platform) { case 'win32': - return execPromise('taskkill /pid ' + pid + ' /T /F') + return execPromise('taskkill /pid ' + pid + ' /T /F').then(() => pids) case 'darwin': getSpawn = function(parentPid) { return spawn('pgrep', ['-P', parentPid]) @@ -31,6 +31,7 @@ export default function kill(pid, signal) { if (err.code !== 'ESRCH') throw err; } } + return pids }) } @@ -49,9 +50,9 @@ function buildTree(allPids, spawnGetChildren, parentPid) { if (code !== 0) return res() res(Promise.all( - allData.match(/\d+/g) + data.match(/\d+/g) .map(Number) - .filter(pid => !bla.has(pid)) + .filter(pid => !allPids.has(pid)) .map(buildTree.bind(this, allPids, spawnGetChildren)) )) }) diff --git a/test/kill/kill.test.mjs b/test/kill/kill.test.mjs index 9a967f0..d14a813 100644 --- a/test/kill/kill.test.mjs +++ b/test/kill/kill.test.mjs @@ -1,4 +1,4 @@ -import { fork } from 'child_process' +import { spawn } from 'child_process' import t from '../../lib/eltro.mjs' import assert from '../../lib/assert.mjs' import kill from '../../lib/kill.mjs' @@ -13,7 +13,7 @@ t.describe('kill', function() { }) t.test('should kill process correctly', function(done) { - worker = fork('./test/') + worker = spawn('node', ['./test/kill/runner.mjs']) assert.ok(worker.pid) worker.on('exit', done.finish(function(code, signal) { @@ -22,4 +22,16 @@ t.describe('kill', function() { kill(worker.pid) }) + + t.test('should succeed in killing tree', async function() { + worker = spawn('node', ['./test/kill/runner.mjs']) + assert.ok(worker.pid) + + // Give it some time to start + await new Promise(res => setTimeout(res, 1000)) + + return kill(worker.pid).then(function(pids) { + assert.strictEqual(pids.size, 2) + }) + }) }) diff --git a/test/kill/runner.mjs b/test/kill/runner.mjs index 2d16889..82b4e9e 100644 --- a/test/kill/runner.mjs +++ b/test/kill/runner.mjs @@ -1 +1,5 @@ -setInterval(function() {}, 1000) \ No newline at end of file +import { spawn } from 'child_process' + +spawn('node', ['./test/kill/second_runner.mjs']) + +setInterval(function() { }, 100) \ No newline at end of file diff --git a/test/kill/second_runner.mjs b/test/kill/second_runner.mjs new file mode 100644 index 0000000..5bb1c71 --- /dev/null +++ b/test/kill/second_runner.mjs @@ -0,0 +1 @@ +setInterval(function() { }, 100) \ No newline at end of file