Skip to content

Commit 6899a86

Browse files
authored
fix: add closeClient on namespaced client (#76)
1 parent 9af1faf commit 6899a86

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ function fastifyRedis (fastify, options, next) {
3636
}
3737

3838
fastify.redis[namespace] = client
39+
if (options.closeClient === true) {
40+
fastify.addHook('onClose', closeNamedInstance)
41+
}
3942
} else {
4043
if (fastify.redis) {
4144
return next(new Error('fastify-redis has already been registered'))

test/test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,41 @@ test('custom client inside a namespace', (t) => {
243243
})
244244
})
245245

246+
test('custom client inside a namespace gets closed', (t) => {
247+
t.plan(7)
248+
const fastify = Fastify()
249+
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
250+
251+
fastify.register(fastifyRedis, {
252+
namespace: 'test',
253+
client: redis,
254+
closeClient: true
255+
})
256+
257+
fastify.ready((err) => {
258+
t.error(err)
259+
t.is(fastify.redis.test, redis)
260+
261+
fastify.redis.test.set('key', 'value', (err) => {
262+
t.error(err)
263+
fastify.redis.test.get('key', (err, val) => {
264+
t.error(err)
265+
t.equal(val, 'value')
266+
267+
const origQuit = fastify.redis.test.quit
268+
fastify.redis.test.quit = (cb) => {
269+
t.pass('redis client closed')
270+
origQuit.call(fastify.redis.test, cb)
271+
}
272+
273+
fastify.close(function (err) {
274+
t.error(err)
275+
})
276+
})
277+
})
278+
})
279+
})
280+
246281
test('fastify.redis.test should throw with duplicate connection namespaces', (t) => {
247282
t.plan(1)
248283

0 commit comments

Comments
 (0)