Spaces:
Sleeping
Sleeping
File size: 2,471 Bytes
cfbebe0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { spawn } from 'child_process'
import { mkdtempSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const PROJECT_ROOT = join(__dirname, '../..')
const PORT = '30993'
function seedDatabase(dbPath) {
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, ['tests/helpers/seed.js'], {
cwd: PROJECT_ROOT,
env: { ...process.env, DB_PATH: dbPath },
stdio: ['ignore', 'pipe', 'pipe'],
})
let stderr = ''
child.stderr.on('data', (d) => { stderr += d })
child.on('exit', (code) =>
code === 0 ? resolve() : reject(new Error(stderr || `Seed exited with code ${code}`)),
)
child.on('error', reject)
})
}
describe('Graceful shutdown', () => {
let tmpDir, dbPath
beforeAll(async () => {
tmpDir = mkdtempSync(join(tmpdir(), 'graceful-shutdown-'))
dbPath = join(tmpDir, 'test.db')
await seedDatabase(dbPath)
}, 15000)
afterAll(() => {
if (tmpDir) {
try { rmSync(tmpDir, { recursive: true, force: true }) } catch { /* ignore */ }
}
})
it('exits cleanly on SIGINT', async () => {
const binPath = join(PROJECT_ROOT, 'bin/start.js')
const child = spawn(process.execPath, [binPath], {
cwd: PROJECT_ROOT,
env: {
...process.env,
START_SERVER: 'true',
PORT,
REDIS_URL: '',
DB_PATH: dbPath,
RATE_LIMIT_ENABLED: 'false',
},
stdio: ['ignore', 'pipe', 'pipe'],
timeout: 15000,
})
let output = ''
child.stdout.on('data', (d) => { output += d.toString() })
await new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Server startup timeout')), 10000)
const check = () => {
if (output.includes(`http://localhost:${PORT}`)) {
clearTimeout(timeout)
resolve()
}
}
child.stdout.on('data', check)
child.on('error', (err) => {
clearTimeout(timeout)
reject(err)
})
})
child.kill('SIGINT')
const code = await new Promise((resolve) => {
const timeout = setTimeout(() => resolve(null), 5000)
child.on('exit', (code) => {
clearTimeout(timeout)
resolve(code)
})
})
expect(code).toBe(0)
}, 20000)
})
|