|
| 1 | +import { strict as assert } from 'node:assert'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
| 4 | +import mm from 'mm'; |
| 5 | +import config from './config'; |
| 6 | +import { RDSClient } from '../src/client'; |
| 7 | + |
| 8 | +describe('test/PoolConfig.test.ts', () => { |
| 9 | + const prefix = 'prefix-PoolConfig' + process.version + '-'; |
| 10 | + const table = 'ali-sdk-test-user'; |
| 11 | + let db: RDSClient; |
| 12 | + let index = 0; |
| 13 | + let newConnectionCount = 0; |
| 14 | + before(async () => { |
| 15 | + db = new RDSClient({ |
| 16 | + // test getConnectionConfig |
| 17 | + connectionLimit: 2, |
| 18 | + getConnectionConfig() { |
| 19 | + console.log('get connection config index: %d', ++index); |
| 20 | + return config; |
| 21 | + }, |
| 22 | + }); |
| 23 | + db.pool.on('acquire', conn => { |
| 24 | + console.log('acquire connection %o', conn.threadId); |
| 25 | + }); |
| 26 | + db.pool.on('connection', conn => { |
| 27 | + newConnectionCount++; |
| 28 | + console.log('new connection %o', conn.threadId); |
| 29 | + }); |
| 30 | + db.pool.on('enqueue', () => { |
| 31 | + console.log('Waiting for available connection slot'); |
| 32 | + }); |
| 33 | + db.pool.on('release', conn => { |
| 34 | + console.log('release connection %o', conn.threadId); |
| 35 | + }); |
| 36 | + |
| 37 | + try { |
| 38 | + const sql = await fs.readFile(path.join(__dirname, 'rds_init.sql'), 'utf-8'); |
| 39 | + await db.query(sql); |
| 40 | + } catch (err) { |
| 41 | + console.log('init table error: %s', err); |
| 42 | + } |
| 43 | + await db.query('delete from ?? where name like ?', [ table, prefix + '%' ]); |
| 44 | + }); |
| 45 | + |
| 46 | + after(async () => { |
| 47 | + return await db.end(); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + mm.restore(); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('new RDSClient(options.getConnectionConfig)', () => { |
| 55 | + it('should get connection config from newConnectionConfig()', async () => { |
| 56 | + assert.equal(db.pool.config.connectionConfig.database, undefined); |
| 57 | + assert.equal(index, 1); |
| 58 | + assert.equal((db.pool.config as any).newConnectionConfig().database, 'test'); |
| 59 | + assert.equal(index, 2); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should connect rds success', async () => { |
| 63 | + const rows = await db.query('show tables'); |
| 64 | + // console.log(rows); |
| 65 | + assert(rows); |
| 66 | + assert(Array.isArray(rows)); |
| 67 | + assert.equal(index, 2); |
| 68 | + const results = await Promise.all([ |
| 69 | + db.query('show tables'), |
| 70 | + db.query('show tables'), |
| 71 | + db.query('show tables'), |
| 72 | + ]); |
| 73 | + assert.equal(results.length, 3); |
| 74 | + assert(results[0]); |
| 75 | + assert(Array.isArray(results[0])); |
| 76 | + assert(results[1]); |
| 77 | + assert(Array.isArray(results[1])); |
| 78 | + assert(results[2]); |
| 79 | + assert(Array.isArray(results[2])); |
| 80 | + assert.equal(index, 3); |
| 81 | + assert.equal(newConnectionCount, 2); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments