Skip to content

Commit

Permalink
fix: Catch the exit of the server process (#104)
Browse files Browse the repository at this point in the history
Fail immediately if the server cannot be started because the
server process failed to start

Close #89
  • Loading branch information
idmontie authored and bahmutov committed Oct 2, 2018
1 parent 0e2735a commit 8cbc535
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"start-with-child": "node test/server-as-child.js",
"start-multiple": "node test/multiple-servers.js",
"start-https": "node test/https-server.js",
"start-fail": "node test/server-fail.js",
"test2": "curl http://127.0.0.1:9000",
"test3": "curl http://127.0.0.1:9000 && curl http://127.0.0.1:9001",
"test4": "curl --insecure https://127.0.0.1:9000",
Expand All @@ -87,6 +88,7 @@
"demo7": "node bin/start.js :9000 test2",
"demo8": "node bin/start.js start-multiple \":9000|:9001\" test3",
"demo9": "node bin/start.js start-https \"https://127.0.0.1:9000\" test4",
"demo10": "node bin/start.js start-fail http://127.0.0.1:9000 test",
"travis-deploy-once": "travis-deploy-once"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ function startAndTest ({ start, url, test }) {
}

const waited = new Promise((resolve, reject) => {
const onClose = () => {
reject(new Error('server closed unexpectedly'))
}

server.on('close', onClose)

debug('starting waitOn %s', url)
waitOn(
{
Expand All @@ -63,6 +69,7 @@ function startAndTest ({ start, url, test }) {
return reject(err)
}
debug('waitOn finished successfully')
server.removeListener('close', onClose)
resolve()
}
)
Expand Down
13 changes: 13 additions & 0 deletions test/server-fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const http = require('http');
const server = http.createServer((req, res) => {
console.log(req.method)
if (req.method === 'GET') {
res.end('All good\n\n')
} else {
res.end();
}
});
setTimeout(() => {
process.exit(1)
}, 5000)
console.log('sleeping for 5 seconds before starting')

0 comments on commit 8cbc535

Please sign in to comment.