Skip to content

Commit b9ec579

Browse files
committed
Merge branch 'ntkoopman-interval'
2 parents c830235 + 3caa77c commit b9ec579

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,17 @@ Standard Options:
121121
122122
Maximum time in ms to wait before exiting with failure (1) code,
123123
default Infinity
124+
Use postfix 'ms', 's', 'm' or 'h' to change the unit.
124125
125126
--tcpTimeout
126127
127-
Maximum time in ms for tcp connect, default 300ms
128+
Maximum time in ms for tcp connect, default 300ms
129+
Use postfix 'ms', 's', 'm' or 'h' to change the unit.
130+
131+
--httpTimeout
132+
133+
Maximum time to wait for the HTTP request, default Infinity
134+
Use postfix 'ms', 's', 'm' or 'h' to change the unit.
128135
129136
-v, --verbose
130137

bin/usage.txt

+7
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,17 @@ Standard Options:
6868

6969
Maximum time in ms to wait before exiting with failure (1) code,
7070
default Infinity
71+
Use postfix 'ms', 's', 'm' or 'h' to change the unit.
7172

7273
--tcpTimeout
7374

7475
Maximum time in ms for tcp connect, default 300ms
76+
Use postfix 'ms', 's', 'm' or 'h' to change the unit.
77+
78+
--httpTimeout
79+
80+
Maximum time to wait for the HTTP request, default Infinity
81+
Use postfix 'ms', 's', 'm' or 'h' to change the unit.
7582

7683
-v, --verbose
7784

bin/wait-on

+22-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ const minimist = require('minimist');
55
const path = require('path');
66
const waitOn = require('../');
77

8+
const interval = ['timeout', 'httpTimeout', 'tcpTimeout'];
89
const minimistOpts = {
9-
string: ['c', 'd', 'i', 's', 't', 'w', 'httpTimeout', 'tcpTimeout'],
10+
string: ['c', 'd', 'i', 's', 't', 'w'].concat(interval),
1011
boolean: ['h', 'l', 'r', 'v'],
1112
alias: {
1213
c: 'config',
@@ -55,7 +56,11 @@ if (argv.help || !hasResources) {
5556
'window'
5657
].reduce(function (accum, x) {
5758
if (argv[x]) {
58-
accum[x] = argv[x];
59+
let value = argv[x];
60+
if (interval.includes(x)) {
61+
value = parseInterval(value);
62+
}
63+
accum[x] = value;
5964
}
6065
return accum;
6166
}, configOpts);
@@ -79,3 +84,18 @@ function errorExit(err) {
7984
}
8085
process.exit(1);
8186
}
87+
88+
function parseInterval(arg) {
89+
const res = /^([\d.]+)(|ms|s|m|h)$/i.exec(arg);
90+
if (!res) {
91+
return arg;
92+
}
93+
const value = parseFloat(res[1]);
94+
switch (res[2]) {
95+
case '':
96+
case 'ms': return Math.floor(value);
97+
case 's': return Math.floor(value * 1000);
98+
case 'm': return Math.floor(value * 1000 * 60);
99+
case 'h': return Math.floor(value * 1000 * 60 * 60);
100+
}
101+
}

test/cli.mocha.js

+60-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ function execCLI(args, options) {
2222
return childProcess.spawn(process.execPath, fullArgs, options);
2323
}
2424

25-
const FAST_OPTS = '-t 1000 -i 100 -w 100'.split(' ');
25+
26+
const FAST_OPTS = '-t 1000 -i 100 -w 100'.split(' ');
27+
const FAST_OPTS_TIMEOUT_UNIT = '-t 1s -i 100 -w 100'.split(' ');
28+
2629

2730
describe('cli', function () {
2831
this.timeout(3000);
@@ -271,6 +274,22 @@ describe('cli', function () {
271274
});
272275
});
273276

277+
it('should timeout when all resources are not available and timout option is specified with unit', function (done) {
278+
temp.mkdir({}, function (err, dirPath) {
279+
if (err) return done(err);
280+
const opts = {
281+
resources: [path.resolve(dirPath, 'foo')],
282+
timeout: '1s'
283+
};
284+
// timeout is in FAST_OPTS
285+
execCLI(opts.resources.concat(FAST_OPTS_TIMEOUT_UNIT), {}).on('exit', function (code) {
286+
expect(code).toNotBe(0);
287+
done();
288+
});
289+
});
290+
});
291+
292+
274293
it('should timeout when some resources are not available and timout option is specified', function (done) {
275294
temp.mkdir({}, function (err, dirPath) {
276295
if (err) return done(err);
@@ -349,6 +368,31 @@ describe('cli', function () {
349368
});
350369
});
351370

371+
it('should timeout when an http resource does not respond before httpTimeout specified with unit', function (done) {
372+
const opts = {
373+
resources: ['http://localhost:8125'],
374+
timeout: 1000,
375+
interval: 100,
376+
window: 100,
377+
httpTimeout: '70ms'
378+
};
379+
380+
httpServer = http.createServer().on('request', function (req, res) {
381+
// make it a slow response, longer than the httpTimeout
382+
setTimeout(function () {
383+
res.end('data');
384+
}, 90);
385+
});
386+
httpServer.listen(8125, 'localhost');
387+
388+
const addOpts = '--httpTimeout 70ms'.split(' ');
389+
// timeout, interval, and window are in FAST_OPTS
390+
execCLI(opts.resources.concat(FAST_OPTS).concat(addOpts), {}).on('exit', function (code) {
391+
expect(code).toNotBe(0);
392+
done();
393+
});
394+
});
395+
352396
it('should timeout when an http GET resource is not available', function (done) {
353397
const opts = {
354398
resources: ['http-get://localhost:3999'],
@@ -422,6 +466,21 @@ describe('cli', function () {
422466
});
423467
});
424468

469+
it('should timeout when a service host is unreachable, tcpTimeout specified with unit', function (done) {
470+
const opts = {
471+
resources: ['tcp:256.0.0.1:1234'],
472+
timeout: 1000,
473+
tcpTimeout: '1s'
474+
};
475+
476+
const addOpts = '--tcpTimeout 1s'.split(' ');
477+
// timeout is in FAST_OPTS
478+
execCLI(opts.resources.concat(FAST_OPTS).concat(addOpts), {}).on('exit', function (code) {
479+
expect(code).toNotBe(0);
480+
done();
481+
});
482+
});
483+
425484
it('should timeout when a service is not listening to a socket', function (done) {
426485
let socketPath;
427486
temp.mkdir({}, function (err, dirPath) {

0 commit comments

Comments
 (0)