Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(cpuUsageThrottle): dont include interval in lag (#1504)
  • Loading branch information
William Blankenship committed Oct 9, 2017
1 parent f7126f2 commit eecb2d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/cpuUsageThrottle.js
Expand Up @@ -142,7 +142,7 @@ function cpuUsageThrottlePlugin (opts) {
// updated the _reject value and how long it actually took. This
// metric accounts for the misbehaviour of pidusage.stat
var now = Date.now();
self._timeoutDelta = now - self._timeoutStart;
self._timeoutDelta = now - self._timeoutStart - self._interval;
self._timeoutStart = now;
});
}
Expand Down
37 changes: 29 additions & 8 deletions test/plugins/cpuUsageThrottle.test.js
Expand Up @@ -19,6 +19,8 @@ var cpuUsageThrottle = proxyquire('../../lib/plugins/cpuUsageThrottle.js', {
var MR = Math.random;
describe('cpuUsageThrottle', function () {

var plugin;

before('Setup: stub math.random', function (done) {
Math.random = function () {
return 0;
Expand All @@ -28,9 +30,8 @@ describe('cpuUsageThrottle', function () {

it('Unit: Should shed load', function (done) {
var opts = { limit: 0, interval: 500 };
var plugin = cpuUsageThrottle(opts);
plugin = cpuUsageThrottle(opts);
function next (cont) {
clearTimeout(plugin._timeout);
assert(cont instanceof Error, 'Should call next with error');
assert.equal(cont.statusCode, 503, 'Defaults to 503 status');
done();
Expand All @@ -40,10 +41,9 @@ describe('cpuUsageThrottle', function () {

it('Unit: Should let request through when not under load', function (done) {
var opts = { interval: 500, limit: 0.9 };
var plugin = cpuUsageThrottle(opts);
plugin = cpuUsageThrottle(opts);
function next (cont) {
assert.isUndefined(cont, 'Should call next');
clearTimeout(plugin._timeout);
done();
}
plugin({}, {}, next);
Expand All @@ -56,7 +56,7 @@ describe('cpuUsageThrottle', function () {
halfLife: 50,
interval: 50
};
var plugin = cpuUsageThrottle(opts);
plugin = cpuUsageThrottle(opts);
opts = {
max: 0.5,
limit: 0.1,
Expand All @@ -68,7 +68,6 @@ describe('cpuUsageThrottle', function () {
assert.equal(plugin.state.max, opts.max, 'opts.max');
assert.equal(plugin.state.halfLife, opts.halfLife, 'opts.halfLife');
assert.equal(plugin.state.interval, opts.interval, 'opts.interval');
plugin.close();
done();
});

Expand All @@ -79,19 +78,33 @@ describe('cpuUsageThrottle', function () {
halfLife: 50,
interval: 50
};
var plugin = cpuUsageThrottle(opts);
plugin = cpuUsageThrottle(opts);
assert.equal(plugin.name, 'cpuUsageThrottle');
done();
});

it('Unit: Should report proper lag', function (done) {
var opts = { max: 1, limit: 0.9, halfLife: 50, interval: 50 };
var dn = Date.now;
var now = 0;
// First timer will be 0, all future timers will be interval
Date.now = function () {
return (now++ > 0) * opts.interval;
};
plugin = cpuUsageThrottle(opts);
Date.now = dn;
assert.equal(plugin.state.lag, 0);
done();
});


it('Integration: Should shed load', function (done) {
var server = restify.createServer();
var client = {
close: function () {}
};
var opts = { interval: 500, limit: 0 };
var plugin = cpuUsageThrottle(opts);
plugin = cpuUsageThrottle(opts);
server.pre(plugin);
server.get('/foo', function (req, res, next) {
res.send(200);
Expand All @@ -112,6 +125,14 @@ describe('cpuUsageThrottle', function () {
});
});

afterEach(function (done) {
if (plugin) {
plugin.close();
}
plugin = undefined;
done();
});

after('Teardown: Reset Math.random', function (done) {
Math.random = MR;
done();
Expand Down

0 comments on commit eecb2d2

Please sign in to comment.