Skip to content

Commit 603da44

Browse files
committed
do not exit when only unref'd timer is present in test code; closes #3817
Signed-off-by: Christopher Hiller <[email protected]>
1 parent ca9eba6 commit 603da44

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

lib/runnable.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var setTimeout = global.setTimeout;
1616
var clearTimeout = global.clearTimeout;
1717
var toString = Object.prototype.toString;
1818

19+
var MAX_TIMEOUT = Math.pow(2, 31) - 1;
20+
1921
module.exports = Runnable;
2022

2123
/**
@@ -77,8 +79,7 @@ Runnable.prototype.timeout = function(ms) {
7779
}
7880

7981
// Clamp to range
80-
var INT_MAX = Math.pow(2, 31) - 1;
81-
var range = [0, INT_MAX];
82+
var range = [0, MAX_TIMEOUT];
8283
ms = utils.clamp(ms, range);
8384

8485
// see #1652 for reasoning
@@ -253,18 +254,19 @@ Runnable.prototype.inspect = function() {
253254
*/
254255
Runnable.prototype.resetTimeout = function() {
255256
var self = this;
256-
var ms = this.timeout() || 1e9;
257+
// if timeout is 0, _enableTimeouts should also be false.
258+
// we set this timeout to MAX_TIMEOUT so the event loop stays active.
259+
var ms = this.timeout() || MAX_TIMEOUT;
257260

258-
if (!this._enableTimeouts) {
259-
return;
260-
}
261261
this.clearTimeout();
262262
this.timer = setTimeout(function() {
263-
if (!self._enableTimeouts) {
264-
return;
263+
// only throw error if timeouts are enabled.
264+
if (self._enableTimeouts) {
265+
self.callback(self._timeoutError(ms));
266+
self.timedOut = true;
267+
} else {
268+
self.resetTimeout();
265269
}
266-
self.callback(self._timeoutError(ms));
267-
self.timedOut = true;
268270
}, ms);
269271
};
270272

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it('unrefs a timeout', function(done) {
2+
setTimeout(done, 10).unref();
3+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
var helpers = require('../helpers');
4+
var runMochaJSON = helpers.runMochaJSON;
5+
6+
describe('--timeout', function() {
7+
var args = [];
8+
9+
it("should complete tests having unref'd async behavior", function(done) {
10+
args = ['--timeout', '0'];
11+
runMochaJSON('options/timeout-unref', args, function(err, res) {
12+
if (err) {
13+
return done(err);
14+
}
15+
expect(res, 'to have passed').and('to have passed test count', 1);
16+
done();
17+
});
18+
});
19+
});

0 commit comments

Comments
 (0)