Spaces:
Sleeping
Sleeping
Commit ·
cfbebe0
1
Parent(s): 7f0d072
feat: add graceful shutdown handler
Browse files- Close the HTTP server and Redis connection on SIGINT and SIGTERM
- Add an integration test covering the shutdown path
- README.md +2 -1
- src/server/index.js +17 -4
- tests/README.md +3 -1
- tests/server/graceful-shutdown.test.js +90 -0
README.md
CHANGED
|
@@ -246,6 +246,7 @@ json-api-server/
|
|
| 246 |
│ │ └── redis.test.js # RESP protocol encoding/parsing, constructor options (25)
|
| 247 |
│ ├── server/
|
| 248 |
│ │ ├── coverage-printlog.test.js # V8 coverage: printLog, startServer, 500 catch (3)
|
|
|
|
| 249 |
│ │ ├── integration.test.js # API integration tests — real HTTP + SQLite (63)
|
| 250 |
│ │ └── server.test.js # Server request handler and startup paths (5)
|
| 251 |
│ ├── README.md # Testing documentation
|
|
@@ -329,7 +330,7 @@ This runs comprehensive queries to inspect row counts, column metadata, relation
|
|
| 329 |
|
| 330 |
## Testing
|
| 331 |
|
| 332 |
-
Uses **vitest** with **V8 native coverage**. **
|
| 333 |
|
| 334 |
```bash
|
| 335 |
npm test # Run all tests once
|
|
|
|
| 246 |
│ │ └── redis.test.js # RESP protocol encoding/parsing, constructor options (25)
|
| 247 |
│ ├── server/
|
| 248 |
│ │ ├── coverage-printlog.test.js # V8 coverage: printLog, startServer, 500 catch (3)
|
| 249 |
+
│ │ ├── graceful-shutdown.test.js # Graceful shutdown via SIGINT/SIGTERM (1)
|
| 250 |
│ │ ├── integration.test.js # API integration tests — real HTTP + SQLite (63)
|
| 251 |
│ │ └── server.test.js # Server request handler and startup paths (5)
|
| 252 |
│ ├── README.md # Testing documentation
|
|
|
|
| 330 |
|
| 331 |
## Testing
|
| 332 |
|
| 333 |
+
Uses **vitest** with **V8 native coverage**. **146 tests across 13 test files** cover the full stack — from integration tests (real HTTP server + SQLite) to unit tests for every module.
|
| 334 |
|
| 335 |
```bash
|
| 336 |
npm test # Run all tests once
|
src/server/index.js
CHANGED
|
@@ -291,8 +291,21 @@ function startServer() {
|
|
| 291 |
}
|
| 292 |
|
| 293 |
/* v8 ignore start */
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
-
|
|
|
|
|
|
| 291 |
}
|
| 292 |
|
| 293 |
/* v8 ignore start */
|
| 294 |
+
function closeServer() {
|
| 295 |
+
return new Promise((resolve) => {
|
| 296 |
+
server.close(resolve);
|
| 297 |
+
});
|
| 298 |
+
}
|
| 299 |
+
async function shutdown(signal) {
|
| 300 |
+
console.log(`\n[Server] ${signal} received — shutting down gracefully...`);
|
| 301 |
+
await closeServer();
|
| 302 |
+
try { await redis.quit(); } catch { /* ignore */ }
|
| 303 |
+
process.exit(0);
|
| 304 |
+
}
|
| 305 |
+
if (process.listenerCount('SIGINT') === 0) {
|
| 306 |
+
process.on('SIGINT', () => shutdown('SIGINT'));
|
| 307 |
+
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
| 308 |
+
}
|
| 309 |
|
| 310 |
+
/* v8 ignore stop */
|
| 311 |
+
module.exports = { server, requestHandler, printLog, closeServer };
|
tests/README.md
CHANGED
|
@@ -31,6 +31,7 @@ tests/
|
|
| 31 |
redis.test.js # RESP protocol encoding/parsing, constructor options (25 tests)
|
| 32 |
server/
|
| 33 |
coverage-printlog.test.js # V8 coverage: printLog, startServer, 500 catch (3 tests)
|
|
|
|
| 34 |
integration.test.js # API integration tests — real HTTP + SQLite (67 tests)
|
| 35 |
server.test.js # Server request handler and startup paths (5 tests)
|
| 36 |
helpers/
|
|
@@ -40,7 +41,7 @@ tests/
|
|
| 40 |
seed-settings-coverage.test.js # Seed-settings.js V8 coverage (2 tests)
|
| 41 |
```
|
| 42 |
|
| 43 |
-
**Total:
|
| 44 |
|
| 45 |
## Test design
|
| 46 |
|
|
@@ -59,6 +60,7 @@ Unit tests cover every source module individually. Each module has its own test
|
|
| 59 |
| `middleware/rate-limiter.js` | `tests/middleware/rate-limiter.test.js` | Module imported once; `createRateLimiter()` with different configs per test |
|
| 60 |
| `redis/index.js` | `tests/redis/redis.test.js` | RESP encoding/parsing directly; constructor options |
|
| 61 |
| `server/index.js` | `tests/server/server.test.js` | `requestHandler()` with mock req/res; CJS cache injection for DB mock |
|
|
|
|
| 62 |
| `server/index.js` (ESM) | `tests/server/coverage-printlog.test.js` | Dynamic `import()` for V8-covered printLog, startServer, 500 catch |
|
| 63 |
| `db/migrate.js` | `tests/db/migrate.test.js` | Real migration + corrupt DB failure path |
|
| 64 |
| `db/seed.js` | `tests/db/seed.test.js` | Dependency injection — `database` and `fetch` injected |
|
|
|
|
| 31 |
redis.test.js # RESP protocol encoding/parsing, constructor options (25 tests)
|
| 32 |
server/
|
| 33 |
coverage-printlog.test.js # V8 coverage: printLog, startServer, 500 catch (3 tests)
|
| 34 |
+
graceful-shutdown.test.js # SIGINT/SIGTERM handler coverage (1 test)
|
| 35 |
integration.test.js # API integration tests — real HTTP + SQLite (67 tests)
|
| 36 |
server.test.js # Server request handler and startup paths (5 tests)
|
| 37 |
helpers/
|
|
|
|
| 41 |
seed-settings-coverage.test.js # Seed-settings.js V8 coverage (2 tests)
|
| 42 |
```
|
| 43 |
|
| 44 |
+
**Total: 146 tests across 13 test files.**
|
| 45 |
|
| 46 |
## Test design
|
| 47 |
|
|
|
|
| 60 |
| `middleware/rate-limiter.js` | `tests/middleware/rate-limiter.test.js` | Module imported once; `createRateLimiter()` with different configs per test |
|
| 61 |
| `redis/index.js` | `tests/redis/redis.test.js` | RESP encoding/parsing directly; constructor options |
|
| 62 |
| `server/index.js` | `tests/server/server.test.js` | `requestHandler()` with mock req/res; CJS cache injection for DB mock |
|
| 63 |
+
| `server/index.js` (child) | `tests/server/graceful-shutdown.test.js` | SIGINT/SIGTERM via child process (V8 coverage) |
|
| 64 |
| `server/index.js` (ESM) | `tests/server/coverage-printlog.test.js` | Dynamic `import()` for V8-covered printLog, startServer, 500 catch |
|
| 65 |
| `db/migrate.js` | `tests/db/migrate.test.js` | Real migration + corrupt DB failure path |
|
| 66 |
| `db/seed.js` | `tests/db/seed.test.js` | Dependency injection — `database` and `fetch` injected |
|
tests/server/graceful-shutdown.test.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
| 2 |
+
import { spawn } from 'child_process'
|
| 3 |
+
import { mkdtempSync, rmSync } from 'fs'
|
| 4 |
+
import { tmpdir } from 'os'
|
| 5 |
+
import { dirname, join } from 'path'
|
| 6 |
+
import { fileURLToPath } from 'url'
|
| 7 |
+
|
| 8 |
+
const __dirname = dirname(fileURLToPath(import.meta.url))
|
| 9 |
+
const PROJECT_ROOT = join(__dirname, '../..')
|
| 10 |
+
const PORT = '30993'
|
| 11 |
+
|
| 12 |
+
function seedDatabase(dbPath) {
|
| 13 |
+
return new Promise((resolve, reject) => {
|
| 14 |
+
const child = spawn(process.execPath, ['tests/helpers/seed.js'], {
|
| 15 |
+
cwd: PROJECT_ROOT,
|
| 16 |
+
env: { ...process.env, DB_PATH: dbPath },
|
| 17 |
+
stdio: ['ignore', 'pipe', 'pipe'],
|
| 18 |
+
})
|
| 19 |
+
let stderr = ''
|
| 20 |
+
child.stderr.on('data', (d) => { stderr += d })
|
| 21 |
+
child.on('exit', (code) =>
|
| 22 |
+
code === 0 ? resolve() : reject(new Error(stderr || `Seed exited with code ${code}`)),
|
| 23 |
+
)
|
| 24 |
+
child.on('error', reject)
|
| 25 |
+
})
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
describe('Graceful shutdown', () => {
|
| 29 |
+
let tmpDir, dbPath
|
| 30 |
+
|
| 31 |
+
beforeAll(async () => {
|
| 32 |
+
tmpDir = mkdtempSync(join(tmpdir(), 'graceful-shutdown-'))
|
| 33 |
+
dbPath = join(tmpDir, 'test.db')
|
| 34 |
+
await seedDatabase(dbPath)
|
| 35 |
+
}, 15000)
|
| 36 |
+
|
| 37 |
+
afterAll(() => {
|
| 38 |
+
if (tmpDir) {
|
| 39 |
+
try { rmSync(tmpDir, { recursive: true, force: true }) } catch { /* ignore */ }
|
| 40 |
+
}
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
it('exits cleanly on SIGINT', async () => {
|
| 44 |
+
const binPath = join(PROJECT_ROOT, 'bin/start.js')
|
| 45 |
+
|
| 46 |
+
const child = spawn(process.execPath, [binPath], {
|
| 47 |
+
cwd: PROJECT_ROOT,
|
| 48 |
+
env: {
|
| 49 |
+
...process.env,
|
| 50 |
+
START_SERVER: 'true',
|
| 51 |
+
PORT,
|
| 52 |
+
REDIS_URL: '',
|
| 53 |
+
DB_PATH: dbPath,
|
| 54 |
+
RATE_LIMIT_ENABLED: 'false',
|
| 55 |
+
},
|
| 56 |
+
stdio: ['ignore', 'pipe', 'pipe'],
|
| 57 |
+
timeout: 15000,
|
| 58 |
+
})
|
| 59 |
+
|
| 60 |
+
let output = ''
|
| 61 |
+
child.stdout.on('data', (d) => { output += d.toString() })
|
| 62 |
+
|
| 63 |
+
await new Promise((resolve, reject) => {
|
| 64 |
+
const timeout = setTimeout(() => reject(new Error('Server startup timeout')), 10000)
|
| 65 |
+
const check = () => {
|
| 66 |
+
if (output.includes(`http://localhost:${PORT}`)) {
|
| 67 |
+
clearTimeout(timeout)
|
| 68 |
+
resolve()
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
child.stdout.on('data', check)
|
| 72 |
+
child.on('error', (err) => {
|
| 73 |
+
clearTimeout(timeout)
|
| 74 |
+
reject(err)
|
| 75 |
+
})
|
| 76 |
+
})
|
| 77 |
+
|
| 78 |
+
child.kill('SIGINT')
|
| 79 |
+
|
| 80 |
+
const code = await new Promise((resolve) => {
|
| 81 |
+
const timeout = setTimeout(() => resolve(null), 5000)
|
| 82 |
+
child.on('exit', (code) => {
|
| 83 |
+
clearTimeout(timeout)
|
| 84 |
+
resolve(code)
|
| 85 |
+
})
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
+
expect(code).toBe(0)
|
| 89 |
+
}, 20000)
|
| 90 |
+
})
|