Fix last bug in kill. Add better test to catch them.
Some checks failed
continuous-integration/appveyor/branch AppVeyor build failed

This commit is contained in:
TheThing 2023-10-31 13:45:30 +00:00
parent 8fad1b45b1
commit ceebb3f88f
4 changed files with 25 additions and 7 deletions

View file

@ -4,12 +4,12 @@ import { spawn, exec } from 'child_process'
const execPromise = promisify(exec) const execPromise = promisify(exec)
export default function kill(pid, signal) { export default function kill(pid, signal) {
let pids = new Set() let pids = new Set([pid])
let getSpawn = null let getSpawn = null
switch (process.platform) { switch (process.platform) {
case 'win32': case 'win32':
return execPromise('taskkill /pid ' + pid + ' /T /F') return execPromise('taskkill /pid ' + pid + ' /T /F').then(() => pids)
case 'darwin': case 'darwin':
getSpawn = function(parentPid) { getSpawn = function(parentPid) {
return spawn('pgrep', ['-P', parentPid]) return spawn('pgrep', ['-P', parentPid])
@ -31,6 +31,7 @@ export default function kill(pid, signal) {
if (err.code !== 'ESRCH') throw err; if (err.code !== 'ESRCH') throw err;
} }
} }
return pids
}) })
} }
@ -49,9 +50,9 @@ function buildTree(allPids, spawnGetChildren, parentPid) {
if (code !== 0) return res() if (code !== 0) return res()
res(Promise.all( res(Promise.all(
allData.match(/\d+/g) data.match(/\d+/g)
.map(Number) .map(Number)
.filter(pid => !bla.has(pid)) .filter(pid => !allPids.has(pid))
.map(buildTree.bind(this, allPids, spawnGetChildren)) .map(buildTree.bind(this, allPids, spawnGetChildren))
)) ))
}) })

View file

@ -1,4 +1,4 @@
import { fork } from 'child_process' import { spawn } from 'child_process'
import t from '../../lib/eltro.mjs' import t from '../../lib/eltro.mjs'
import assert from '../../lib/assert.mjs' import assert from '../../lib/assert.mjs'
import kill from '../../lib/kill.mjs' import kill from '../../lib/kill.mjs'
@ -13,7 +13,7 @@ t.describe('kill', function() {
}) })
t.test('should kill process correctly', function(done) { t.test('should kill process correctly', function(done) {
worker = fork('./test/') worker = spawn('node', ['./test/kill/runner.mjs'])
assert.ok(worker.pid) assert.ok(worker.pid)
worker.on('exit', done.finish(function(code, signal) { worker.on('exit', done.finish(function(code, signal) {
@ -22,4 +22,16 @@ t.describe('kill', function() {
kill(worker.pid) 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)
})
})
}) })

View file

@ -1 +1,5 @@
setInterval(function() {}, 1000) import { spawn } from 'child_process'
spawn('node', ['./test/kill/second_runner.mjs'])
setInterval(function() { }, 100)

View file

@ -0,0 +1 @@
setInterval(function() { }, 100)