Fix last bug in kill. Add better test to catch them.
Some checks failed
continuous-integration/appveyor/branch AppVeyor build failed
Some checks failed
continuous-integration/appveyor/branch AppVeyor build failed
This commit is contained in:
parent
8fad1b45b1
commit
ceebb3f88f
4 changed files with 25 additions and 7 deletions
|
@ -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))
|
||||
))
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1 +1,5 @@
|
|||
setInterval(function() {}, 1000)
|
||||
import { spawn } from 'child_process'
|
||||
|
||||
spawn('node', ['./test/kill/second_runner.mjs'])
|
||||
|
||||
setInterval(function() { }, 100)
|
1
test/kill/second_runner.mjs
Normal file
1
test/kill/second_runner.mjs
Normal file
|
@ -0,0 +1 @@
|
|||
setInterval(function() { }, 100)
|
Loading…
Reference in a new issue