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

add timeout support for kill() #51

Merged
merged 3 commits into from
Apr 21, 2017
Merged

add timeout support for kill() #51

merged 3 commits into from
Apr 21, 2017

Conversation

neekey
Copy link
Owner

@neekey neekey commented Apr 1, 2017

What

Add timeout for kill() function. Now by default the timeout will be 30s, if the killing is not successful after 30s, the callback will be invoked with an timeout error, you can also manually set a timeout value (as seconds):

ps.kill(id, { timeout: 10 }, function(){}); // set the timeout to 10s

@neekey neekey force-pushed the bugfix/kill_timeout branch 2 times, most recently from 918f0eb to 6efec76 Compare April 1, 2017 07:10
Copy link

@trotzig trotzig left a comment

Choose a reason for hiding this comment

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

Looks good from my end. I left some suggestions for improvements, but overall it looks like this should work. Thanks for taking the time to help us out!

test/test.js Outdated
});
});
});
});
Copy link

Choose a reason for hiding this comment

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

It's a little annoying for the test to have to wait the default 30 seconds. I get why you want to cover this case though. I would have been okay with a 5 or 10 second timeout to be honest.

test/test.js Outdated
var killStartDate = Date.now();
PS.lookup({pid: pid}, function (err, list) {
assert.equal(list.length, 1);
PS.kill(pid, { timeout: 5 }, function (err) {
Copy link

Choose a reason for hiding this comment

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

Picking a lower number here would make tests faster.

});
}

next && checkKilled(next);

checkTimeoutTimer = next && setTimeout(function() {
Copy link

Choose a reason for hiding this comment

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

Using setTimeout here seems a little overkill. You could just remember a timestamp when the killing started, then compare the current timestamp against that inside the recursion. That would remove the need for the two variables to keep track of timing out.

Copy link
Owner Author

Choose a reason for hiding this comment

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

That sounds doable and easier to implement, will try : )

@lencioni
Copy link

lencioni commented Apr 1, 2017

It probably makes sense to bring in something like sinon so you can manually control the clock instead of waiting. This will make tests faster and less flakey. Look at sinon.useFakeTimers.

@neekey
Copy link
Owner Author

neekey commented Apr 2, 2017

@lencioni That's a good suggestion, will try : )

@neekey
Copy link
Owner Author

neekey commented Apr 2, 2017

@lencioni @trotzig thanks for reviewing this PR and your suggestions, I will do some optimization based on your suggestions, should be easy.

The problem I am facing now is the test cases randomly fails, I put some logs to Travis-CI, it seems like that the result of the process checking is not stable, for the timeout test cases for kill(), since I have already faked the process.kill, the ps.kill should not work, and the checking should always return 1 which means the process is still alive, but according to the console log I put in Travis, sometimes the result returned as 0 even the process is still alive, and the next check becomes 1 back again.

You can see here https://travis-ci.org/neekey/ps/jobs/217466603#L340, so the randomly false checking result causes the test case failed.

That's just very weird and I have to say it happens before that's why there is a confident check in the checking.

Do you guys have any idea about this? Otherwise, I will keep on debugging on it until I can fix it.

@neekey
Copy link
Owner Author

neekey commented Apr 2, 2017

I did more debugging, and found somehow in the Travis machine, sometime's the data event of the spawn process just didn't fire, neither did the error event, but the process just fired the exit process, which cause the checking's result as 0 (not found the target process), very weird... https://travis-ci.org/neekey/ps/jobs/217699731#L1338

This happens only for node 5 and 0.12

@neekey
Copy link
Owner Author

neekey commented Apr 21, 2017

I added an extra regression for checkConfident to prevent the unstable check result to interrupt the result 26d1902, have created an issue for this #55, will merge this PR now : )

@neekey neekey merged commit c2f3ba0 into release/0.1.6 Apr 21, 2017
@neekey neekey deleted the bugfix/kill_timeout branch April 21, 2017 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants