Don't reuse closed or mismatched MariaDB clients
grabMariaDBClient reused global.MARIADB_CLIENT whenever the requested config matched global.CONFIG. It never checked that the client was made from that config or was still open. init() sets global.CONFIG before creating the client, so a re-init after a close, or a config change, got back a closed or wrong client. - Remember the cache key each client was created with, and compare that - Wrapping close() drops the client from the cache and unsets global.MARIADB_CLIENT, so the next grab or ensureInit makes a new one - resolveClient goes through grabMariaDBClient instead of repeating the global.CONFIG comparison - Remove the queue test's workaround for this Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { afterEach, expect, test } from "bun:test";
|
||||
import grabMariaDBClient from "./grab-mariadb-client";
|
||||
import setMariaDBClient from "./set-mariadb-client";
|
||||
|
||||
// Clients connect lazily, so none of these open a connection
|
||||
const config = { db_name: "bmt_client_cache", ssl_reject_unauthorized: false };
|
||||
|
||||
const originalConfig = global.CONFIG;
|
||||
|
||||
afterEach(async () => {
|
||||
await global.MARIADB_CLIENT?.close();
|
||||
global.CONFIG = originalConfig;
|
||||
});
|
||||
|
||||
test("reuses the client for the same config", () => {
|
||||
expect(grabMariaDBClient({ config })).toBe(grabMariaDBClient({ config: { ...config } }));
|
||||
});
|
||||
|
||||
test("a closed client is dropped from the cache and the global slot", async () => {
|
||||
setMariaDBClient({ config });
|
||||
const first = global.MARIADB_CLIENT!;
|
||||
|
||||
await first.close();
|
||||
expect(global.MARIADB_CLIENT).toBeUndefined();
|
||||
|
||||
const second = grabMariaDBClient({ config });
|
||||
expect(second).toBeDefined();
|
||||
expect(second).not.toBe(first);
|
||||
});
|
||||
|
||||
test("the global client is not reused for a different config", () => {
|
||||
setMariaDBClient({ config });
|
||||
global.CONFIG = { db_name: "bmt_other" };
|
||||
|
||||
const other = grabMariaDBClient({ config: global.CONFIG });
|
||||
expect(other).not.toBe(global.MARIADB_CLIENT);
|
||||
other?.close();
|
||||
});
|
||||
Reference in New Issue
Block a user