|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// framework#3769: a database endpoint that accepts the TCP connection but never |
| 4 | +// completes the handshake (overloaded instance, half-open firewall, LB mid- |
| 5 | +// failover) makes every query WAIT rather than fail. Left to tarn's default that |
| 6 | +// wait is 30s per query on the request path. SqlDriver now bounds it by default, |
| 7 | +// while leaving a host's own choice alone. |
| 8 | + |
| 9 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 10 | +import net from 'node:net'; |
| 11 | +import { SqlDriver } from './sql-driver.js'; |
| 12 | + |
| 13 | +/** A listener that accepts sockets and then says nothing, ever. */ |
| 14 | +function blackHole() { |
| 15 | + const held: net.Socket[] = []; |
| 16 | + const server = net.createServer((sock) => { sock.on('error', () => {}); held.push(sock); }); |
| 17 | + return { |
| 18 | + listen: () => new Promise<number>((resolve) => { |
| 19 | + server.listen(0, '127.0.0.1', () => resolve((server.address() as net.AddressInfo).port)); |
| 20 | + }), |
| 21 | + close: () => { held.forEach((s) => s.destroy()); server.close(); }, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +const drivers: SqlDriver[] = []; |
| 26 | +const make = (cfg: any) => { const d = new SqlDriver(cfg); drivers.push(d); return d; }; |
| 27 | + |
| 28 | +afterEach(async () => { |
| 29 | + await Promise.all(drivers.splice(0).map((d) => d.disconnect().catch(() => {}))); |
| 30 | +}); |
| 31 | + |
| 32 | +describe('SqlDriver — connection-attempt bound (framework#3769)', () => { |
| 33 | + it('applies a default createTimeoutMillis when the host sets no pool config', () => { |
| 34 | + const d = make({ client: 'pg', connection: 'postgres://u:p@127.0.0.1:1/d' }); |
| 35 | + expect((d as any).knex.client.config.pool.createTimeoutMillis).toBe(10_000); |
| 36 | + }); |
| 37 | + |
| 38 | + it('applies it alongside a host pool config without clobbering min/max', () => { |
| 39 | + const d = make({ |
| 40 | + client: 'pg', |
| 41 | + connection: 'postgres://u:p@127.0.0.1:1/d', |
| 42 | + pool: { min: 0, max: 5 }, |
| 43 | + }); |
| 44 | + const pool = (d as any).knex.client.config.pool; |
| 45 | + expect(pool).toMatchObject({ min: 0, max: 5, createTimeoutMillis: 10_000 }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("leaves a host's explicit createTimeoutMillis alone", () => { |
| 49 | + const d = make({ |
| 50 | + client: 'pg', |
| 51 | + connection: 'postgres://u:p@127.0.0.1:1/d', |
| 52 | + pool: { min: 0, max: 5, createTimeoutMillis: 45_000 }, |
| 53 | + }); |
| 54 | + expect((d as any).knex.client.config.pool.createTimeoutMillis).toBe(45_000); |
| 55 | + }); |
| 56 | + |
| 57 | + it('actually bounds a query against a black-holing endpoint', async () => { |
| 58 | + const bh = blackHole(); |
| 59 | + const port = await bh.listen(); |
| 60 | + try { |
| 61 | + // 700ms so the assertion is decisive: unbounded is 30s. |
| 62 | + const d = make({ |
| 63 | + client: 'pg', |
| 64 | + connection: `postgres://u:p@127.0.0.1:${port}/d`, |
| 65 | + pool: { min: 0, max: 2, createTimeoutMillis: 700 }, |
| 66 | + }); |
| 67 | + const started = Date.now(); |
| 68 | + await expect((d as any).knex.raw('SELECT 1')).rejects.toThrow(); |
| 69 | + expect(Date.now() - started).toBeLessThan(5_000); |
| 70 | + } finally { |
| 71 | + bh.close(); |
| 72 | + } |
| 73 | + }, 20_000); |
| 74 | + |
| 75 | + it('does not disturb a working sqlite connection', async () => { |
| 76 | + const d = make({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 77 | + await expect(d.checkHealth()).resolves.toBe(true); |
| 78 | + }); |
| 79 | +}); |
0 commit comments