Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timers: fix regression with clearImmediate() #9086

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,21 @@ function processImmediate() {
domain.enter();

immediate._callback = immediate._onImmediate;

// Save next in case `clearImmediate(immediate)` is called from callback
var next = immediate._idleNext;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const?

Copy link
Contributor Author

@mscdex mscdex Oct 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept with var because it is still used throughout timers.js.


tryOnImmediate(immediate, tail);

if (domain)
domain.exit();

immediate = immediate._idleNext;
// If `clearImmediate(immediate)` wasn't called from the callback, use the
// `immediate`'s next item
if (immediate._idleNext)
immediate = immediate._idleNext;
else
immediate = next;
}

// Only round-trip to C++ land if we have to. Calling clearImmediate() on an
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-timers-clearImmediate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
require('../common');
const assert = require('assert');

const N = 3;
var count = 0;
function next() {
const immediate = setImmediate(function() {
clearImmediate(immediate);
++count;
});
}
for (var i = 0; i < N; ++i)
next();

process.on('exit', () => {
assert.strictEqual(count, N, `Expected ${N} immediate callback executions`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe even go a bit further?

'use strict';
const common = require('../common');

function next() {
  const immediate = setImmediate(common.mustCall(function callback() {
    clearImmediate(immediate);
  }));
}
for (var i = 0; i < 3; ++i)
  next();

One benefit is that we don't lose the information of what count was if the assertion fires.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One benefit is that we don't lose the information of what count was if the assertion fires.

I don't understand what you mean here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the way the test is written now, when the test fails, we get:

AssertionError: Expected 3 immediate callback executions

So, we were expecting 3, but we got some other number. How many? 0? 1? 2? 4?

So I was looking at how to work count in there so that information would be preserved, but I realized we could just do away with the explicit exit listener entirely if we use common.mustCall(). So if you use the test as written in that previous comment and it fails, you get results of this format:

Mismatched callback function calls. Expected 1, actual 0.

And the test is a compact 10 lines to boot.

(As usual: It's a nit, so you know, ignore my suggestion if you don't like it.)

Copy link
Contributor Author

@mscdex mscdex Oct 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fishrock123 Is this ok with you, since you previously requested that a custom assertion message be added?

});