This commit is contained in:
@@ -2,23 +2,25 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { registerCleanup, graceful, initShutdownHandlers, clearCleanup } from '../../server/utils/shutdown.js'
|
||||
|
||||
describe('shutdown integration', () => {
|
||||
let originalExit
|
||||
let exitCalls
|
||||
let originalOn
|
||||
const testState = {
|
||||
originalExit: null,
|
||||
exitCalls: [],
|
||||
originalOn: null,
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
clearCleanup()
|
||||
exitCalls = []
|
||||
originalExit = process.exit
|
||||
testState.exitCalls = []
|
||||
testState.originalExit = process.exit
|
||||
process.exit = vi.fn((code) => {
|
||||
exitCalls.push(code)
|
||||
testState.exitCalls.push(code)
|
||||
})
|
||||
originalOn = process.on
|
||||
testState.originalOn = process.on
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
process.exit = originalExit
|
||||
process.on = originalOn
|
||||
process.exit = testState.originalExit
|
||||
process.on = testState.originalOn
|
||||
clearCleanup()
|
||||
})
|
||||
|
||||
@@ -30,7 +32,7 @@ describe('shutdown integration', () => {
|
||||
initShutdownHandlers()
|
||||
expect(process.on).toHaveBeenCalledWith('SIGTERM', expect.any(Function))
|
||||
expect(process.on).toHaveBeenCalledWith('SIGINT', expect.any(Function))
|
||||
process.on = originalOn
|
||||
process.on = testState.originalOn
|
||||
})
|
||||
|
||||
it('signal handler calls graceful', async () => {
|
||||
@@ -42,8 +44,8 @@ describe('shutdown integration', () => {
|
||||
const sigtermHandler = handlers.SIGTERM
|
||||
expect(sigtermHandler).toBeDefined()
|
||||
await sigtermHandler()
|
||||
expect(exitCalls.length).toBeGreaterThan(0)
|
||||
process.on = originalOn
|
||||
expect(testState.exitCalls.length).toBeGreaterThan(0)
|
||||
process.on = testState.originalOn
|
||||
})
|
||||
|
||||
it('signal handler handles graceful error', async () => {
|
||||
@@ -58,8 +60,8 @@ describe('shutdown integration', () => {
|
||||
throw new Error('Force error')
|
||||
})
|
||||
await sigintHandler()
|
||||
expect(exitCalls.length).toBeGreaterThan(0)
|
||||
process.on = originalOn
|
||||
expect(testState.exitCalls.length).toBeGreaterThan(0)
|
||||
process.on = testState.originalOn
|
||||
})
|
||||
|
||||
it('covers timeout path in graceful', async () => {
|
||||
@@ -68,7 +70,7 @@ describe('shutdown integration', () => {
|
||||
})
|
||||
graceful()
|
||||
await new Promise(resolve => setTimeout(resolve, 100))
|
||||
expect(exitCalls.length).toBeGreaterThan(0)
|
||||
expect(testState.exitCalls.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('covers graceful catch block', async () => {
|
||||
@@ -76,6 +78,6 @@ describe('shutdown integration', () => {
|
||||
throw new Error('Test error')
|
||||
})
|
||||
await graceful()
|
||||
expect(exitCalls.length).toBeGreaterThan(0)
|
||||
expect(testState.exitCalls.length).toBeGreaterThan(0)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user