From eecb2d259deda34c2f297f2ef8b6d4fedc504e9e Mon Sep 17 00:00:00 2001 From: William Blankenship Date: Mon, 9 Oct 2017 14:17:01 -0700 Subject: [PATCH] fix(cpuUsageThrottle): dont include interval in lag (#1504) --- lib/plugins/cpuUsageThrottle.js | 2 +- test/plugins/cpuUsageThrottle.test.js | 37 +++++++++++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/plugins/cpuUsageThrottle.js b/lib/plugins/cpuUsageThrottle.js index c03f3365f..9d94632f8 100644 --- a/lib/plugins/cpuUsageThrottle.js +++ b/lib/plugins/cpuUsageThrottle.js @@ -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; }); } diff --git a/test/plugins/cpuUsageThrottle.test.js b/test/plugins/cpuUsageThrottle.test.js index 006ee6162..0b49b3792 100644 --- a/test/plugins/cpuUsageThrottle.test.js +++ b/test/plugins/cpuUsageThrottle.test.js @@ -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; @@ -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(); @@ -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); @@ -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, @@ -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(); }); @@ -79,11 +78,25 @@ 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(); @@ -91,7 +104,7 @@ describe('cpuUsageThrottle', function () { 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); @@ -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();