Skip to content

Commit

Permalink
more PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
bengl committed Aug 27, 2021
1 parent 1892aa9 commit e4e4093
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
4 changes: 2 additions & 2 deletions fastify.js
Expand Up @@ -401,11 +401,11 @@ function fastify (options) {

try {
const dc = require('diagnostics_channel')
const initChannel = dc.channel('fastify.initialized')
const initChannel = dc.channel('fastify.initialization')
if (initChannel.hasSubscribers) {
initChannel.publish({ fastify })
}
} catch {
} catch (e) {
// This only happens if `diagnostics_channel` isn't available, i.e. earlier
// versions of Node.js. In that event, we don't care, so ignore the error.
}
Expand Down
69 changes: 52 additions & 17 deletions test/diagnostics-channel.test.js
Expand Up @@ -2,25 +2,60 @@

const t = require('tap')
const test = t.test
let dc
try {
dc = require('diagnostics_channel')
} catch {
// Not available in this version of Node.js
t.skip('diagnostics_channel is not available')
process.exit(0)
}

test('diagnostics_channel', t => {
t.plan(2)
const proxyquire = require('proxyquire')

test('diagnostics_channel when present and subscribers', t => {
t.plan(3)

let fastifyInHook

const channel = dc.channel('fastify.initialized')
channel.subscribe(function ({ fastify }) {
t.pass('message passed to channel')
fastifyInHook = fastify
})
const fastify = require('../fastify')()
const dc = {
channel (name) {
t.equal(name, 'fastify.initialization')
return {
hasSubscribers: true,
publish (event) {
t.ok(event.fastify)
fastifyInHook = event.fastify
}
}
},
'@noCallThru': true
}

const fastify = proxyquire('../fastify', {
diagnostics_channel: dc
})()
t.equal(fastifyInHook, fastify)
})

test('diagnostics_channel when present and no subscribers', t => {
t.plan(1)

const dc = {
channel (name) {
t.equal(name, 'fastify.initialization')
return {
hasSubscribers: false,
publish () {
t.fail('publish should not be called')
}
}
},
'@noCallThru': true
}

proxyquire('../fastify', {
diagnostics_channel: dc
})()
})

test('diagnostics_channel when not present', t => {
t.plan(1)

t.doesNotThrow(() => {
proxyquire('../fastify', {
diagnostics_channel: null
})()
})
})

0 comments on commit e4e4093

Please sign in to comment.