|
| 1 | +import type { RowDataPacket } from '../../../../index.js'; |
| 2 | +import { assert, describe, it, skip } from 'poku'; |
| 3 | +import { createConnection } from '../../../../promise.js'; |
| 4 | +import { config } from '../../../common.test.mjs'; |
| 5 | + |
| 6 | +if (!('asyncDispose' in Symbol)) { |
| 7 | + skip('Symbol.asyncDispose is not supported in this runtime'); |
| 8 | +} |
| 9 | + |
| 10 | +await describe('PromiseConnection should implement Symbol.asyncDispose', async () => { |
| 11 | + await using conn = await createConnection(config); |
| 12 | + |
| 13 | + it('should be a function', () => { |
| 14 | + assert.strictEqual(typeof conn[Symbol.asyncDispose], 'function'); |
| 15 | + }); |
| 16 | +}); |
| 17 | + |
| 18 | +await describe('asyncDispose should end the connection', async () => { |
| 19 | + await using conn = await createConnection(config); |
| 20 | + const [rows] = await conn.query<RowDataPacket[]>('SELECT 1'); |
| 21 | + |
| 22 | + await conn[Symbol.asyncDispose](); |
| 23 | + |
| 24 | + it('should have received the query result', () => { |
| 25 | + assert.deepStrictEqual(rows, [{ 1: 1 }]); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should have closed the connection', () => { |
| 29 | + // @ts-expect-error: internal access |
| 30 | + assert.strictEqual(conn.connection._closing, true); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +await describe('asyncDispose should handle manual destroy before asyncDispose on connection', async () => { |
| 35 | + await using conn = await createConnection(config); |
| 36 | + const [rows] = await conn.query<RowDataPacket[]>('SELECT 1'); |
| 37 | + |
| 38 | + conn.destroy(); |
| 39 | + await conn[Symbol.asyncDispose](); |
| 40 | + |
| 41 | + it('should have received the query result', () => { |
| 42 | + assert.deepStrictEqual(rows, [{ 1: 1 }]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should have closed the connection', () => { |
| 46 | + // @ts-expect-error: internal access |
| 47 | + assert.strictEqual(conn.connection._closing, true); |
| 48 | + }); |
| 49 | +}); |
0 commit comments