Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test: pipelining and reserved-transaction pins expect BEGIN READ WRITE (#7183)
garrytanbcec262c9edc
diff --git a/test/e2e/pgbouncer-transaction-pipeline-6930.test.ts b/test/e2e/pgbouncer-transaction-pipeline-6930.test.tsindex 2706f5146a..13d5904a24 100644--- a/test/e2e/pgbouncer-transaction-pipeline-6930.test.ts+++ b/test/e2e/pgbouncer-transaction-pipeline-6930.test.ts@@ -97,7 +97,7 @@ describe.skipIf(!(pooled && pooledAdmin))('#6930 transaction pipelining throughawait engine.executeRaw('SELECT 1 AS bounded_read', [], { timeoutMs: 5000 });timedOut = await engine.executeRaw('SELECT pg_sleep(2) AS slept', [], { timeoutMs: 200 }).then(() => 'none', (e: { code?: string }) => e.code);});- expect(sentBeforeAnswer(records, /^BEGIN; SET LOCAL statement_timeout|^SELECT 1 AS bounded_read$|^COMMIT$/, 3)).toEqual([true, true]);+ expect(sentBeforeAnswer(records, /^BEGIN READ WRITE; SET LOCAL statement_timeout|^SELECT 1 AS bounded_read$|^COMMIT$/, 3)).toEqual([true, true]);expect(timedOut).toBe('57014');}, 60_000);@@ -148,7 +148,7 @@ describe.skipIf(!(pooled && pooledAdmin))('#6930 transaction pipelining throughawait engine.withReservedConnection(conn => (conn as unknown as { transaction<R>(run: (tx: PostgresEngine) => Promise<R>): Promise<R> }).transaction(tx => Promise.all([tx.executeRaw('SELECT 1 AS reserved_body'), tx.executeRaw('SELECT 2 AS reserved_body')])));});- const begin = records.find(r => /^begin$/i.test(r.sql))!;+ const begin = records.find(r => /^begin read write$/i.test(r.sql))!;const first = records.find(r => /AS reserved_body$/.test(r.sql))!;expect(first.conn).toBe(begin.conn);expect(first.t >= end(begin)).toBe(true);@@ -186,7 +186,7 @@ describe.skipIf(!(pooled && pooledAdmin))('GBRA-156: a transaction\'s first stat"SELECT current_setting('gbrain.persistence_protocol', true) AS protocol, txid_current()::text AS xid, pg_backend_pid() AS pid /* after declaration */");}, { pipelineFirst: isPersistenceDeclaration });});- const begin = records.find(r => /^begin\s*$/i.test(r.sql))!;+ const begin = records.find(r => /^begin read write$/i.test(r.sql))!;const declared = records.find(r => r.sql === declaration)!;const after = records.find(r => /after declaration \*\/$/.test(r.sql))!;expect(declared.t < end(begin)).toBe(true);@@ -201,7 +201,7 @@ describe.skipIf(!(pooled && pooledAdmin))('GBRA-156: a transaction\'s first statawait tx.executeRaw(`DELETE FROM ${table} WHERE n=0`);}, { pipelineFirst: isPersistenceDeclaration });});- const begin = records.find(r => /^begin\s*$/i.test(r.sql))!;+ const begin = records.find(r => /^begin read write$/i.test(r.sql))!;const insert = records.find(r => /refused first \*\/$/.test(r.sql))!;expect(insert.t >= end(begin)).toBe(true);}, 60_000);@@ -276,7 +276,7 @@ describe.skipIf(!(pooled && pooledAdmin))('GBRA-156: a transaction\'s first statawait tx.executeRaw('SELECT 1 AS reserved_after_declaration');}, { pipelineFirst: isPersistenceDeclaration }));});- const begin = records.find(r => /^begin$/i.test(r.sql))!;+ const begin = records.find(r => /^begin read write$/i.test(r.sql))!;const declared = records.find(r => r.sql === declaration)!;const after = records.find(r => /AS reserved_after_declaration$/.test(r.sql))!;expect([declared.conn, after.conn]).toEqual([begin.conn, begin.conn]);@@ -331,7 +331,7 @@ describe.skipIf(!(pooled && pooledAdmin))('GBRA-156: a transaction\'s first statconst records = await traced('first-off', 1, async engine => {await engine.transaction(tx => declarePersistenceProtocol(tx), { pipelineFirst: isPersistenceDeclaration });}, { GBRAIN_PG_TXN_PIPELINE: '0' });- const begin = records.find(r => /^begin\s*$/i.test(r.sql))!;+ const begin = records.find(r => /^begin read write$/i.test(r.sql))!;const declared = records.find(r => r.sql === declaration)!;expect(declared.t >= end(begin)).toBe(true);}, 60_000);@@ -343,7 +343,7 @@ describe.skipIf(!(pooled && pooledAdmin))('GBRA-156: a transaction\'s first statfailed = await engine.executeRaw('SELECT 1/0 AS bounded_failure', [], { timeoutMs: 5000 }).then(() => 'none', (e: { code?: string }) => e.code);next = await engine.sql.unsafe("SELECT current_setting('statement_timeout') AS timeout, now() = statement_timestamp() AS fresh") as unknown as typeof next;});- expect(sentBeforeAnswer(records, /^BEGIN; SET LOCAL statement_timeout|^SELECT 1\/0 AS bounded_failure$|^COMMIT$/, 3)).toEqual([true, true]);+ expect(sentBeforeAnswer(records, /^BEGIN READ WRITE; SET LOCAL statement_timeout|^SELECT 1\/0 AS bounded_failure$|^COMMIT$/, 3)).toEqual([true, true]);expect(failed).toBe('22012');expect(next.map(r => r.fresh)).toEqual([true]);expect(next[0]!.timeout).not.toBe('5s');diff --git a/test/pathological/pg-round-trips-6930.test.ts b/test/pathological/pg-round-trips-6930.test.tsindex 15a64b15d9..bcafcd2bcb 100644--- a/test/pathological/pg-round-trips-6930.test.ts+++ b/test/pathological/pg-round-trips-6930.test.ts@@ -165,7 +165,7 @@ describe('#6930 unit: scripted server', () => {const server = await serverFor();const engine = await engineOn(server.url('?prepare=false'), 1);await engine.executeRaw('SELECT 1 AS bounded', [], { timeoutMs: 5000 });- const sent = server.arrivals.filter(a => /^BEGIN; SET LOCAL statement_timeout|^SELECT 1 AS bounded$|^COMMIT$/.test(a.sql));+ const sent = server.arrivals.filter(a => /^BEGIN READ WRITE; SET LOCAL statement_timeout|^SELECT 1 AS bounded$|^COMMIT$/.test(a.sql));expect(sent.map(a => a.unanswered)).toEqual([0, 1, 2]);}));diff --git a/test/persistence-pipeline-first.test.ts b/test/persistence-pipeline-first.test.tsindex cd1bc45384..a5fad0acec 100644--- a/test/persistence-pipeline-first.test.ts+++ b/test/persistence-pipeline-first.test.ts@@ -12,6 +12,7 @@ import { declarePublication } from '../src/core/persistence/publication-deadlineimport type { BrainEngine } from '../src/core/engine.ts';import type postgres from '#postgres';import { reservedTransactions } from '../src/core/postgres-engine/reserved-transactions.ts';+import { READ_WRITE_BEGIN } from '../src/core/postgres-engine/read-write-transactions.ts';import { composablePostgresTransaction } from '../src/core/page-state/transactions.ts';function recorder(kind = 'postgres'): { engine: BrainEngine; sent: string[] } {@@ -102,7 +103,7 @@ function lazyReserved(beginFails: boolean) {if (loads !== undefined) self.strings = [loads];const text = self.strings.reduce((a, s, i) => a + '$' + i + s);sent.push({ text, beginAnswered });- if (text === 'begin') setTimeout(() => { beginAnswered = true; beginFails ? self.reject(Object.assign(new Error('no server connection available'), { code: '08P01' })) : resolve(Object.assign([], { command: 'BEGIN' })); }, 20);+ if (text === READ_WRITE_BEGIN) setTimeout(() => { beginAnswered = true; beginFails ? self.reject(Object.assign(new Error('no server connection available'), { code: '08P01' })) : resolve(Object.assign([], { command: 'BEGIN' })); }, 20);else resolve(Object.assign([], { command: text.toUpperCase() }));},execute: () => { if (!q.executed) { q.executed = true; void Promise.resolve().then(() => q.handler(q)); } return q; },@@ -126,7 +127,7 @@ describe('reserved transactions: the declaration goes out with BEGIN', () => {test('only the accepted declaration is sent before BEGIN\'s answer; the next statement and COMMIT wait', async () => {const { reserved, sent } = lazyReserved(false);await beginFirst(reserved, async sql => { await sql.unsafe(DECLARATION); await sql.unsafe('insert into t values (1)'); });- expect(sent).toEqual([{ text: 'begin', beginAnswered: false }, { text: DECLARATION, beginAnswered: false },+ expect(sent).toEqual([{ text: READ_WRITE_BEGIN, beginAnswered: false }, { text: DECLARATION, beginAnswered: false },{ text: 'insert into t values (1)', beginAnswered: true }, { text: 'commit', beginAnswered: true }]);});@@ -140,7 +141,7 @@ describe('reserved transactions: the declaration goes out with BEGIN', () => {}).then(() => null, (e: { code?: string }) => e.code);expect(error).toBe('08P01');expect(swallowed).toBe('08P01');- expect(sent.map(s => s.text).filter(t => t !== 'rollback')).toEqual(['begin', DECLARATION]);+ expect(sent.map(s => s.text).filter(t => t !== 'rollback')).toEqual([READ_WRITE_BEGIN, DECLARATION]);});test('a write first, a fragment argument and a savepoint all wait for BEGIN\'s answer', async () => {@@ -151,7 +152,7 @@ describe('reserved transactions: the declaration goes out with BEGIN', () => {]) {const { reserved, sent } = lazyReserved(false);await beginFirst(reserved, body);- expect(sent[0]).toEqual({ text: 'begin', beginAnswered: false });+ expect(sent[0]).toEqual({ text: READ_WRITE_BEGIN, beginAnswered: false });expect(sent.slice(1).every(s => s.beginAnswered)).toBe(true);}});@@ -168,7 +169,7 @@ describe('reserved transactions: the declaration goes out with BEGIN', () => {test(`${name} after the declaration waits for BEGIN's answer`, async () => {const { reserved, sent } = lazyReserved(false);await beginFirst(reserved, async sql => { await sql.unsafe(DECLARATION); await reach(sql, child => child`insert into t values (${1})`); });- expect(sent.slice(0, 2)).toEqual([{ text: 'begin', beginAnswered: false }, { text: DECLARATION, beginAnswered: false }]);+ expect(sent.slice(0, 2)).toEqual([{ text: READ_WRITE_BEGIN, beginAnswered: false }, { text: DECLARATION, beginAnswered: false }]);expect(sent.find(s => s.text === 'insert into t values ($1)')).toEqual({ text: 'insert into t values ($1)', beginAnswered: true });expect(sent.slice(2).every(s => s.beginAnswered)).toBe(true);});@@ -186,7 +187,7 @@ describe('reserved transactions: the declaration goes out with BEGIN', () => {}).then(() => null, (e: { code?: string }) => e.code);expect(error).toBe('08P01');expect(swallowed).toBe('08P01');- expect(sent.map(s => s.text).filter(t => t !== 'rollback')).toEqual(['begin', DECLARATION]);+ expect(sent.map(s => s.text).filter(t => t !== 'rollback')).toEqual([READ_WRITE_BEGIN, DECLARATION]);});}@@ -201,7 +202,7 @@ describe('reserved transactions: the declaration goes out with BEGIN', () => {swallowed = await sql.file('refused.sql').then(() => 'sent', (e: { code?: string }) => e.code);}).catch(() => undefined);expect(swallowed).toBe('08P01');- expect(refused.sent.map(s => s.text).filter(t => t !== 'rollback')).toEqual(['begin', DECLARATION]);+ expect(refused.sent.map(s => s.text).filter(t => t !== 'rollback')).toEqual([READ_WRITE_BEGIN, DECLARATION]);});});diff --git a/test/read-transaction-early-result.test.ts b/test/read-transaction-early-result.test.tsindex 68afad5736..db273f06f1 100644--- a/test/read-transaction-early-result.test.ts+++ b/test/read-transaction-early-result.test.ts@@ -164,7 +164,7 @@ describe('search read transactions answer before COMMIT', () => {srv.releaseCommit();expect(await settledWithin(next, 1_000)).toEqual([]);- expect(textsAfter(srv.sent, from)).toEqual(['1:begin', "1:select set_config('statement_timeout',", '1:select 41', '1:commit', '1:select 2']);+ expect(textsAfter(srv.sent, from)).toEqual(['1:begin read', "1:select set_config('statement_timeout',", '1:select 41', '1:commit', '1:select 2']);expect(engine.getPoolDiagnostics()!.tracked.tx).toBe(0);}));@@ -202,7 +202,7 @@ describe('search read transactions answer before COMMIT', () => {const from = srv.sent.length;await expect(engine.keywordBoundedRead(async () => { throw new Error('read sentinel'); })).rejects.toThrow('read sentinel');expect(await engine.executeRaw(ANSWER)).toEqual([{ answer: 42 }]);- expect(textsAfter(srv.sent, from)).toEqual(['1:begin', "1:select set_config('statement_timeout',", '1:rollback', '1:select 41']);+ expect(textsAfter(srv.sent, from)).toEqual(['1:begin read', "1:select set_config('statement_timeout',", '1:rollback', '1:select 41']);}));test("a caller's transaction still waits for its COMMIT", () => withEnv(ENV, async () => {@@ -219,6 +219,6 @@ describe('search read transactions answer before COMMIT', () => {const { srv, engine } = await fixture();const from = srv.sent.length;expect(await engine.transaction(tx => tx.keywordBoundedRead(db => db.executeRaw(ANSWER)))).toEqual([{ answer: 42 }]);- expect(textsAfter(srv.sent, from).filter(s => !/current_setting|set_config/.test(s))).toEqual(['1:begin', '1:savepoint "s0"', '1:select 41', '1:commit']);+ expect(textsAfter(srv.sent, from).filter(s => !/current_setting|set_config/.test(s))).toEqual(['1:begin read', '1:savepoint "s0"', '1:select 41', '1:commit']);}));});diff --git a/test/reserved-transactions-begin.test.ts b/test/reserved-transactions-begin.test.tsindex bf3a55b480..de131a1d65 100644--- a/test/reserved-transactions-begin.test.ts+++ b/test/reserved-transactions-begin.test.ts@@ -6,6 +6,7 @@import { describe, expect, test } from 'bun:test';import type postgres from '#postgres';import { reservedTransactions } from '../src/core/postgres-engine/reserved-transactions.ts';+import { READ_WRITE_BEGIN } from '../src/core/postgres-engine/read-write-transactions.ts';/** A reserved connection that records each statement with whether BEGIN had been answered when it was sent. */function fakeReserved(maxPipeline: number, beginFails: boolean) {@@ -13,7 +14,7 @@ function fakeReserved(maxPipeline: number, beginFails: boolean) {let beginAnswered = false;const statement = (text: string) => {sent.push({ text, beginAnswered });- const result: Promise<unknown> = text === 'begin'+ const result: Promise<unknown> = text === READ_WRITE_BEGIN? new Promise((resolve, reject) => setTimeout(() => {beginAnswered = true;beginFails ? reject(Object.assign(new Error('no server connection available'), { code: '08P01' })) : resolve(Object.assign([], { command: 'BEGIN' }));@@ -42,12 +43,12 @@ describe('reserved transactions and BEGIN', () => {test('on a non-pipelining pool the body is sent only after BEGIN is answered', async () => {const { reserved, sent } = fakeReserved(0, false);expect(await begin(reserved)).toBe('ok');- expect(sent).toEqual([{ text: 'begin', beginAnswered: false }, { text: 'insert into t values (1)', beginAnswered: true }, { text: 'commit', beginAnswered: true }]);+ expect(sent).toEqual([{ text: READ_WRITE_BEGIN, beginAnswered: false }, { text: 'insert into t values (1)', beginAnswered: true }, { text: 'commit', beginAnswered: true }]);});test('on a pipelining pool BEGIN still goes out with the body', async () => {const { reserved, sent } = fakeReserved(100, false);expect(await begin(reserved)).toBe('ok');- expect(sent.slice(0, 2)).toEqual([{ text: 'begin', beginAnswered: false }, { text: 'insert into t values (1)', beginAnswered: false }]);+ expect(sent.slice(0, 2)).toEqual([{ text: READ_WRITE_BEGIN, beginAnswered: false }, { text: 'insert into t values (1)', beginAnswered: false }]);});});