Skip to content

Commit 4dd1a05

Browse files
author
denshikov-vovan
committed
[ #154578064 ] - updated tests for pool
1 parent 443e915 commit 4dd1a05

File tree

3 files changed

+8
-64
lines changed

3 files changed

+8
-64
lines changed

tests/helpers.ts

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

7-
export const PROXY_SERVER_PORT = 3000;
87
export const FAKE_PG_SERVER_PORT = 3001;
98

109
export function createNewPool(poolOptions: PoolOptions = {}, connectionOptions: ConnectionConfig = {}, logger?: Logger): ConnectionPool {
@@ -56,41 +55,6 @@ export function removeAllClientAndShutdownPool(pool: ConnectionPool): Promise<vo
5655
return shutdownPool();
5756
}
5857

59-
export function pgProxyServer(timeout: number, cb): net.Server {
60-
const server = net.createServer(proxySocket => {
61-
const pgSocket = new net.Socket();
62-
63-
pgSocket.connect(settings.connection.port, settings.connection.host);
64-
65-
proxySocket.on('data', data => {
66-
setTimeout(() => {
67-
const flushed = pgSocket.write(data);
68-
69-
!flushed && proxySocket.pause();
70-
}, timeout);
71-
});
72-
73-
pgSocket.on('data', data => {
74-
const flushed = proxySocket.write(data);
75-
76-
!flushed && pgSocket.pause();
77-
});
78-
79-
proxySocket.on('drain', () => pgSocket.resume());
80-
81-
pgSocket.on('drain', () => proxySocket.resume());
82-
83-
proxySocket.on('close', () => pgSocket.end());
84-
85-
pgSocket.on('close', () => proxySocket.end());
86-
87-
});
88-
89-
server.listen(PROXY_SERVER_PORT, cb);
90-
91-
return server;
92-
}
93-
9458
export function fakePgServer(cb: Function): net.Server {
9559
const server = net.createServer();
9660

tests/poolIdleTimeout.spec.ts

Lines changed: 5 additions & 15 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

@@ -107,8 +97,8 @@ describe('Pool idle timeout;', () => {
10797
});
10898

10999
it('should removes client after timeout error', async done => {
110-
const idleTimeout = 150;
111-
const pool = createNewPool({connectionTimeout: 150, idleTimeout}, {port: PROXY_SERVER_PORT});
100+
const idleTimeout = 400;
101+
const pool = createNewPool({connectionTimeout: 1, idleTimeout});
112102
let client, timeoutError;
113103

114104
try {
@@ -142,9 +132,9 @@ describe('Pool idle timeout;', () => {
142132
});
143133

144134
it('should removes client after multiple timeout errors', async done => {
145-
const idleTimeout = 1000;
135+
const idleTimeout = 400;
146136
const iterations = 15;
147-
const pool = createNewPool({connectionTimeout: 150, idleTimeout, maxSize: iterations}, {port: PROXY_SERVER_PORT});
137+
const pool = createNewPool({connectionTimeout: 1, idleTimeout, maxSize: iterations});
148138
const errors = [];
149139

150140
try {

tests/poolSizing.spec.ts

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

3-
import {createNewPool, createClient, wait, pgProxyServer, PROXY_SERVER_PORT} from './helpers';
4-
5-
let server;
3+
import {createNewPool, createClient, wait} from './helpers';
64

75
describe('Pool size of 1;', () => {
8-
before(done => {
9-
server = pgProxyServer(250, done);
10-
});
11-
12-
after(done => {
13-
server.close(done);
14-
});
15-
166
it('can create a single client and use it once', async done => {
177
const pool = createNewPool({maxSize: 1});
188

@@ -36,7 +26,7 @@ describe('Pool size of 1;', () => {
3626
});
3727

3828
it('can create a single client and use it multiple times', async done => {
39-
const pool = createNewPool({maxSize: 1, connectionTimeout: 300}, {port: PROXY_SERVER_PORT});
29+
const pool = createNewPool({maxSize: 1, connectionTimeout: 400});
4030
let clientA, clientB;
4131

4232
try {

0 commit comments

Comments
 (0)