Skip to content

Commit

Permalink
add additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffbski committed Nov 3, 2023
1 parent 5dab987 commit 3caa77c
Showing 1 changed file with 60 additions and 1 deletion.
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 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);
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 3caa77c

Please sign in to comment.