Skip to content

Commit

Permalink
Adds a tcp timeout. Fixes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
TKasperczyk committed Oct 30, 2018
1 parent 911cc18 commit b32d023
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -110,6 +110,10 @@ Standard Options:
Maximum time in ms to wait before exiting with failure (1) code,
default Infinity
--tcpTimeout
Maximum time in ms for tcp connect, default 300ms
-v, --verbose
Enable debug output to stdout
Expand Down Expand Up @@ -145,6 +149,7 @@ var opts = {
delay: 1000, // initial delay in ms, default 0
interval: 100, // poll interval in ms, default 250ms
timeout: 30000, // timeout in ms, default Infinity
tcpTimeout: 1000, // tcp timeout in ms, default 300ms
window: 1000, // stabilization time in ms, default 750ms

// http options
Expand Down Expand Up @@ -200,6 +205,7 @@ waitOn(opts, [cb]) - function which triggers resource checks
- opts.log - optional flag which outputs to stdout, remaining resources waited on and when complete or errored
- opts.reverse - optional flag to reverse operation so checks are for resources being NOT available, default false
- opts.timeout - optional timeout in ms, default Infinity. Aborts with error.
- opts.tcpTimeout - optional tcp timeout in ms, default 300ms
- opts.verbose - optional flag which outputs debug output, default false
- opts.window - optional stabilization time in ms, default 750ms. Waits this amount of time for file sizes to stabilize or other resource availability to remain unchanged.
- http(s) specific options, see https://github.com/request/request#readme for specific details
Expand Down
4 changes: 4 additions & 0 deletions bin/usage.txt
Expand Up @@ -58,6 +58,10 @@ Standard Options:
Maximum time in ms to wait before exiting with failure (1) code,
default Infinity

--tcpTimeout

Maximum time in ms for tcp connect, default 300ms

-v, --verbose

Enable debug output to stdout
Expand Down
2 changes: 1 addition & 1 deletion bin/wait-on
Expand Up @@ -42,7 +42,7 @@ if (configFile) {
opts.resources = argv._;

// now check for specific options and set those
opts = ['delay', 'interval', 'log', 'reverse', 'timeout', 'verbose', 'window']
opts = ['delay', 'interval', 'log', 'reverse', 'timeout', 'tcpTimeout', 'verbose', 'window']
.reduce(function (accum, x) {
if (argv[x]) { accum[x] = argv[x]; }
return accum;
Expand Down
14 changes: 10 additions & 4 deletions lib/wait-on.js
Expand Up @@ -20,6 +20,7 @@ var WAIT_ON_SCHEMA = Joi.object().keys({
timeout: Joi.number().integer().min(0).default(Infinity),
verbose: Joi.boolean().default(false),
window: Joi.number().integer().min(0).default(750),
tcpTimeout: Joi.number().integer().min(0).default(300),

// http options
ca: [Joi.string(), Joi.binary()],
Expand Down Expand Up @@ -78,7 +79,7 @@ function waitOn(opts, cb) {
} else {
resolve();
}
})
})
});
}
}
Expand Down Expand Up @@ -234,7 +235,7 @@ function create$(resource, options) {
} else if (resource.startsWith('https-get:')) {
return createHttpGet$('https:' + resource.slice('https-get:'.length), options);
} else if (resource.startsWith('tcp:')) {
return createTcp$(resource.slice('tcp:'.length));
return createTcp$(resource.slice('tcp:'.length), options);
} else if (resource.startsWith('socket:')) {
return createSocket$(resource.slice('socket:'.length));
} else if (resource.startsWith('file:')) {
Expand Down Expand Up @@ -295,7 +296,7 @@ function createHttpGet$(url, options) {
});
}

function createTcp$(hostAndPort) {
function createTcp$(hostAndPort, options) {
var arrParts = hostAndPort.split(':');
var port = arrParts[arrParts.length - 1];
var host = arrParts[arrParts.length - 2] || 'localhost';
Expand All @@ -309,7 +310,12 @@ function createTcp$(hostAndPort) {
observer.onNext({ val: 1 });
observer.onCompleted();
conn.end();
});
}).on('timeout', function () {
observer.onNext({ val: -1, err: {}});
observer.onCompleted();
conn.end();
});
conn.setTimeout(options.tcpTimeout);
});
}

Expand Down
40 changes: 35 additions & 5 deletions test/api.mocha.js
Expand Up @@ -388,6 +388,21 @@ describe('api', function () {
});
});

it('should timeout when a service host is unreachable', function (done) {
var opts = {
resources: [
'tcp:256.0.0.1:1234'
],
timeout: 1000,
tcpTimeout: 1000
};

waitOn(opts, function (err) {
expect(err).toExist();
done();
});
});

it('should timeout when an http service listening to a socket returns 404', function (done) {
var socketPath;
temp.mkdir({}, function (err, dirPath) {
Expand Down Expand Up @@ -449,6 +464,21 @@ describe('api', function () {
});
});

it('should succeed when a service host is unreachable in reverse mode', function (done) {
var opts = {
resources: [
'tcp:256.0.0.1:1234'
],
timeout: 1000,
tcpTimeout: 1000
};

waitOn(opts, function (err) {
expect().toNotExist();
done();
});
});

it('should succeed when file resources are not available in reverse mode', function (done) {
temp.mkdir({}, function (err, dirPath) {
var opts = {
Expand Down Expand Up @@ -535,12 +565,12 @@ describe('api', function () {
path.resolve(dirPath, 'bar')
]
};

setTimeout(function () {
fs.writeFile(opts.resources[0], 'data1', function () {});
fs.writeFile(opts.resources[1], 'data2', function () {});
}, 300);

waitOn(opts)
.then(function () {
done();
Expand All @@ -567,7 +597,7 @@ describe('api', function () {
});
});
});

it('should timeout when some resources are not available and timout option is specified', function (done) {
temp.mkdir({}, function (err, dirPath) {
var opts = {
Expand Down Expand Up @@ -607,7 +637,7 @@ describe('api', function () {
});
});
});

it('should succeed when file resources are not available later in reverse mode', function (done) {
temp.mkdir({}, function (err, dirPath) {
var opts = {
Expand All @@ -632,7 +662,7 @@ describe('api', function () {
});
});
});

it('should timeout when file resources are available in reverse mode', function (done) {
temp.mkdir({}, function (err, dirPath) {
var opts = {
Expand Down
32 changes: 32 additions & 0 deletions test/cli.mocha.js
Expand Up @@ -380,6 +380,22 @@ describe('cli', function () {
});
});

it('should timeout when a service host is unreachable', function (done) {
var opts = {
resources: [
'tcp:256.0.0.1:1234'
],
timeout: 1000,
tcpTimeout: 1000
};

execCLI(opts.resources.concat(FAST_OPTS), {})
.on('exit', function (code) {
expect(code).toNotBe(0);
done();
});
});

it('should timeout when a service is not listening to a socket', function (done) {
var socketPath;
temp.mkdir({}, function (err, dirPath) {
Expand Down Expand Up @@ -492,4 +508,20 @@ describe('cli', function () {
});
});

it('should succeed when a service host is unreachable in reverse mode', function (done) {
var opts = {
resources: [
'tcp:256.0.0.1:1234'
],
timeout: 1000,
tcpTimeout: 1000,
};
var OPTS = FAST_OPTS.concat(['-r']);
execCLI(opts.resources.concat(OPTS), {})
.on('exit', function (code) {
expect(code).toBe(0);
done();
});
});

});

0 comments on commit b32d023

Please sign in to comment.