|
| 1 | +import { assert, assertEquals, assertRejects } from "../deps/std/assert.ts"; |
| 2 | +import { createDefaultPool } from "./default_pool.ts"; |
| 3 | + |
| 4 | +class FakeConnection implements Disposable { |
| 5 | + #isClosed = false; |
| 6 | + isClosed() { |
| 7 | + return this.#isClosed; |
| 8 | + } |
| 9 | + [Symbol.dispose]() { |
| 10 | + if (this.#isClosed) { |
| 11 | + throw new Error("Already closed"); |
| 12 | + } |
| 13 | + this.#isClosed = true; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +Deno.test({ |
| 18 | + name: "DefaultPool", |
| 19 | + permissions: "none", |
| 20 | + fn: async () => { |
| 21 | + const openConnections: Array<FakeConnection> = []; |
| 22 | + const pool = createDefaultPool({ |
| 23 | + acquire: () => { |
| 24 | + const connection = new FakeConnection(); |
| 25 | + openConnections.push(connection); |
| 26 | + return Promise.resolve(connection); |
| 27 | + }, |
| 28 | + maxConnections: 2, |
| 29 | + }); |
| 30 | + assertEquals(openConnections, []); |
| 31 | + |
| 32 | + const signal = AbortSignal.timeout(200); |
| 33 | + |
| 34 | + const conn1 = await pool.acquire(signal); |
| 35 | + assertEquals(openConnections, [conn1]); |
| 36 | + assert(openConnections.every((x) => !x.isClosed())); |
| 37 | + assert(!signal.aborted); |
| 38 | + |
| 39 | + const conn2 = await pool.acquire(signal); |
| 40 | + assertEquals(openConnections, [conn1, conn2]); |
| 41 | + assert(!conn2.isClosed()); |
| 42 | + assert(openConnections.every((x) => !x.isClosed())); |
| 43 | + assert(!signal.aborted); |
| 44 | + |
| 45 | + { |
| 46 | + // Tests timeout handling |
| 47 | + await assertRejects( |
| 48 | + () => pool.acquire(signal), |
| 49 | + "Intentionally aborted", |
| 50 | + ); |
| 51 | + assert(signal.aborted); |
| 52 | + assertEquals(openConnections, [conn1, conn2]); |
| 53 | + assert(openConnections.every((x) => !x.isClosed())); |
| 54 | + } |
| 55 | + |
| 56 | + { |
| 57 | + // Tests `release()` |
| 58 | + pool.release(conn2); |
| 59 | + assertEquals(openConnections, [conn1, conn2]); |
| 60 | + |
| 61 | + const conn = await pool.acquire(new AbortController().signal); |
| 62 | + assert(conn === conn2, "A new connection should not be created"); |
| 63 | + assertEquals(openConnections, [conn1, conn2]); |
| 64 | + } |
| 65 | + |
| 66 | + { |
| 67 | + // `Pool#acquire` should wait for an active connection to be released. |
| 68 | + const signal = AbortSignal.timeout(3_000); |
| 69 | + const promise = pool.acquire(signal); |
| 70 | + setTimeout(() => { |
| 71 | + pool.release(conn1); |
| 72 | + }, 50); |
| 73 | + const conn = await promise; |
| 74 | + assert(conn === conn1, "A new connection should not be created"); |
| 75 | + assertEquals(openConnections, [conn1, conn2]); |
| 76 | + assert(!signal.aborted); |
| 77 | + } |
| 78 | + |
| 79 | + { |
| 80 | + // `Pool#close` closes all connections |
| 81 | + pool.close(); |
| 82 | + assert(openConnections.every((x) => x.isClosed())); |
| 83 | + } |
| 84 | + }, |
| 85 | +}); |
0 commit comments