Skip to content

Commit

Permalink
fix(cpuUsageThrottle): support breaking change in pidusage module
Browse files Browse the repository at this point in the history
  • Loading branch information
retrohacker committed Mar 18, 2019
1 parent 85ca117 commit 7460064
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/plugins/cpuUsageThrottle.js
Expand Up @@ -130,14 +130,14 @@ function cpuUsageThrottlePlugin(opts) {
// plugin._timeoutDelta represents the amount of time between when we
// _should_ have run updateReject and the actual time it was invoked.
// This allows us to monitor lag caused by both the event loop
// and pidusage.stat
// and pidusage
plugin._timeoutDelta = 0;
plugin._timeoutStart = Date.now();

// updateReject should be called on an interval, it checks the average CPU
// usage between two invocations of updateReject.
function updateReject() {
pidusage.stat(process.pid, function pidusageStat(e, stat) {
pidusage(process.pid, function pidusageStat(e, stat) {
// Requeue an updateReject irrespective of whether or not pidusage
// encountered an error
plugin._timeout = setTimeout(updateReject, plugin._interval);
Expand All @@ -164,7 +164,7 @@ function cpuUsageThrottlePlugin(opts) {

// Calculate how long it took between when our interval should have
// updated the _reject value and how long it actually took. This
// metric accounts for the misbehaviour of pidusage.stat
// metric accounts for the misbehaviour of pidusage
var now = Date.now();
plugin._timeoutDelta =
now - plugin._timeoutStart - plugin._interval;
Expand Down
17 changes: 13 additions & 4 deletions test/plugins/cpuUsageThrottle.test.js
Expand Up @@ -5,15 +5,14 @@ var assert = require('chai').assert;
var proxyquire = require('proxyquire');
var restify = require('../../lib/index.js');
var restifyClients = require('restify-clients');
var pidusage = require('pidusage');

// Allow tests to set the CPU usage returned by pidUsage
var CPU = 50;

var cpuUsageThrottle = proxyquire('../../lib/plugins/cpuUsageThrottle.js', {
pidusage: {
stat: function(pid, cb) {
return cb(null, { cpu: CPU });
}
pidusage: function(pid, cb) {
return cb(null, { cpu: CPU });
}
});

Expand Down Expand Up @@ -129,6 +128,16 @@ describe('cpuUsageThrottle', function() {
});
});

it('Integration: pidusage should report CPU usage', function(done) {
assert.isFunction(pidusage, 'pidusage can be invoked');
pidusage(process.pid, function(e, stat) {
assert.ifError(e);
assert.isObject(stat);
assert.isNumber(stat.cpu);
done();
});
});

afterEach(function(done) {
if (plugin) {
plugin.close();
Expand Down

0 comments on commit 7460064

Please sign in to comment.