Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: kill any child processes of server (#3)
* chore(test): Added test case where child process is not closed

* minor(stop): Also kill child processes of the server

* fix(deps): remove unused eslint plugins
  • Loading branch information
dallonf authored and bahmutov committed Mar 14, 2018
1 parent 591e4cd commit ce26532
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
16 changes: 9 additions & 7 deletions package.json
Expand Up @@ -72,9 +72,11 @@
"unused-deps": "dependency-check --unused --no-dev .",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"start": "node test/server.js",
"start-with-child": "node test/server-as-child.js",
"test2": "curl http://127.0.0.1:9000",
"demo": "node bin/start.js http://127.0.0.1:9000",
"demo2": "node bin/start.js start http://127.0.0.1:9000 test2"
"demo2": "node bin/start.js start http://127.0.0.1:9000 test2",
"demo3": "node bin/start.js start-with-child http://127.0.0.1:9000 test"
},
"release": {
"analyzeCommits": "simple-commit-message",
Expand All @@ -85,7 +87,7 @@
}
},
"devDependencies": {
"ban-sensitive-files": "1.9.0",
"ban-sensitive-files": "1.9.2",
"dependency-check": "2.9.1",
"deps-ok": "1.2.1",
"dont-crack": "1.2.1",
Expand All @@ -101,12 +103,12 @@
"standard": "10.0.3"
},
"dependencies": {
"bluebird": "3.5.1",
"check-more-types": "2.24.0",
"lazy-ass": "1.6.0",
"debug": "3.1.0",
"execa": "0.8.0",
"wait-on": "2.0.2",
"bluebird": "3.5.1",
"debug": "3.1.0"
"lazy-ass": "1.6.0",
"ps-tree": "1.1.0",
"wait-on": "2.0.2"
}
}

15 changes: 13 additions & 2 deletions src/index.js
Expand Up @@ -5,6 +5,7 @@ const is = require('check-more-types')
const execa = require('execa')
const waitOn = require('wait-on')
const Promise = require('bluebird')
const psTree = require('ps-tree')
const debug = require('debug')('start-server-and-test')

function startAndTest ({ start, url, test }) {
Expand All @@ -17,10 +18,20 @@ function startAndTest ({ start, url, test }) {
let serverStopped

function stopServer () {
debug('getting child processes')
if (!serverStopped) {
debug('stopping server')
server.kill()
serverStopped = true
return Promise.fromNode(cb => psTree(server.pid, cb))
.then(children => {
debug('stopping child processes')
children.forEach(child => {
process.kill(child.PID)
})
})
.then(() => {
debug('stopping server')
server.kill()
})
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/server-as-child.js
@@ -0,0 +1,9 @@
const childProcess = require('child_process');

console.log('Starting server as child process');
const result = childProcess.spawnSync(
'node',
[].concat(require.resolve('./server')).concat(process.argv),
{ stdio: 'inherit' }
);
console.log('Done');

0 comments on commit ce26532

Please sign in to comment.