Files
SupplierSystem/server/tests/dbPoolErrorGuard.test.ts
Ubuntu 325162dd86
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(db): 空闲连接被掐断不再杀死进程——Pool 注册 error 监听(真机事故:Connection terminated unexpectedly 导致 API 退出)
2026-10-23 16:01:27 +08:00

29 lines
1.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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');
});
});