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

lib: remove redundant code from timers.js #3143

Closed
wants to merge 3 commits into from

Conversation

Trott
Copy link
Member

@Trott Trott commented Oct 1, 2015

insert() is only called from one place where there is already a check
that msecs is greater than or equal to zero
, so do not repeat the check
inside insert().

@Trott Trott added the timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. label Oct 1, 2015
@Trott
Copy link
Member Author

Trott commented Oct 1, 2015

@Fishrock123
Copy link
Contributor

Seems to have originated along with the start of timers, ala pre- bc47353, perhaps insert was once used in more than one place. Not sure.

LGTM. Can you ensure there is something to test that negative timeouts do not fire?

@thefourtheye
Copy link
Contributor

Can you leave this check and remove the one at the place where it is called?

@Trott
Copy link
Member Author

Trott commented Oct 2, 2015

@Fishrock123 Regarding testing negative timeouts: test/parallel/test-timers.js has a bunch of tests using setTimeout() and setInterval() for various negative numbers. But the very first thing setTimeout() and setInterval() both do is change any value less than 1 to 1. So active() never actually gets the negative number. Does that cover the situation you are concerned about? Or not quite because of the guard code in setTimeout() and setInterval()?

Since active() is exported by timers.js, I can try to rig up a test calling it directly. But I dont' think active() should actually be exported. I can't find anything else that uses it, it's not documented, and the argument it requires is awfully specific to timers.js internal working, so I think it should be deprecated and made internal to timers.js. (But uh, that's a much bigger thing than I'd want to tackle with this small PR which I thought was going to be a two-line change...)

@Trott
Copy link
Member Author

Trott commented Oct 2, 2015

@thefourtheye Yes, that is a slightly better approach. Thanks for suggesting it. I've made the change and pushed.

@thefourtheye
Copy link
Contributor

LGTM.

@@ -26,11 +26,11 @@ var lists = {};
// the main function - creates lists on demand and the watchers associated
// with them.
function insert(item, msecs) {
if (msecs < 0) return;

item._idleStart = Timer.now();
item._idleTimeout = msecs;
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't this line also redundant?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. In fact, the whole separation of insert() from active() at this point seems redundant. Just pushed an additional commit.

@Fishrock123
Copy link
Contributor

Yes, active is public but should not be -- see: #896

(I'll deal with this soon(tm).)

I'd add a test anyways since it currently is public, if the others don't already test it. I haven't looked.

@Trott
Copy link
Member Author

Trott commented Oct 2, 2015

@Fishrock123 Per your suggestion, I've added a test for timers.active(). Thanks.

@Trott
Copy link
Member Author

Trott commented Oct 3, 2015

@Fishrock123 Regarding #896, if (and only if) you want, I'd be happy to take the first step of that off your plate and make those APIs private within the file and replace the existing exposed functions with deprecation warning-wrapped versions and put it in a semver major PR.

@Fishrock123
Copy link
Contributor

@Trott not worthwhile I think. They should be reworked at the same time. I'll look at it once I'm back from vacation.

@Fishrock123
Copy link
Contributor

This LGTM if it works & CI is happy.

@Trott
Copy link
Member Author

Trott commented Oct 5, 2015

legitTimers.forEach(function(legit) {
active(legit);
// active() should mutate these objects
assert.notDeepEqual(Object.keys(legit), ['_idleTimeout']);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just hasOwnProperty? Also I feel that if we can check the values also it would be better.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this check _idleStart also?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also note that we do now have notDeepStrictEqual :D

Copy link
Member Author

Choose a reason for hiding this comment

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

@thefourtheye @Fishrock123 OK, I've improved the tests quite a bit, I think. Looks good to you now?

insert() is only called from one place where there is already a check
that msecs is greater than or equal to zero, so do not repeat the check
inside insert().

timers.active() is not documented and should not be exposed, but since
it is exposed for now, let's test it.
@Trott
Copy link
Member Author

Trott commented Oct 6, 2015

Made the tests a fair bit more rigorous with input from @thefourtheye and @Fishrock123. New CI: https://ci.nodejs.org/job/node-test-pull-request/426/

const assert = require('assert');
const active = require('timers').active;

// active() should not create a timer for these
Copy link
Contributor

Choose a reason for hiding this comment

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

should, or should not?

Copy link
Contributor

Choose a reason for hiding this comment

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

AH, this should be "should". :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed the typo. Thanks for the catch.

bogusTimers.forEach(function(bogus) {
const savedTimeout = bogus._idleTimeout;
active(bogus);
// active() should not mutate these objects
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: these?

@thefourtheye
Copy link
Contributor

LGTM

Trott added a commit to Trott/io.js that referenced this pull request Oct 6, 2015
insert() is only called from one place where there is already a check
that msecs is greater than or equal to zero, so do not repeat the check
inside insert().

timers.active() is not documented and should not be exposed, but since
it is exposed for now, let's test it.

PR-URL: nodejs#3143
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
@Trott
Copy link
Member Author

Trott commented Oct 6, 2015

Landed in 070aac4

@Trott Trott closed this Oct 6, 2015
@jasnell jasnell mentioned this pull request Oct 8, 2015
29 tasks
Trott added a commit that referenced this pull request Oct 8, 2015
insert() is only called from one place where there is already a check
that msecs is greater than or equal to zero, so do not repeat the check
inside insert().

timers.active() is not documented and should not be exposed, but since
it is exposed for now, let's test it.

PR-URL: #3143
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants