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
5ae61a62e9
4 changed files with 36 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,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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -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)
|
2
test/kill/second_runner.mjs
Normal file
2
test/kill/second_runner.mjs
Normal file
|
@ -0,0 +1,2 @@
|
|||
console.log('secondary')
|
||||
setInterval(function() { console.log('secondary') }, 100)
|
Loading…
Reference in a new issue