|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const mysql = require('../..'); |
| 4 | +const test = require('utest'); |
| 5 | +const assert = require('assert'); |
| 6 | +const common = require('../common'); |
| 7 | + |
| 8 | +const poolConfig = common.getConfig(); |
| 9 | + |
| 10 | +const ACQUITE_TIMEOUT = 500; |
| 11 | +const poolWithAcquireTimeout = new mysql.createPool({ |
| 12 | + ...poolConfig, |
| 13 | + acquireTimeout: ACQUITE_TIMEOUT, |
| 14 | + connectionLimit: 2 |
| 15 | +}); |
| 16 | + |
| 17 | +poolWithAcquireTimeout.getConnection(function (err, c1) { |
| 18 | + assert.equal(!!c1, true); |
| 19 | + assert.ifError(err); |
| 20 | + poolWithAcquireTimeout.getConnection(function (err, c2) { |
| 21 | + assert.ifError(err); |
| 22 | + assert.equal(!!c2, true); |
| 23 | + const C3_STARTED_AT = Date.now(); |
| 24 | + poolWithAcquireTimeout.getConnection(function (e3, c3) { |
| 25 | + const C3_DONE_AT = Date.now(); |
| 26 | + |
| 27 | + poolWithAcquireTimeout.releaseConnection(c1); |
| 28 | + |
| 29 | + poolWithAcquireTimeout.getConnection(function (e4, c4) { |
| 30 | + |
| 31 | + test('Pool With Acquire Timeout', { |
| 32 | + 'timeout of pool is full': () => { |
| 33 | + assert.equal(e3 !== null, true); |
| 34 | + assert.equal(!c3, true); |
| 35 | + assert.equal(C3_DONE_AT - C3_STARTED_AT >= ACQUITE_TIMEOUT, true); |
| 36 | + assert.equal(C3_DONE_AT - C3_STARTED_AT < ACQUITE_TIMEOUT * 2, true); |
| 37 | + }, |
| 38 | + 'ok if pool is not full': () => { |
| 39 | + assert.equal(e4 === null, true); |
| 40 | + assert.equal(!!c4, true); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + poolWithAcquireTimeout.releaseConnection(c4); |
| 45 | + }); |
| 46 | + }); |
| 47 | + }); |
| 48 | +}); |
0 commit comments