diff --git a/lib/hooks.js b/lib/hooks.js index 45fdb2b332..d744b8716d 100644 --- a/lib/hooks.js +++ b/lib/hooks.js @@ -181,10 +181,6 @@ function onListenHookRunner (server) { const hooks = server[kHooks].onListen const hooksLen = hooks.length - if (hooksLen === 0) { - return - } - let i = 0 let c = 0 @@ -196,7 +192,7 @@ function onListenHookRunner (server) { if ( i === hooksLen ) { - if (c < server[kChildren].length) { + while (c < server[kChildren].length) { const child = server[kChildren][c++] onListenHookRunner(child) } diff --git a/test/hooks.on-listen.test.js b/test/hooks.on-listen.test.js index 24718ef03a..f13159fc12 100644 --- a/test/hooks.on-listen.test.js +++ b/test/hooks.on-listen.test.js @@ -272,8 +272,8 @@ test('localhost Register onListen hook after a plugin inside a plugin should log }) }) -test('localhost onListen encapsulation should be called in order', t => { - t.plan(6) +test('localhost onListen encapsulation should be called in order', async t => { + t.plan(8) const fastify = Fastify() t.teardown(fastify.close.bind(fastify)) @@ -285,20 +285,75 @@ test('localhost onListen encapsulation should be called in order', t => { done() }) - fastify.register(async (childOne, o) => { + await fastify.register(async (childOne, o) => { childOne.addHook('onListen', function (done) { t.equal(++order, 2, 'called in childOne') t.equal(this.pluginName, childOne.pluginName, 'the this binding is the right instance') done() }) - childOne.register(async (childTwo, o) => { + + await childOne.register(async (childTwo, o) => { childTwo.addHook('onListen', async function () { t.equal(++order, 3, 'called in childTwo') t.equal(this.pluginName, childTwo.pluginName, 'the this binding is the right instance') }) }) + + await childOne.register(async (childTwoPeer, o) => { + childTwoPeer.addHook('onListen', async function () { + t.equal(++order, 4, 'called second in childTwo') + t.equal(this.pluginName, childTwoPeer.pluginName, 'the this binding is the right instance') + }) + }) }) - fastify.listen({ + await fastify.listen({ + host: 'localhost', + port: 0 + }) +}) + +test('localhost onListen encapsulation with only nested hook', async t => { + t.plan(1) + const fastify = Fastify() + t.teardown(fastify.close.bind(fastify)) + + await fastify.register(async (child) => { + await child.register(async (child2) => { + child2.addHook('onListen', function (done) { + t.pass() + done() + }) + }) + }) + + await fastify.listen({ + host: 'localhost', + port: 0 + }) +}) + +test('localhost onListen peer encapsulations with only nested hooks', async t => { + t.plan(2) + const fastify = Fastify() + t.teardown(fastify.close.bind(fastify)) + + await fastify.register(async (child) => { + await child.register(async (child2) => { + child2.addHook('onListen', function (done) { + t.pass() + done() + }) + }) + + await child.register(async (child2) => { + child2.addHook('onListen', function (done) { + t.pass() + done() + }) + }) + }) + + await fastify.listen({ host: 'localhost', port: 0 })