Skip to content

Commit

Permalink
FEAT: Allow interval option from environment variable (#315)
Browse files Browse the repository at this point in the history
* Allow interval option from environment variable

* feat(test): add new demo for testing, update README
  • Loading branch information
cpow committed Sep 2, 2021
1 parent 7ac144f commit 693dc2c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -279,6 +279,10 @@ To disable HTTPS checks for `wait-on`, run with environment variable `START_SERV

This utility will wait for maximum of 5 minutes while checking for the server to respond (default). Setting an environment variable `WAIT_ON_TIMEOUT=600000` (milliseconds) sets the timeout for example to 10 minutes.

### Interval

This utility will check for a server response every two seconds (default). Setting an environment variable `WAIT_ON_INTERVAL=600000` (milliseconds) sets the interval for example to 10 minutes.

### Starting two servers

Sometimes you need to start one API server and one webserver in order to test the application. Use the syntax:
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -92,6 +92,8 @@
"demo10": "node src/bin/start.js start-fail http://127.0.0.1:9000 test",
"demo11": "node src/bin/start.js http-get://127.0.0.1:9000",
"demo12": "node src/bin/start.js start-304 9000 test2",
"demo-interval": "WAIT_ON_INTERVAL=1000 node src/bin/start.js start http://127.0.0.1:9000 test2",
"demo-timeout": "WAIT_ON_TIMEOUT=10000 node src/bin/start.js start http://127.0.0.1:9000 test2",
"demo-cross-env": "node src/bin/start.js start-cross-env 9000",
"demo-commands": "node src/bin/start.js 'node test/server.js --port 8800' 8800 'node test/client --port 8800'",
"demo-multiple": "node src/bin/start.js 'node test/server --port 6000' 6000 'node test/server --port 6010' 6010 'curl http://127.0.0.1:6000 && curl http://127.0.0.1:6010'",
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Expand Up @@ -13,10 +13,16 @@ const debug = require('debug')('start-server-and-test')
* Used for timeout (ms)
*/
const fiveMinutes = 5 * 60 * 1000
const twoSeconds = 2000

const waitOnTimeout = process.env.WAIT_ON_TIMEOUT
? Number(process.env.WAIT_ON_TIMEOUT)
: fiveMinutes

const waitOnInterval = process.env.WAIT_ON_INTERVAL
? Number(process.env.WAIT_ON_INTERVAL)
: twoSeconds

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

Expand Down Expand Up @@ -74,7 +80,7 @@ function waitAndRun ({ start, url, runFn }) {
debug('starting waitOn %s', url)
const options = {
resources: Array.isArray(url) ? url : [url],
interval: 2000,
interval: waitOnInterval,
window: 1000,
timeout: waitOnTimeout,
verbose: isDebug(),
Expand Down
8 changes: 8 additions & 0 deletions src/utils.js
Expand Up @@ -199,6 +199,14 @@ function printArguments ({ services, test }) {
)
})

if (process.env.WAIT_ON_INTERVAL !== undefined) {
console.log('WAIT_ON_INTERVAL is set to', process.env.WAIT_ON_INTERVAL)
}

if (process.env.WAIT_ON_TIMEOUT !== undefined) {
console.log('WAIT_ON_TIMEOUT is set to', process.env.WAIT_ON_TIMEOUT)
}

console.log('running tests using command "%s"', test)
console.log('')
}
Expand Down

0 comments on commit 693dc2c

Please sign in to comment.