Merge pull request #2182 from mnolet/fix/test-infra-openInboundDb
fix(test-infra): openInboundDb honors in-memory test DB
This commit is contained in:
@@ -27,6 +27,7 @@ const DEFAULT_HEARTBEAT_PATH = '/workspace/.heartbeat';
|
|||||||
let _inbound: Database | null = null;
|
let _inbound: Database | null = null;
|
||||||
let _outbound: Database | null = null;
|
let _outbound: Database | null = null;
|
||||||
let _heartbeatPath: string = DEFAULT_HEARTBEAT_PATH;
|
let _heartbeatPath: string = DEFAULT_HEARTBEAT_PATH;
|
||||||
|
let _testMode = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Avoid all cached db reads; open inbound.db read-only with mmap and page cache disabled.
|
* Avoid all cached db reads; open inbound.db read-only with mmap and page cache disabled.
|
||||||
@@ -42,6 +43,13 @@ let _heartbeatPath: string = DEFAULT_HEARTBEAT_PATH;
|
|||||||
* Cost is microseconds per query, so safe for universal use.
|
* Cost is microseconds per query, so safe for universal use.
|
||||||
*/
|
*/
|
||||||
export function openInboundDb(): Database {
|
export function openInboundDb(): Database {
|
||||||
|
// In test mode return a thin wrapper over the in-memory singleton.
|
||||||
|
// Callers do try/finally { db.close() } — the wrapper no-ops close()
|
||||||
|
// so the singleton survives for the rest of the test.
|
||||||
|
if (_testMode && _inbound) {
|
||||||
|
const db = _inbound;
|
||||||
|
return { prepare: (sql: string) => db.prepare(sql), exec: (sql: string) => db.exec(sql), close: () => {} } as unknown as Database;
|
||||||
|
}
|
||||||
const db = new Database(DEFAULT_INBOUND_PATH, { readonly: true });
|
const db = new Database(DEFAULT_INBOUND_PATH, { readonly: true });
|
||||||
db.exec('PRAGMA busy_timeout = 5000');
|
db.exec('PRAGMA busy_timeout = 5000');
|
||||||
db.exec('PRAGMA mmap_size = 0');
|
db.exec('PRAGMA mmap_size = 0');
|
||||||
@@ -170,6 +178,7 @@ export function clearStaleProcessingAcks(): void {
|
|||||||
|
|
||||||
/** For tests — creates in-memory DBs with the session schemas. */
|
/** For tests — creates in-memory DBs with the session schemas. */
|
||||||
export function initTestSessionDb(): { inbound: Database; outbound: Database } {
|
export function initTestSessionDb(): { inbound: Database; outbound: Database } {
|
||||||
|
_testMode = true;
|
||||||
_inbound = new Database(':memory:');
|
_inbound = new Database(':memory:');
|
||||||
_inbound.exec('PRAGMA foreign_keys = ON');
|
_inbound.exec('PRAGMA foreign_keys = ON');
|
||||||
_inbound.exec(`
|
_inbound.exec(`
|
||||||
@@ -246,6 +255,7 @@ export function initTestSessionDb(): { inbound: Database; outbound: Database } {
|
|||||||
export function closeSessionDb(): void {
|
export function closeSessionDb(): void {
|
||||||
_inbound?.close();
|
_inbound?.close();
|
||||||
_inbound = null;
|
_inbound = null;
|
||||||
|
_testMode = false;
|
||||||
_outbound?.close();
|
_outbound?.close();
|
||||||
_outbound = null;
|
_outbound = null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user