Skip to content

Commit 443e915

Browse files
author
denshikov-vovan
committed
[ #154578064 ] - some updates
1 parent ceb225c commit 443e915

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

tests/helpers.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as net from 'net';
22
import {Client, ConnectionConfig} from 'pg';
33
import {buildLogger, Logger} from '../lib/util';
4-
import {ConnectionPool, PoolOptions} from '../lib/Pool';
4+
import {CLOSED_EVENT, ConnectionPool, PoolOptions} from '../lib/Pool';
55
import {settings} from './settings';
66

7-
export const PROXY_SERVER_PORT = 3000;
7+
export const PROXY_SERVER_PORT = 3000;
8+
export const FAKE_PG_SERVER_PORT = 3001;
89

910
export function createNewPool(poolOptions: PoolOptions = {}, connectionOptions: ConnectionConfig = {}, logger?: Logger): ConnectionPool {
1011
const poolSettings = Object.assign({}, settings.pool, poolOptions);
@@ -22,6 +23,39 @@ export function createClient(pool: ConnectionPool): Promise<Client> {
2223
return new Promise((resolve, reject) => pool.acquire((err, client) => err ? reject(err) : resolve(client)));
2324
}
2425

26+
export function removeAllClientAndShutdownPool(pool: ConnectionPool): Promise<void> {
27+
const removeClient = (client: Client) => {
28+
const idleTimeout = (pool as any).idle.get(client);
29+
30+
if (idleTimeout) {
31+
clearTimeout(idleTimeout);
32+
(pool as any).idle.delete(client);
33+
}
34+
35+
(pool as any).clients.delete(client);
36+
37+
if ((client as any).connection) {
38+
(client as any).connection.stream.destroy();
39+
} else {
40+
client.end();
41+
}
42+
};
43+
44+
const shutdownPool = (): Promise<void> => {
45+
return new Promise((resolve, reject) => {
46+
pool.shutdown(error => {
47+
error ? reject(error) : resolve();
48+
});
49+
});
50+
};
51+
52+
for (let client of (pool as any).clients.keys()) {
53+
removeClient(client);
54+
}
55+
56+
return shutdownPool();
57+
}
58+
2559
export function pgProxyServer(timeout: number, cb): net.Server {
2660
const server = net.createServer(proxySocket => {
2761
const pgSocket = new net.Socket();
@@ -56,3 +90,11 @@ export function pgProxyServer(timeout: number, cb): net.Server {
5690

5791
return server;
5892
}
93+
94+
export function fakePgServer(cb: Function): net.Server {
95+
const server = net.createServer();
96+
97+
server.listen(FAKE_PG_SERVER_PORT, cb);
98+
99+
return server;
100+
}

tests/poolConnectionTimeout.spec.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import {expect} from 'chai';
22

33
import {ConnectionError} from '../lib/errors';
4-
import {createNewPool, pgProxyServer, PROXY_SERVER_PORT} from './helpers';
4+
import {createNewPool, fakePgServer, removeAllClientAndShutdownPool, FAKE_PG_SERVER_PORT} from './helpers';
55

6-
const connectionOptions = {port: PROXY_SERVER_PORT};
6+
const connectionOptions = {port: FAKE_PG_SERVER_PORT};
77

88
let server;
99

1010
describe('Pool connection timeout;', () => {
1111
before(done => {
12-
server = pgProxyServer(250, done);
12+
server = fakePgServer(done);
1313
});
1414

1515
after(done => {
@@ -19,17 +19,21 @@ describe('Pool connection timeout;', () => {
1919
it('should callback with an error if timeout is passed', done => {
2020
const pool = createNewPool({connectionTimeout: 150}, connectionOptions);
2121

22-
pool.acquire((err, client) => {
22+
pool.acquire(async (err, client) => {
2323
try {
2424
expect(err).to.be.an.instanceof(ConnectionError);
2525
expect(err.message).to.contain('Connection request has timed out');
2626
expect(pool.idleCount).to.equal(0);
2727
expect(pool.totalCount).to.equal(1);
2828
expect(client).to.be.undefined;
2929

30-
pool.shutdown(done);
30+
await removeAllClientAndShutdownPool(pool);
31+
32+
done();
3133
} catch (err) {
32-
pool.shutdown(() => done(err));
34+
removeAllClientAndShutdownPool(pool)
35+
.then( () => done(err))
36+
.catch(() => done(err));
3337
}
3438
});
3539
});
@@ -58,27 +62,33 @@ describe('Pool connection timeout;', () => {
5862
expect(pool.idleCount).to.equal(0);
5963
expect(pool.totalCount).to.equal(15);
6064

61-
pool.shutdown(done);
65+
await removeAllClientAndShutdownPool(pool);
66+
67+
done();
6268
} catch (err) {
63-
pool.shutdown(() => done(err));
69+
removeAllClientAndShutdownPool(pool)
70+
.then( () => done(err))
71+
.catch(() => done(err));
6472
}
6573
});
6674

6775
it('should timeout on checkout of used connection', done => {
68-
const pool = createNewPool({connectionTimeout: 400, maxSize: 1}, connectionOptions);
76+
const pool = createNewPool({connectionTimeout: 400, maxSize: 1});
77+
6978
try {
7079
pool.acquire((err, client) => {
7180
expect(err).to.be.undefined;
7281
expect(client).to.not.be.undefined;
7382
expect(pool.totalCount).to.equal(1);
7483

75-
pool.acquire((err, client) => {
84+
pool.acquire(async (err, client) => {
7685
expect(err).to.be.an.instanceof(ConnectionError);
7786
expect(err.message).to.contain('Connection request has timed out');
7887
expect(client).to.be.undefined;
7988
expect(pool.totalCount).to.equal(1);
8089

8190
(pool as any).clients.entries().next().value[0].release();
91+
8292
pool.shutdown(done);
8393
});
8494
});
@@ -88,7 +98,7 @@ describe('Pool connection timeout;', () => {
8898
});
8999

90100
it('should timeout on query if all clients are busy', done => {
91-
const pool = createNewPool({connectionTimeout: 400, maxSize: 1}, connectionOptions);
101+
const pool = createNewPool({connectionTimeout: 400, maxSize: 1});
92102

93103
pool.acquire((err, client) => {
94104
expect(err).to.be.undefined;
@@ -109,7 +119,7 @@ describe('Pool connection timeout;', () => {
109119
});
110120

111121
it('should recover from timeout errors', done => {
112-
const pool = createNewPool({connectionTimeout: 400, maxSize: 1}, connectionOptions);
122+
const pool = createNewPool({connectionTimeout: 400, maxSize: 1});
113123

114124
pool.acquire((err, client) => {
115125
expect(err).to.be.undefined;

0 commit comments

Comments
 (0)