Skip to content

Commit

Permalink
fix: do not exit when only unref'd timer is present in test code (#3825)
Browse files Browse the repository at this point in the history
* do not exit when only unref'd timer is present in test code; closes #3817

Signed-off-by: Christopher Hiller <boneskull@boneskull.com>

* 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 <boneskull@boneskull.com>
Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
boneskull and JoshuaKGoldberg committed Jul 20, 2024
1 parent 24560c1 commit 6d24689
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -95,8 +97,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 @@ -233,11 +234,8 @@ Runnable.prototype.clearTimeout = function () {
*/
Runnable.prototype.resetTimeout = function () {
var self = this;
var ms = this.timeout();
var ms = this.timeout() || MAX_TIMEOUT;

if (ms === 0) {
return;
}
this.clearTimeout();
this.timer = setTimeout(function () {
if (self.timeout() === 0) {
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();
});
11 changes: 11 additions & 0 deletions test/integration/options/timeout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,15 @@ describe('--timeout', function () {
}
);
});

it("should complete tests having unref'd async behavior", function(done) {
runMochaJSON('options/timeout-unref', ['--timeout', '0'], function(err, res) {
if (err) {
done(err);
return;
}
expect(res, 'to have passed').and('to have passed test count', 1);
done();
});
});
});
1 change: 1 addition & 0 deletions test/unit/runnable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ describe('Runnable(title, fn)', function () {
runnable.timeout(10);
runnable.resetTimeout();
runnable.timeout(0);
runnable.run();
setTimeout(function () {
expect(runnable.timedOut, 'to be', false);
done();
Expand Down

0 comments on commit 6d24689

Please sign in to comment.