From e7241370117e25815b670a7c39ee460b61cdae20 Mon Sep 17 00:00:00 2001 From: Tim Koopman Date: Tue, 9 Mar 2021 11:32:16 +0800 Subject: [PATCH 1/3] Allow specifying time-outs in seconds/minutes/hours --- bin/usage.txt | 7 +++++++ bin/wait-on | 24 ++++++++++++++++++++++-- test/cli.mocha.js | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/bin/usage.txt b/bin/usage.txt index 11fc76e..8c36757 100644 --- a/bin/usage.txt +++ b/bin/usage.txt @@ -68,10 +68,17 @@ Standard Options: Maximum time in ms to wait before exiting with failure (1) code, default Infinity + Use postfix 'ms', 's', 'm' or 'h' to change the unit. --tcpTimeout Maximum time in ms for tcp connect, default 300ms + Use postfix 'ms', 's', 'm' or 'h' to change the unit. + + --httpTimeout + + Maximum time to wait for the HTTP request, default Infinity + Use postfix 'ms', 's', 'm' or 'h' to change the unit. -v, --verbose diff --git a/bin/wait-on b/bin/wait-on index 578f37b..a9ed46f 100755 --- a/bin/wait-on +++ b/bin/wait-on @@ -5,8 +5,9 @@ const minimist = require('minimist'); const path = require('path'); const waitOn = require('../'); +const interval = ['timeout', 'httpTimeout', 'tcpTimeout']; const minimistOpts = { - string: ['c', 'd', 'i', 's', 't', 'w', 'httpTimeout', 'tcpTimeout'], + string: ['c', 'd', 'i', 's', 't', 'w'].concat(interval), boolean: ['h', 'l', 'r', 'v'], alias: { c: 'config', @@ -57,7 +58,11 @@ if (argv.help || !argv._.length) { 'window', ].reduce(function (accum, x) { if (argv[x]) { - accum[x] = argv[x]; + let value = argv[x]; + if (interval.includes(x)) { + value = parseInterval(value); + } + accum[x] = value; } return accum; }, opts); @@ -81,3 +86,18 @@ function errorExit(err) { } process.exit(1); } + +function parseInterval(arg) { + const res = /^([\d.]+)(|ms|s|m|h)$/i.exec(arg); + if (!res) { + return arg; + } + const value = parseFloat(res[1]); + switch (res[2]) { + case '': + case 'ms': return Math.floor(value); + case 's': return Math.floor(value * 1000); + case 'm': return Math.floor(value * 1000 * 60); + case 'h': return Math.floor(value * 1000 * 60 * 60); + } +} diff --git a/test/cli.mocha.js b/test/cli.mocha.js index b327438..993f877 100644 --- a/test/cli.mocha.js +++ b/test/cli.mocha.js @@ -22,7 +22,7 @@ function execCLI(args, options) { return childProcess.spawn(process.execPath, fullArgs, options); } -const FAST_OPTS = '-t 1000 -i 100 -w 100'.split(' '); +const FAST_OPTS = '-t 1s -i 100 -w 100'.split(' '); describe('cli', function () { this.timeout(3000); From 5dab9878e7bb62c1d284e42778da5ec541d0dde6 Mon Sep 17 00:00:00 2001 From: Jeff Barczewski Date: Thu, 2 Nov 2023 19:15:36 -0500 Subject: [PATCH 2/3] add to README about timeout, tcpTimeout, httpTimeout allowing unit If no unit it specified defaults to ms. Allows units: ms,s,m,h --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 68cba09..1d05a15 100644 --- a/README.md +++ b/README.md @@ -121,10 +121,17 @@ Standard Options: Maximum time in ms to wait before exiting with failure (1) code, default Infinity + Use postfix 'ms', 's', 'm' or 'h' to change the unit. --tcpTimeout - Maximum time in ms for tcp connect, default 300ms + Maximum time in ms for tcp connect, default 300ms + Use postfix 'ms', 's', 'm' or 'h' to change the unit. + + --httpTimeout + + Maximum time to wait for the HTTP request, default Infinity + Use postfix 'ms', 's', 'm' or 'h' to change the unit. -v, --verbose From 3caa77c711fb9bff8170e9e64b4840a3195b1762 Mon Sep 17 00:00:00 2001 From: Jeff Barczewski Date: Thu, 2 Nov 2023 19:17:22 -0500 Subject: [PATCH 3/3] add additional unit tests --- test/cli.mocha.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/test/cli.mocha.js b/test/cli.mocha.js index e04eb30..c07353e 100644 --- a/test/cli.mocha.js +++ b/test/cli.mocha.js @@ -22,7 +22,10 @@ function execCLI(args, options) { return childProcess.spawn(process.execPath, fullArgs, options); } -const FAST_OPTS = '-t 1s -i 100 -w 100'.split(' '); + +const FAST_OPTS = '-t 1000 -i 100 -w 100'.split(' '); +const FAST_OPTS_TIMEOUT_UNIT = '-t 1s -i 100 -w 100'.split(' '); + describe('cli', function () { this.timeout(3000); @@ -271,6 +274,22 @@ describe('cli', function () { }); }); + it('should timeout when all resources are not available and timout option is specified with unit', function (done) { + temp.mkdir({}, function (err, dirPath) { + if (err) return done(err); + const opts = { + resources: [path.resolve(dirPath, 'foo')], + timeout: '1s' + }; + // timeout is in FAST_OPTS + execCLI(opts.resources.concat(FAST_OPTS_TIMEOUT_UNIT), {}).on('exit', function (code) { + expect(code).toNotBe(0); + done(); + }); + }); + }); + + it('should timeout when some resources are not available and timout option is specified', function (done) { temp.mkdir({}, function (err, dirPath) { if (err) return done(err); @@ -349,6 +368,31 @@ describe('cli', function () { }); }); + it('should timeout when an http resource does not respond before httpTimeout specified with unit', function (done) { + const opts = { + resources: ['http://localhost:8125'], + timeout: 1000, + interval: 100, + window: 100, + httpTimeout: '70ms' + }; + + httpServer = http.createServer().on('request', function (req, res) { + // make it a slow response, longer than the httpTimeout + setTimeout(function () { + res.end('data'); + }, 90); + }); + httpServer.listen(8125, 'localhost'); + + const addOpts = '--httpTimeout 70ms'.split(' '); + // timeout, interval, and window are in FAST_OPTS + execCLI(opts.resources.concat(FAST_OPTS).concat(addOpts), {}).on('exit', function (code) { + expect(code).toNotBe(0); + done(); + }); + }); + it('should timeout when an http GET resource is not available', function (done) { const opts = { resources: ['http-get://localhost:3999'], @@ -422,6 +466,21 @@ describe('cli', function () { }); }); + it('should timeout when a service host is unreachable, tcpTimeout specified with unit', function (done) { + const opts = { + resources: ['tcp:256.0.0.1:1234'], + timeout: 1000, + tcpTimeout: '1s' + }; + + const addOpts = '--tcpTimeout 1s'.split(' '); + // timeout is in FAST_OPTS + execCLI(opts.resources.concat(FAST_OPTS).concat(addOpts), {}).on('exit', function (code) { + expect(code).toNotBe(0); + done(); + }); + }); + it('should timeout when a service is not listening to a socket', function (done) { let socketPath; temp.mkdir({}, function (err, dirPath) {