Skip to content

Commit

Permalink
Merge branch 'ntkoopman-interval'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffbski committed Nov 3, 2023
2 parents c830235 + 3caa77c commit b9ec579
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions bin/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 22 additions & 2 deletions bin/wait-on
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -55,7 +56,11 @@ if (argv.help || !hasResources) {
'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;
}, configOpts);
Expand All @@ -79,3 +84,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);
}
}
61 changes: 60 additions & 1 deletion test/cli.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ 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 1000 -i 100 -w 100'.split(' ');
const FAST_OPTS_TIMEOUT_UNIT = '-t 1s -i 100 -w 100'.split(' ');


describe('cli', function () {
this.timeout(3000);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b9ec579

Please sign in to comment.