mirror of
https://github.com/jjbrunton/Sauci.git
synced 2026-10-31 18:19:43 +00:00
Share one configured PostgreSQL pool across worker repositories and make API and worker connection budgets explicit. Add privacy-safe aggregate request, query, pool, sync, and outbox telemetry, a loopback-only representative load fixture, bounded worker shutdown, and focused regression and integration coverage. Document the database-wide budget formula and defer pg_stat_statements activation to a reviewed migration and planned restart.
30 lines
1.7 KiB
TypeScript
30 lines
1.7 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { OUTBOX_STATE_SQL, PostgresOperationsRepository } from '../src/domains/operations/repository.js';
|
|
|
|
describe('operations outbox observation', () => {
|
|
it('uses a transaction-scoped statement timeout and always releases on query failure', async () => {
|
|
const query = vi.fn(async (sql: string) => {
|
|
if (sql === OUTBOX_STATE_SQL) throw new Error('statement timeout');
|
|
return { rows: [], rowCount: 0 };
|
|
});
|
|
const release = vi.fn();
|
|
const pool = { connect: vi.fn(async () => ({ query, release })) };
|
|
const repository = new PostgresOperationsRepository(pool as never);
|
|
await expect(repository.outboxState()).rejects.toThrow('statement timeout');
|
|
expect(query).toHaveBeenNthCalledWith(1, 'begin');
|
|
expect(query).toHaveBeenNthCalledWith(2, "set local statement_timeout = '250ms'");
|
|
expect(query).toHaveBeenNthCalledWith(3, OUTBOX_STATE_SQL);
|
|
expect(query).toHaveBeenCalledWith('rollback');
|
|
expect(release).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it('evicts the client when rollback itself fails while preserving the original error', async () => {
|
|
const original = new Error('statement timeout'); const rollback = new Error('rollback failed');
|
|
const query = vi.fn(async (sql: string) => { if (sql === OUTBOX_STATE_SQL) throw original; if (sql === 'rollback') throw rollback; return { rows: [], rowCount: 0 }; });
|
|
const release = vi.fn(); const repository = new PostgresOperationsRepository({ connect: vi.fn(async () => ({ query, release })) } as never);
|
|
await expect(repository.outboxState()).rejects.toBe(original);
|
|
expect(release).toHaveBeenCalledTimes(1);
|
|
expect(release).toHaveBeenCalledWith(rollback);
|
|
});
|
|
});
|