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

TheThing 2023-10-31 14:03:34 +00:00
parent 8fad1b45b1
commit 5ae61a62e9
4 changed files with 36 additions and 7 deletions

View File

@ -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))
))
})

View File

@ -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,20 @@ 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 => {
worker.stdout.on('data', function(data) {
if (data.toString().indexOf('primary') >= 0) res()
})
})
return kill(worker.pid).then(function(pids) {
assert.strictEqual(pids.size, 2)
})
})
})

View File

@ -1 +1,11 @@
setInterval(function() {}, 1000)
import { spawn } from 'child_process'
let secondary = spawn('node', ['./test/kill/second_runner.mjs'])
secondary.stdout.on('data', function(data) {
process.stdout.write(data)
})
console.log('primary')
setInterval(function() { console.log('primary') }, 100)

View File

@ -0,0 +1,2 @@
console.log('secondary')
setInterval(function() { console.log('secondary') }, 100)