Skip to content

Commit ca7fee0

Browse files
authored
Merge pull request #6 from herculesinc/ftr/new_pool_test
[ #154578064 ] - updated tests to work on win os
2 parents 299e010 + 4286b1a commit ca7fee0

File tree

11 files changed

+93
-155
lines changed

11 files changed

+93
-155
lines changed

tests/connection.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ describe('List query tests;', function () {
358358
});
359359
});
360360

361-
it('Unnamed list queries should aggregte into undefined key', () => {
361+
it('Unnamed list queries should aggregate into undefined key', () => {
362362
return new Database(settings).connect().then((session) => {
363363
return prepareDatabase(session).then(() => {
364364
const query1: ListResultQuery<User> = {
@@ -388,7 +388,7 @@ describe('List query tests;', function () {
388388
});
389389
});
390390

391-
it('List query with a handler should be parsed using custom parsing mehtod', () => {
391+
it('List query with a handler should be parsed using custom parsing method', () => {
392392
return new Database(settings).connect().then((session) => {
393393
return prepareDatabase(session).then(() => {
394394
const query: ListResultQuery<number> = {
@@ -559,7 +559,7 @@ describe('Mixed query tests;', function () {
559559
// ================================================================================================
560560
describe('Parametrized query tests;', function () {
561561

562-
it('Object query parametrized with number should retrive correct row', () => {
562+
it('Object query parametrized with number should retrieve correct row', () => {
563563
return new Database(settings).connect().then((session) => {
564564
return prepareDatabase(session).then(() => {
565565
const query: SingleResultQuery<User> = {
@@ -578,7 +578,7 @@ describe('Parametrized query tests;', function () {
578578
});
579579
});
580580

581-
it('Object query parametrized with string should retrive correct row', () => {
581+
it('Object query parametrized with string should retrieve correct row', () => {
582582
return new Database(settings).connect().then((session) => {
583583
return prepareDatabase(session).then(() => {
584584
const query: SingleResultQuery<User> = {
@@ -597,7 +597,7 @@ describe('Parametrized query tests;', function () {
597597
});
598598
});
599599

600-
it('Object query parametrized with unsafe string should retrive correct row', () => {
600+
it('Object query parametrized with unsafe string should retrieve correct row', () => {
601601
return new Database(settings).connect().then((session) => {
602602
return prepareDatabase(session).then(() => {
603603
const query: SingleResultQuery<User> = {
@@ -787,7 +787,7 @@ describe('Session lifecycle tests;', function () {
787787
});
788788
});
789789

790-
it('Commiting a transaction should update the data in the database', () => {
790+
it('Committing a transaction should update the data in the database', () => {
791791
const database = new Database(settings);
792792
return database.connect().then((session) => {
793793
return prepareDatabase(session).then(() => {
@@ -1129,7 +1129,7 @@ describe('Error condition tests;', function () {
11291129
});
11301130
});
11311131

1132-
it('Executing a query after commiting a transaction should throw an error', () => {
1132+
it('Executing a query after committing a transaction should throw an error', () => {
11331133
const database = new Database(settings);
11341134

11351135
return database.connect({startTransaction: true}).then((session) => {

tests/helpers.ts

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {buildLogger, Logger} from '../lib/util';
44
import {ConnectionPool, PoolOptions} from '../lib/Pool';
55
import {settings} from './settings';
66

7-
export const PROXY_SERVER_PORT = 3000;
7+
export const FAKE_PG_SERVER_PORT = 3001;
88

99
export function createNewPool(poolOptions: PoolOptions = {}, connectionOptions: ConnectionConfig = {}, logger?: Logger): ConnectionPool {
1010
const poolSettings = Object.assign({}, settings.pool, poolOptions);
@@ -22,37 +22,43 @@ export function createClient(pool: ConnectionPool): Promise<Client> {
2222
return new Promise((resolve, reject) => pool.acquire((err, client) => err ? reject(err) : resolve(client)));
2323
}
2424

25-
export function pgProxyServer(timeout: number, cb): net.Server {
26-
const server = net.createServer(proxySocket => {
27-
const pgSocket = new net.Socket();
28-
29-
pgSocket.connect(settings.connection.port, settings.connection.host);
30-
31-
proxySocket.on('data', data => {
32-
setTimeout(() => {
33-
const flushed = pgSocket.write(data);
34-
35-
!flushed && proxySocket.pause();
36-
}, timeout);
25+
export function removeAllClientAndShutdownPool(pool: ConnectionPool): Promise<void> {
26+
const removeClient = (client: Client) => {
27+
const idleTimeout = (pool as any).idle.get(client);
28+
29+
if (idleTimeout) {
30+
clearTimeout(idleTimeout);
31+
(pool as any).idle.delete(client);
32+
}
33+
34+
(pool as any).clients.delete(client);
35+
36+
if ((client as any).connection) {
37+
(client as any).connection.stream.destroy();
38+
} else {
39+
client.end();
40+
}
41+
};
42+
43+
const shutdownPool = (): Promise<void> => {
44+
return new Promise((resolve, reject) => {
45+
pool.shutdown(error => {
46+
error ? reject(error) : resolve();
47+
});
3748
});
49+
};
3850

39-
pgSocket.on('data', data => {
40-
const flushed = proxySocket.write(data);
41-
42-
!flushed && pgSocket.pause();
43-
});
51+
for (let client of (pool as any).clients.keys()) {
52+
removeClient(client);
53+
}
4454

45-
proxySocket.on('drain', () => pgSocket.resume());
46-
47-
pgSocket.on('drain', () => proxySocket.resume());
48-
49-
proxySocket.on('close', () => pgSocket.end());
50-
51-
pgSocket.on('close', () => proxySocket.end());
55+
return shutdownPool();
56+
}
5257

53-
});
58+
export function fakePgServer(cb: Function): net.Server {
59+
const server = net.createServer();
5460

55-
server.listen(PROXY_SERVER_PORT, cb);
61+
server.listen(FAKE_PG_SERVER_PORT, cb);
5662

5763
return server;
5864
}

tests/mocks/Logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// IMPORTS
22
// =================================================================================================
3-
import { Logger } from './../../lib/util';
3+
import { Logger } from '../../lib/util';
44

55
// LOGGER CLASS
66
// =================================================================================================

tests/pool.spec.ts

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

3-
import {ERROR_EVENT} from '../lib/Pool';
3+
import {ERROR_EVENT, CLOSED_EVENT} from '../lib/Pool';
44
import {createNewPool} from './helpers';
55
import {settings} from './settings';
66

@@ -39,7 +39,7 @@ describe('Pool;', () => {
3939
pool.acquire((err, client) => {
4040
expect(client).to.be.undefined;
4141

42-
// a connection error should not polute the pool with a dead client
42+
// a connection error should not pollute the pool with a dead client
4343
expect(pool.totalCount).to.equal(0);
4444

4545
pool.shutdown(done);
@@ -48,7 +48,6 @@ describe('Pool;', () => {
4848

4949
it('removes client if it errors in background', done => {
5050
const pool = createNewPool();
51-
const testString = 'foo';
5251
const errorMessage = 'on purpose';
5352
let testClient;
5453

@@ -58,7 +57,6 @@ describe('Pool;', () => {
5857
testClient = client;
5958

6059
testClient.release();
61-
(testClient as any).testString = testString;
6260

6361
setTimeout(() => {
6462
testClient.emit('error', new Error(errorMessage));
@@ -69,11 +67,14 @@ describe('Pool;', () => {
6967
expect(err.message).to.equal(errorMessage);
7068
expect(client).to.not.be.undefined;
7169
expect(client).to.equal(testClient);
72-
expect((client as any).testString).to.equal(testString);
70+
});
7371

74-
(client as any).connection.stream.on('end', () => {
75-
pool.shutdown(done);
76-
});
72+
pool.on(CLOSED_EVENT, (client) => {
73+
expect(client).to.equal(testClient);
74+
75+
expect(pool.totalCount).to.equal(0);
76+
77+
pool.shutdown(done);
7778
})
7879
});
7980

@@ -93,7 +94,7 @@ describe('Pool;', () => {
9394
});
9495
});
9596

96-
it('never calls callback syncronously', done => {
97+
it('never calls callback synchronously', done => {
9798
const pool = createNewPool();
9899

99100
pool.acquire((err, client) => {

tests/poolConnectionTimeout.spec.ts

Lines changed: 21 additions & 11 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,14 +62,19 @@ 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;
@@ -79,6 +88,7 @@ describe('Pool connection timeout;', () => {
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;

tests/poolIdleTimeout.spec.ts

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,9 @@ import {expect} from 'chai';
22

33
import {CLOSED_EVENT} from '../lib/Pool';
44
import {ConnectionError} from '../lib/errors';
5-
import {createNewPool, pgProxyServer, createClient, wait, PROXY_SERVER_PORT} from './helpers';
6-
7-
let server;
5+
import {createNewPool, createClient, wait} from './helpers';
86

97
describe('Pool idle timeout;', () => {
10-
before(done => {
11-
server = pgProxyServer(250, done);
12-
});
13-
14-
after(done => {
15-
server.close(done);
16-
});
17-
188
it('should timeout and remove the client', done => {
199
const pool = createNewPool({idleTimeout: 10});
2010

@@ -38,35 +28,6 @@ describe('Pool idle timeout;', () => {
3828
});
3929
});
4030

41-
it('times out and removes clients when others are also removed', async done => {
42-
const pool = createNewPool({idleTimeout: 10});
43-
44-
try {
45-
const clientA = await createClient(pool);
46-
const clientB = await createClient(pool);
47-
48-
clientA.release();
49-
clientB.release(new Error());
50-
51-
const removal = new Promise(resolve => {
52-
pool.on(CLOSED_EVENT, () => {
53-
expect(pool.idleCount).to.equal(0);
54-
expect(pool.totalCount).to.equal(0);
55-
resolve();
56-
})
57-
});
58-
59-
const timeout = wait(100).then(() =>
60-
Promise.reject(new Error('Idle timeout failed to occur')));
61-
62-
await Promise.race([removal, timeout]);
63-
64-
pool.shutdown(done);
65-
} catch (err) {
66-
done(err);
67-
}
68-
});
69-
7031
it('can remove idle clients and recreate them', async done => {
7132
const iterations = 20;
7233
const pool = createNewPool({idleTimeout: 1});
@@ -136,8 +97,8 @@ describe('Pool idle timeout;', () => {
13697
});
13798

13899
it('should removes client after timeout error', async done => {
139-
const idleTimeout = 150;
140-
const pool = createNewPool({connectionTimeout: 150, idleTimeout}, {port: PROXY_SERVER_PORT});
100+
const idleTimeout = 2000;
101+
const pool = createNewPool({connectionTimeout: 1, idleTimeout});
141102
let client, timeoutError;
142103

143104
try {
@@ -170,10 +131,12 @@ describe('Pool idle timeout;', () => {
170131
}
171132
});
172133

173-
it('should removes client after multiple timeout errors', async done => {
174-
const idleTimeout = 50;
175-
const iterations = 15;
176-
const pool = createNewPool({connectionTimeout: 150, idleTimeout, maxSize: iterations}, {port: PROXY_SERVER_PORT});
134+
it('should removes client after multiple timeout errors', async function (done) {
135+
this.timeout(15000);
136+
137+
const idleTimeout = 4000;
138+
const iterations = 10;
139+
const pool = createNewPool({connectionTimeout: 1, idleTimeout, maxSize: iterations});
177140
const errors = [];
178141

179142
try {
@@ -197,7 +160,7 @@ describe('Pool idle timeout;', () => {
197160
expect(pool.idleCount).to.equal(0);
198161
expect(pool.totalCount).to.equal(iterations);
199162

200-
await wait(200);
163+
await wait(idleTimeout / 4);
201164

202165
expect(pool.idleCount).to.equal(iterations);
203166
expect(pool.totalCount).to.equal(iterations);

0 commit comments

Comments
 (0)