Skip to content
Snippets Groups Projects
Commit badd6e24 authored by Tamlyn Rhodes's avatar Tamlyn Rhodes
Browse files

fix(server): wait before closing pubsub database connection

Add `drain` parameter to `destroy` function to optionally await the `drain` event on the pubsub database client before calling `end`.
Expose manager on `pubsweet-server` package root.
parent 08b119cf
No related branches found
No related tags found
No related merge requests found
......@@ -42,12 +42,18 @@ module.exports = {
return pubsub
},
destroy: () => {
if (client) {
/**
* @param drain {boolean} whether to wait for the database client to emit 'drain' before closing
* @returns {Promise<void>}
*/
destroy: async ({ drain } = {}) => {
if (pubsub) {
if (drain) {
await new Promise(resolve => pubsub.client.on('drain', resolve))
}
await pubsub.client.end()
pubsub = null
return client.end()
}
return Promise.resolve()
},
/**
......
......@@ -8,6 +8,7 @@ module.exports.Collection = require('./models/Collection')
module.exports.helpers = require('./helpers/authorization')
module.exports.db = require('./db')
module.exports.pubsubManager = require('./graphql/pubsub')
module.exports.NotFoundError = require('./errors/NotFoundError')
module.exports.startServer = require('./start-server')
const { getPubsub, destroy } = require('../../src/graphql/pubsub')
describe('pubsub manager', () => {
afterEach(() => destroy())
it('can call destroy before connect', () =>
expect(destroy()).resolves.toBeUndefined())
it('waits for client to drain before closing', async () => {
const pubsub = await getPubsub()
const cb = jest.fn()
pubsub.subscribe('test_channel', cb)
pubsub.publish('test_channel', { test: 'content' })
const destroyPromise = destroy({ drain: true })
expect(cb).not.toHaveBeenCalled()
await destroyPromise
expect(cb).toHaveBeenCalledWith({ test: 'content' })
})
})
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment