Skip to content

Commit

Permalink
do not exit when only unref'd timer is present in test code; closes #…
Browse files Browse the repository at this point in the history
…3817

Signed-off-by: Christopher Hiller <[email protected]>
  • Loading branch information
boneskull committed Mar 11, 2019
1 parent ca9eba6 commit 399f813
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var setTimeout = global.setTimeout;
var clearTimeout = global.clearTimeout;
var toString = Object.prototype.toString;

var MAX_TIMEOUT = Math.pow(2, 31) - 1;

module.exports = Runnable;

/**
Expand Down Expand Up @@ -77,8 +79,7 @@ Runnable.prototype.timeout = function(ms) {
}

// Clamp to range
var INT_MAX = Math.pow(2, 31) - 1;
var range = [0, INT_MAX];
var range = [0, MAX_TIMEOUT];
ms = utils.clamp(ms, range);

// see #1652 for reasoning
Expand Down Expand Up @@ -253,18 +254,17 @@ Runnable.prototype.inspect = function() {
*/
Runnable.prototype.resetTimeout = function() {
var self = this;
var ms = this.timeout() || 1e9;
// if timeout is 0, _enableTimeouts should also be false.
// we set this timeout to MAX_TIMEOUT so the event loop stays active.
var ms = this.timeout() || MAX_TIMEOUT;

if (!this._enableTimeouts) {
return;
}
this.clearTimeout();
this.timer = setTimeout(function() {
if (!self._enableTimeouts) {
return;
// only throw error if timeouts are enabled.
if (self._enableTimeouts) {
self.callback(self._timeoutError(ms));
self.timedOut = true;
}
self.callback(self._timeoutError(ms));
self.timedOut = true;
}, ms);
};

Expand Down
3 changes: 3 additions & 0 deletions test/integration/fixtures/options/timeout-unref.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('unrefs a timeout', function(done) {
setTimeout(done, 10).unref();
});
19 changes: 19 additions & 0 deletions test/integration/options/timeout.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var helpers = require('../helpers');
var runMochaJSON = helpers.runMochaJSON;

describe('--timeout', function() {
var args = [];

it("should complete tests having unref'd async behavior", function(done) {
args = ['--timeout', '0'];
runMochaJSON('options/timeout-unref', args, function(err, res) {
if (err) {
return done(err);
}
expect(res, 'to have passed').and('to have passed test count', 1);
done();
});
});
});

0 comments on commit 399f813

Please sign in to comment.