From c64f883fc113616654225091454a25775eb9894f Mon Sep 17 00:00:00 2001 From: retrohacker Date: Tue, 26 Sep 2017 12:56:31 -0700 Subject: [PATCH 1/4] fix: dont include interval in lag * plugin.close for all tests --- lib/plugins/cpuUsageThrottle.js | 2 +- test/plugins/cpuUsageThrottle.test.js | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 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..7220248b1 100644 --- a/test/plugins/cpuUsageThrottle.test.js +++ b/test/plugins/cpuUsageThrottle.test.js @@ -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(); assert(cont instanceof Error, 'Should call next with error'); assert.equal(cont.statusCode, 503, 'Defaults to 503 status'); done(); @@ -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); @@ -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(); }); @@ -80,10 +80,24 @@ 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 = () => (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(); @@ -107,6 +121,7 @@ describe('cpuUsageThrottle', function () { assert.equal(res.statusCode, 503, 'Default shed status code returned'); clearTimeout(plugin._timeout); + plugin.close(); done(); }); }); From 35f59a52be66e4c136a78e2e70da51780ea347c5 Mon Sep 17 00:00:00 2001 From: retrohacker Date: Tue, 26 Sep 2017 13:10:10 -0700 Subject: [PATCH 2/4] lint --- test/plugins/cpuUsageThrottle.test.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/test/plugins/cpuUsageThrottle.test.js b/test/plugins/cpuUsageThrottle.test.js index 7220248b1..661a68390 100644 --- a/test/plugins/cpuUsageThrottle.test.js +++ b/test/plugins/cpuUsageThrottle.test.js @@ -86,16 +86,18 @@ describe('cpuUsageThrottle', function () { }); 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 = () => (now++ > 0) * opts.interval; - var plugin = cpuUsageThrottle(opts); - Date.now = dn; - plugin.close(); - assert.equal(plugin.state.lag, 0); - 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(); }); From fc8b526be5a86bd559894f0598dda59ac6c46f50 Mon Sep 17 00:00:00 2001 From: retrohacker Date: Mon, 9 Oct 2017 08:33:28 -0700 Subject: [PATCH 3/4] ensure close is called after each test --- test/plugins/cpuUsageThrottle.test.js | 28 +++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/test/plugins/cpuUsageThrottle.test.js b/test/plugins/cpuUsageThrottle.test.js index 661a68390..25085e96f 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 = undefined + 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) { - plugin.close(); assert(cont instanceof Error, 'Should call next with error'); assert.equal(cont.statusCode, 503, 'Defaults to 503 status'); done(); @@ -40,9 +41,8 @@ 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) { - plugin.close(); assert.isUndefined(cont, 'Should call next'); done(); } @@ -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, @@ -64,7 +64,6 @@ 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'); @@ -79,8 +78,7 @@ describe('cpuUsageThrottle', function () { halfLife: 50, interval: 50 }; - var plugin = cpuUsageThrottle(opts); - plugin.close(); + plugin = cpuUsageThrottle(opts); assert.equal(plugin.name, 'cpuUsageThrottle'); done(); }); @@ -93,9 +91,8 @@ describe('cpuUsageThrottle', function () { Date.now = function () { return (now++ > 0) * opts.interval; }; - var plugin = cpuUsageThrottle(opts); + plugin = cpuUsageThrottle(opts); Date.now = dn; - plugin.close(); assert.equal(plugin.state.lag, 0); done(); }); @@ -107,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); @@ -123,12 +120,19 @@ describe('cpuUsageThrottle', function () { assert.equal(res.statusCode, 503, 'Default shed status code returned'); clearTimeout(plugin._timeout); - plugin.close(); done(); }); }); }); + afterEach(function(done) { + if(plugin) { + plugin.close(); + } + plugin = undefined; + done(); + }); + after('Teardown: Reset Math.random', function (done) { Math.random = MR; done(); From 22171f5cad8f4b849ab92ff56519f9139ff8da02 Mon Sep 17 00:00:00 2001 From: retrohacker Date: Mon, 9 Oct 2017 08:35:45 -0700 Subject: [PATCH 4/4] lint fix --- test/plugins/cpuUsageThrottle.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/plugins/cpuUsageThrottle.test.js b/test/plugins/cpuUsageThrottle.test.js index 25085e96f..0b49b3792 100644 --- a/test/plugins/cpuUsageThrottle.test.js +++ b/test/plugins/cpuUsageThrottle.test.js @@ -19,7 +19,7 @@ var cpuUsageThrottle = proxyquire('../../lib/plugins/cpuUsageThrottle.js', { var MR = Math.random; describe('cpuUsageThrottle', function () { - var plugin = undefined + var plugin; before('Setup: stub math.random', function (done) { Math.random = function () { @@ -125,8 +125,8 @@ describe('cpuUsageThrottle', function () { }); }); - afterEach(function(done) { - if(plugin) { + afterEach(function (done) { + if (plugin) { plugin.close(); } plugin = undefined;