Skip to content

Commit

Permalink
fix: ensure onListen hooks are called when they should be (fastify#…
Browse files Browse the repository at this point in the history
…5273)

* fix: ensure `onListen` hooks is called for nested encapsulation contexts

* also ensure `onListen` runs for all peer contexts
  • Loading branch information
bienzaaron committed Jan 17, 2024
1 parent 7dc69db commit 79042e6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
6 changes: 1 addition & 5 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
Expand Down
65 changes: 60 additions & 5 deletions test/hooks.on-listen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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
})
Expand Down

0 comments on commit 79042e6

Please sign in to comment.