Skip to content

Commit

Permalink
Prevented duplicate "request completed" logs (#63)
Browse files Browse the repository at this point in the history
Co-authored-by: Manuel Spigolon <[email protected]>
  • Loading branch information
chrismeyers and Eomm authored Mar 20, 2022
1 parent b107a7c commit 6ce56ac
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/classic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ function plugin(fastify, opts, next) {

if (disposeOnClose) {
fastify.addHook('onClose', (instance, done) => {
return diContainer.dispose().then(done, done)
diContainer.dispose().then(done, done)
})
}

if (disposeOnResponse) {
fastify.addHook('onResponse', (request, reply, done) => {
return request.diScope.dispose().then(done, done)
request.diScope.dispose().then(done, done)
})
}

Expand Down
4 changes: 2 additions & 2 deletions lib/fastifyAwilixPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ function plugin(fastify, opts, next) {

if (disposeOnClose) {
fastify.addHook('onClose', (instance, done) => {
return diContainer.dispose().then(done)
diContainer.dispose().then(done, done)
})
}

if (disposeOnResponse) {
fastify.addHook('onResponse', (request, reply, done) => {
return request.diScope.dispose().then(done)
request.diScope.dispose().then(done, done)
})
}

Expand Down
72 changes: 63 additions & 9 deletions test/fastifyAwilixPlugin.dispose.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ describe('fastifyAwilixPlugin', () => {

variations.forEach((variation) => {
describe(variation.name, () => {
describe('dispose singletons', () => {
const endpoint = async (req, res) => {
const userRepository = app.diContainer.resolve('userRepository')
storedUserRepository = userRepository
expect(userRepository.disposeCounter).toEqual(0)
const endpoint = async (req, res) => {
const userRepository = app.diContainer.resolve('userRepository')
storedUserRepository = userRepository
expect(userRepository.disposeCounter).toEqual(0)

res.send({
status: 'OK',
})
}
res.send({
status: 'OK',
})
}

describe('dispose singletons', () => {
const endpointWithScope = async (req, res) => {
const userRepository = req.diScope.resolve('userRepository')
const userRepositoryScoped = req.diScope.resolve('userRepositoryScoped')
Expand Down Expand Up @@ -172,6 +172,60 @@ describe('fastifyAwilixPlugin', () => {
expect(storedUserRepository.disposeCounter).toEqual(1)
})
})

describe('response logging', () => {
it('should only produce one "request completed" log with dispose settings enabled', async () => {
let requestCompletedLogCount = 0
jest.spyOn(process.stdout, 'write').mockImplementation((data) => {
const log = JSON.parse(data)
if (log.msg === 'request completed') requestCompletedLogCount += 1
return true
})

app = fastify({ logger: true })
app.register(variation.plugin, { disposeOnClose: true, disposeOnResponse: true })
variation.container.register({
userRepository: asClass(UserRepository, {
lifetime: Lifetime.SINGLETON,
dispose: (service) => service.dispose(),
}),
})

app.post('/', endpoint)
await app.ready()

await app.inject().post('/').end()

await app.close()
expect(requestCompletedLogCount).toEqual(1)
})

it('should only produce one "request completed" log with dispose settings disabled', async () => {
let requestCompletedLogCount = 0
jest.spyOn(process.stdout, 'write').mockImplementation((data) => {
const log = JSON.parse(data)
if (log.msg === 'request completed') requestCompletedLogCount += 1
return true
})

app = fastify({ logger: true })
app.register(variation.plugin, { disposeOnClose: false, disposeOnResponse: false })
variation.container.register({
userRepository: asClass(UserRepository, {
lifetime: Lifetime.SINGLETON,
dispose: (service) => service.dispose(),
}),
})

app.post('/', endpoint)
await app.ready()

await app.inject().post('/').end()

await app.close()
expect(requestCompletedLogCount).toEqual(1)
})
})
})
})
})

0 comments on commit 6ce56ac

Please sign in to comment.