From 9de8cf0f139a8c23d7d55e5ccda20643dfe00dee Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 28 Mar 2014 01:00:43 -0700 Subject: [PATCH] Fix load limit condition. Closes #1536 --- lib/server.js | 7 +++---- test/server.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/lib/server.js b/lib/server.js index 96c90181c..62de422a5 100755 --- a/lib/server.js +++ b/lib/server.js @@ -240,7 +240,7 @@ internals.Server.prototype._dispatch = function (options) { // Check load if (limits.sampleInterval && - ((limits.maxEventLoopDelay && (load.eventLoopDelay > limits.maxEventLoopDelay || self._loadBench.elapsed() - limits.sampleInterval > limits.maxEventLoopDelay)) || + ((limits.maxEventLoopDelay && (load.eventLoopDelay > limits.maxEventLoopDelay || self._loadBench.elapsed() > limits.maxEventLoopDelay)) || (limits.maxHeapUsedBytes && load.heapUsed > limits.maxHeapUsedBytes) || (limits.maxRssBytes && load.rss > limits.maxRssBytes))) { @@ -373,8 +373,7 @@ internals.Server.prototype._stop = function (options, callback) { Object.keys(self._connections).forEach(function (key) { - var connection = self._connections[key]; - return connection && connection.destroy(); + self._connections[key].destroy(); }); }, options.timeout); @@ -457,7 +456,7 @@ internals.Server.prototype.inject = function (options, callback) { delete res.raw.res._hapi; } else { - res.result = res.result || res.payload; + res.result = res.payload; } return callback(res); diff --git a/test/server.js b/test/server.js index 5cc23f34e..740f312d4 100755 --- a/test/server.js +++ b/test/server.js @@ -393,6 +393,38 @@ describe('Server', function () { }); }); + it('rejects request due to high event loop delay load before next sample', function (done) { + + var server = new Hapi.Server(0, { load: { sampleInterval: 500, maxEventLoopDelay: 1 } }); + var handler = function (request, reply) { + + var start = Date.now(); + while (Date.now() - start < 10); + reply('ok'); + }; + + server.route({ method: 'GET', path: '/', handler: handler }); + server.start(function (err) { + + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(200); + + setImmediate(function () { + + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(503); + server.stop(function () { + + done(); + }); + }); + }); + }); + }); + }); + it('reuses the same cache segment', function (done) { var server = new Hapi.Server({ cache: { engine: 'catbox-memory', shared: true } }); @@ -1041,6 +1073,16 @@ describe('Server', function () { expect(server._stateDefinitions['steve']).deep.equal(Defaults.state); done(); }); + + it('throws when missing name', function (done) { + + var server = new Hapi.Server(); + expect(function () { + + server.state(); + }).to.throw('Invalid name'); + done(); + }); }); describe('Timeouts', function () {