Skip to content

Commit 6d24689

Browse files
fix: do not exit when only unref'd timer is present in test code (#3825)
* do not exit when only unref'd timer is present in test code; closes #3817 Signed-off-by: Christopher Hiller <[email protected]> * git checkout master -- docs/example/tests.html * fix: still bail if self.timeout() === 0 * fix: unref timers implicitly scheduled for MAX_TIMEOUT * Revert "fix: unref timers implicitly scheduled for MAX_TIMEOUT" This reverts commit d4a65aa. * test: call .run() to not leave timer pending --------- Signed-off-by: Christopher Hiller <[email protected]> Co-authored-by: Josh Goldberg <[email protected]>
1 parent 24560c1 commit 6d24689

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

lib/runnable.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var setTimeout = global.setTimeout;
2020
var clearTimeout = global.clearTimeout;
2121
var toString = Object.prototype.toString;
2222

23+
var MAX_TIMEOUT = Math.pow(2, 31) - 1;
24+
2325
module.exports = Runnable;
2426

2527
/**
@@ -95,8 +97,7 @@ Runnable.prototype.timeout = function (ms) {
9597
}
9698

9799
// Clamp to range
98-
var INT_MAX = Math.pow(2, 31) - 1;
99-
var range = [0, INT_MAX];
100+
var range = [0, MAX_TIMEOUT];
100101
ms = utils.clamp(ms, range);
101102

102103
// see #1652 for reasoning
@@ -233,11 +234,8 @@ Runnable.prototype.clearTimeout = function () {
233234
*/
234235
Runnable.prototype.resetTimeout = function () {
235236
var self = this;
236-
var ms = this.timeout();
237+
var ms = this.timeout() || MAX_TIMEOUT;
237238

238-
if (ms === 0) {
239-
return;
240-
}
241239
this.clearTimeout();
242240
this.timer = setTimeout(function () {
243241
if (self.timeout() === 0) {
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+
});

test/integration/options/timeout.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,15 @@ describe('--timeout', function () {
8383
}
8484
);
8585
});
86+
87+
it("should complete tests having unref'd async behavior", function(done) {
88+
runMochaJSON('options/timeout-unref', ['--timeout', '0'], function(err, res) {
89+
if (err) {
90+
done(err);
91+
return;
92+
}
93+
expect(res, 'to have passed').and('to have passed test count', 1);
94+
done();
95+
});
96+
});
8697
});

test/unit/runnable.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ describe('Runnable(title, fn)', function () {
702702
runnable.timeout(10);
703703
runnable.resetTimeout();
704704
runnable.timeout(0);
705+
runnable.run();
705706
setTimeout(function () {
706707
expect(runnable.timedOut, 'to be', false);
707708
done();

0 commit comments

Comments
 (0)