Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(postgres-driver): PgBouncer cancel twin uses its own database; goldens carry desyncDiscards

time-attackaba43f0a949b
diff --git a/test/e2e/postgres-driver-cancel-pipeline.test.ts b/test/e2e/postgres-driver-cancel-pipeline.test.ts
index a3ded43282..21629ccd57 100644
--- a/test/e2e/postgres-driver-cancel-pipeline.test.ts
+++ b/test/e2e/postgres-driver-cancel-pipeline.test.ts
@@ -4,21 +4,42 @@
* sent for it was read for the next query: the next query resolved with the cancelled statement's rows (a bare
* count resolved to []) and the cancelled query never settled. The cancelled query now stays current, absorbs its
* own answer and rejects as cancelled (57014); every later query reads its own rows. Runs direct and, when
- * GBRAIN_PGBOUNCER_URL is set, through PgBouncer in transaction mode.
+ * the CI PgBouncer fixture is configured (GBRAIN_PGBOUNCER_URL + GBRAIN_PGBOUNCER_DIRECT_URL), through PgBouncer
+ * in transaction mode against a database of its own.
*/
-import { describe, expect, test } from 'bun:test';
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import postgres from '#postgres';
import { assertSafeE2eDatabaseUrl } from '../helpers/db-guard.ts';
-const targets = [['direct', process.env.DATABASE_URL], ['pgbouncer', process.env.GBRAIN_PGBOUNCER_URL]].filter((t): t is [string, string] => !!t[1]);
+const pooled = process.env.GBRAIN_PGBOUNCER_URL;
+const pooledAdmin = process.env.GBRAIN_PGBOUNCER_DIRECT_URL;
+if (process.env.GBRAIN_CI_REQUIRE_PGBOUNCER === '1' && !(pooled && pooledAdmin)) throw new Error('The cancel pipeline PgBouncer twin requires the configured CI PgBouncer fixture.');
+const pooledDatabase = `gbrain_test_driver_cancel_${crypto.randomUUID().replace(/-/g, '').slice(0, 12)}`;
+const targets: Array<[string, () => string]> = [];
+if (process.env.DATABASE_URL) targets.push(['direct', () => process.env.DATABASE_URL!]);
+if (pooled && pooledAdmin) targets.push(['pgbouncer', () => { const url = new URL(pooled); url.pathname = `/${pooledDatabase}`; return url.toString(); }]);
+
+beforeAll(async () => {
+ if (!(pooled && pooledAdmin)) return;
+ assertSafeE2eDatabaseUrl(pooledAdmin);
+ const admin = postgres(pooledAdmin, { max: 1, prepare: false, onnotice: () => {} });
+ try { await admin.unsafe(`CREATE DATABASE ${pooledDatabase}`); } finally { await admin.end(); }
+}, 60_000);
+
+afterAll(async () => {
+ if (!(pooled && pooledAdmin)) return;
+ const admin = postgres(pooledAdmin, { max: 1, prepare: false, onnotice: () => {} });
+ try { await admin.unsafe(`DROP DATABASE IF EXISTS ${pooledDatabase} WITH (FORCE)`); } finally { await admin.end(); }
+});
type Outcome = { value: unknown[] } | { error: string } | { hung: true };
const settle = (p: Promise<Iterable<unknown>>): Promise<Outcome> => Promise.race([p.then(value => ({ value: [...value] }), (error: { code?: string }) => ({ error: error?.code ?? String(error) })),
new Promise<{ hung: true }>(resolve => setTimeout(() => resolve({ hung: true }), 5_000))]);
describe.skipIf(targets.length === 0)('vendored driver: a query cancelled while queued behind another on one connection', () => {
- for (const [target, databaseUrl] of targets) for (const prepare of [true, false]) {
+ for (const [target, resolveUrl] of targets) for (const prepare of [true, false]) {
for (const second of ["SELECT 'q2' AS who WHERE false", 'SELECT pg_sleep(0.3) AS slept']) {
test(`the next query reads its own row and the cancelled one rejects 57014 (${target}, prepare ${prepare}, ${second})`, async () => {
+ const databaseUrl = resolveUrl();
assertSafeE2eDatabaseUrl(databaseUrl);
const sql = postgres(databaseUrl, { max: 1, prepare, onnotice: () => {} });
const table = `driver_cancel_${crypto.randomUUID().replace(/-/g, '').slice(0, 12)}`;
diff --git a/test/fixtures/goldens/exports/types.json b/test/fixtures/goldens/exports/types.json
index 6e4610b47b..59aaf8a96e 100644
--- a/test/fixtures/goldens/exports/types.json
+++ b/test/fixtures/goldens/exports/types.json
@@ -1082,7 +1082,7 @@
"getOntology: (entitySlug: string, opts?: OntologyReadOpts) => Promise<OntologyValue[]>",
"getPage: (slug: string, opts?: PageSnapshotOptions) => Promise<Page | null>",
"getPageTimestamps: (slugs: string[]) => Promise<Map<string, Date>>",
- "getPoolDiagnostics: () => { tracked: PoolGaugeSnapshot; poolMax: number | null; poisonedDiscards: number; stuckDiscards: number; buildFailures: number; pool: DriverPoolStats | null; prepare: boolean | null; } | null",
+ "getPoolDiagnostics: () => { tracked: PoolGaugeSnapshot; poolMax: number | null; poisonedDiscards: number; stuckDiscards: number; buildFailures: number; desyncDiscards: number; pool: DriverPoolStats | null; prepare: boolean | null; } | null",
"getRawData: (slug: string, source?: string, opts?: PageReadScope & { includeDeleted?: boolean; }) => Promise<RawData[]>",
"getRecentSalience: (opts: SalienceOpts) => Promise<SalienceResult[]>",
"getSalienceScores: (refs: Array<{ slug: string; source_id: string; }>, opts?: PageReadPolicy) => Promise<Map<string, number>>",
diff --git a/test/fixtures/goldens/postgres-engine-gauge/sequence.json b/test/fixtures/goldens/postgres-engine-gauge/sequence.json
index e12bfbc561..e6c81045ce 100644
--- a/test/fixtures/goldens/postgres-engine-gauge/sequence.json
+++ b/test/fixtures/goldens/postgres-engine-gauge/sequence.json
@@ -3,6 +3,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -20,6 +21,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -37,6 +39,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -54,6 +57,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -71,6 +75,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -88,6 +93,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -105,6 +111,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -122,6 +129,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -139,6 +147,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -156,6 +165,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -173,6 +183,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -190,6 +201,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -207,6 +219,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -224,6 +237,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -241,6 +255,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -258,6 +273,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -275,6 +291,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,
@@ -292,6 +309,7 @@
{
"diagnostics": {
"buildFailures": 0,
+ "desyncDiscards": 0,
"poisonedDiscards": 0,
"pool": null,
"poolMax": 10,