Skip to content

Commit

Permalink
feat: add wait-on timeout of 5 minutes for the server to respond
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 23, 2019
1 parent 2979b15 commit aada298
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ To see diagnostic messages, run with environment variable `DEBUG=start-server-an

To see disable HTTPS checks for `wait-on`, run with environment variable `START_SERVER_AND_TEST_INSECURE=1`.

### Timeout

This utility will wait for maximum of 5 minutes while checking for the server to respond.

### Starting two servers

Sometimes you need to start one API server and one webserver in order to test the application. Just have two commands cascade. First command should wait on the webserver script, which in turn uses `start-server-and-test` to start the API server before running the webserver. Something like this
Expand Down
44 changes: 25 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const Promise = require('bluebird')
const psTree = require('ps-tree')
const debug = require('debug')('start-server-and-test')

/**
* Used for timeout (ms)
*/
const fiveMinutes = 5 * 60 * 1000

const isDebug = () =>
process.env.DEBUG && process.env.DEBUG.indexOf('start-server-and-test') !== -1

Expand Down Expand Up @@ -63,26 +68,27 @@ function startAndTest ({ start, url, test }) {
server.on('close', onClose)

debug('starting waitOn %s', url)
waitOn(
{
resources: Array.isArray(url) ? url : [url],
interval: 2000,
window: 1000,
verbose: isDebug(),
strictSSL: !isInsecure(),
log: isDebug()
},
err => {
if (err) {
debug('error waiting for url', url)
debug(err.message)
return reject(err)
}
debug('waitOn finished successfully')
server.removeListener('close', onClose)
resolve()
const options = {
resources: Array.isArray(url) ? url : [url],
interval: 2000,
window: 1000,
timeout: fiveMinutes,
verbose: isDebug(),
strictSSL: !isInsecure(),
log: isDebug()
}
debug('wait-on options %o', options)

waitOn(options, err => {
if (err) {
debug('error waiting for url', url)
debug(err.message)
return reject(err)
}
)
debug('waitOn finished successfully')
server.removeListener('close', onClose)
resolve()
})
})

function runTests () {
Expand Down

0 comments on commit aada298

Please sign in to comment.