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

fix: dont include interval in lag #1504

Merged
merged 4 commits into from Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
23 changes: 20 additions & 3 deletions test/plugins/cpuUsageThrottle.test.js
Expand Up @@ -30,7 +30,7 @@ describe('cpuUsageThrottle', function () {
var opts = { limit: 0, interval: 500 };
var plugin = cpuUsageThrottle(opts);
function next (cont) {
clearTimeout(plugin._timeout);
plugin.close();
Copy link
Member

Choose a reason for hiding this comment

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

Should this just be in an afterEach block?

Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a way to do that without maintaining state outside the test?

Copy link
Member

Choose a reason for hiding this comment

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

Not that I know of - usually just use a plugin var within the closure.

assert(cont instanceof Error, 'Should call next with error');
assert.equal(cont.statusCode, 503, 'Defaults to 503 status');
done();
Expand All @@ -42,8 +42,8 @@ describe('cpuUsageThrottle', function () {
var opts = { interval: 500, limit: 0.9 };
var plugin = cpuUsageThrottle(opts);
function next (cont) {
plugin.close();
assert.isUndefined(cont, 'Should call next');
clearTimeout(plugin._timeout);
done();
}
plugin({}, {}, next);
Expand All @@ -64,11 +64,11 @@ describe('cpuUsageThrottle', function () {
interval: 1000
};
plugin.update(opts);
plugin.close();
assert.equal(plugin.state.limit, opts.limit, 'opts.limit');
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 @@ -80,10 +80,26 @@ describe('cpuUsageThrottle', function () {
interval: 50
};
var plugin = cpuUsageThrottle(opts);
plugin.close();
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;
};
var plugin = cpuUsageThrottle(opts);
Date.now = dn;
plugin.close();
assert.equal(plugin.state.lag, 0);
done();
});


it('Integration: Should shed load', function (done) {
var server = restify.createServer();
Expand All @@ -107,6 +123,7 @@ describe('cpuUsageThrottle', function () {
assert.equal(res.statusCode, 503,
'Default shed status code returned');
clearTimeout(plugin._timeout);
plugin.close();
done();
});
});
Expand Down