Skip to content
Open
Changes from 7 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
50 changes: 26 additions & 24 deletions test/graceful-shutdown.test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
'use strict'

const t = require('tap')
// Tests skip on win32 platforms due SIGINT signal is not supported across all windows platforms
const test = (process.platform === 'win32') ? t.skip : t.test
const { test, beforeEach, afterEach } = require('node:test')
const sinon = require('sinon')
const start = require('../start')

let _port = 3001

function getPort () {
return '' + _port++
}

let spy = null
let fastify = null
let spy = null
let signalCounter = null
const sandbox = sinon.createSandbox()

t.beforeEach(async () => {
// Skip tests on Windows, Linux and MacOS
const isWindows = process.platform === 'win32'
const isMacOS = process.platform === 'darwin'
const isLinux = process.platform === 'linux'
const conditionalTest = (isWindows || isMacOS || isLinux) ? test.skip : test

beforeEach(async () => {
signalCounter = process.listenerCount('SIGINT')

const argv = ['-p', getPort(), './examples/plugin.js']
fastify = await start.start(argv)
spy = sinon.spy(fastify, 'close')
})

t.afterEach(async () => {
afterEach(async () => {
sandbox.restore()
})

test('should add and remove SIGINT listener as expected ', async t => {
test('should add and remove SIGINT listener as expected', { skip: isWindows }, async (t) => {
t.plan(2)

t.equal(process.listenerCount('SIGINT'), signalCounter + 1)

const initialCount = process.listenerCount('SIGINT')
t.assert.strictEqual(initialCount, signalCounter + 1)
await fastify.close()

t.equal(process.listenerCount('SIGINT'), signalCounter)

t.end()
t.assert.strictEqual(process.listenerCount('SIGINT'), signalCounter)
})

test('should have called fastify.close() when receives a SIGINT signal', async t => {
process.once('SIGINT', () => {
sinon.assert.called(spy)

t.end()

process.exit()
})

conditionalTest('should call fastify.close() on SIGINT', (t) => {
const sigintHandler = async () => {
try {
sinon.assert.called(spy)
t.assert.ok('fastify.close() was called on SIGINT')
} finally {
process.removeListener('SIGINT', sigintHandler)
await fastify.close()
}
}

process.once('SIGINT', sigintHandler)
process.kill(process.pid, 'SIGINT')
})
Loading