29 lines
1.3 KiB
TypeScript
29 lines
1.3 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* **空闲连接被掐断不能让进程崩掉**(2026-10-23 真机事故):
|
||
* dev 后端 `Error: Connection terminated unexpectedly` + `Emitted 'error' event on BoundPool instance`
|
||
* ⇒ unhandled 'error' ⇒ Node 直接退出,API 端口消失、页面全部打不开。
|
||
* 根因:pg 的 Pool 没有 `error` 监听器时,空闲连接错误会变成未捕获异常。
|
||
*/
|
||
const src = readFileSync(new URL('../src/db.ts', import.meta.url), 'utf8');
|
||
|
||
describe('db pool 错误兜底', () => {
|
||
it('注册了 pool error 监听(记录 + 让 pool 自行恢复,不抛出)', () => {
|
||
expect(src).toContain("pool.on('error'");
|
||
expect(src).toMatch(/pool\.on\('error', \(error\) => \{/);
|
||
expect(src).toContain('[db] idle client error');
|
||
});
|
||
|
||
it('监听器只 warn、不 rethrow(rethrow 仍会杀进程)', () => {
|
||
const block = src.slice(src.indexOf("pool.on('error'"), src.indexOf("pool.on('error'") + 400);
|
||
expect(block).toContain('console.warn');
|
||
expect(block).not.toContain('throw');
|
||
});
|
||
|
||
it('连接池仍是同一条 DATABASE_URL 配置(没顺手改连接参数)', () => {
|
||
expect(src).toContain('connectionString: config.databaseUrl');
|
||
});
|
||
});
|