diff --git a/index.js b/index.js index 8f3dcdb..acb7483 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ async function start (opts) { let listening = false let stopped = false const res = { - app: await (spinUpFastify(opts, serverWrapper, restart).ready()), + app: await (spinUpFastify(opts, serverWrapper, restart, true).ready()), restart, get address () { if (!listening) { @@ -55,6 +55,7 @@ async function start (opts) { old.server.removeListener('clientError', listener) } res.app = newApp + await old.close() } @@ -73,7 +74,7 @@ async function start (opts) { } } -function spinUpFastify (opts, serverWrapper, restart) { +function spinUpFastify (opts, serverWrapper, restart, isStart = false) { const server = serverWrapper.server const _opts = Object.assign({}, opts) _opts.serverFactory = function (handler) { @@ -83,7 +84,7 @@ function spinUpFastify (opts, serverWrapper, restart) { const app = Fastify(_opts) app.decorate('restart', restart) - + app.decorate('restarted', !isStart) app.register(opts.app, opts) return app diff --git a/test/test.js b/test/test.js index 88120f3..ba120e8 100644 --- a/test/test.js +++ b/test/test.js @@ -17,7 +17,7 @@ setGlobalDispatcher(new Agent({ })) test('restart fastify', async ({ pass, teardown, plan, same, equal }) => { - plan(9) + plan(11) const _opts = { port: 0, @@ -33,6 +33,9 @@ test('restart fastify', async ({ pass, teardown, plan, same, equal }) => { } const server = await start(_opts) + + same(server.app.restarted, false) + const { stop, restart, listen } = server teardown(stop) @@ -48,6 +51,7 @@ test('restart fastify', async ({ pass, teardown, plan, same, equal }) => { } await restart() + same(server.app.restarted, true) { const res = await request(`http://127.0.0.1:${port}`)