Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a restarted decorator flag #30

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -55,6 +55,7 @@ async function start (opts) {
old.server.removeListener('clientError', listener)
}
res.app = newApp

await old.close()
}

Expand All @@ -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) {
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ setGlobalDispatcher(new Agent({
}))

test('restart fastify', async ({ pass, teardown, plan, same, equal }) => {
plan(9)
plan(11)

const _opts = {
port: 0,
Expand All @@ -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)

Expand All @@ -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}`)
Expand Down