Skip to content

Commit ad2b337

Browse files
author
denshikov-vovan
committed
[ #154578064 ] - added some tests
1 parent adf1fe3 commit ad2b337

File tree

5 files changed

+226
-141
lines changed

5 files changed

+226
-141
lines changed

bin/lib/Database.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Logger, buildLogger, DbLogger } from './util';
1010

1111
// MODULE VARIABLES
1212
// ================================================================================================
13-
const ERROR_EVENT = 'error';
13+
export const ERROR_EVENT = 'error';
1414

1515
// INTERFACES
1616
// ================================================================================================

lib/Pool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface PoolOptions {
2020
connectionTimeout? : number;
2121
}
2222

23-
const enum PoolState {
23+
export const enum PoolState {
2424
active = 2, closing = 3, closed = 4
2525
}
2626

tests/database.spec.ts

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import * as os from 'os';
2+
import * as http from 'http';
3+
import {exec} from 'child_process';
4+
import {expect} from 'chai';
5+
6+
import {Database, ERROR_EVENT} from '../lib/Database';
7+
import {Session} from '../lib/Session';
8+
import {PoolState} from '../lib/Pool';
9+
import {wait} from './helpers';
10+
11+
import {prepareDatabase} from './setup';
12+
import {settings} from './settings';
13+
14+
const isWin = os.type().search('Windows') > -1;
15+
16+
let database, pool;
17+
18+
describe.only('Database;', function () {
19+
this.timeout(15000);
20+
21+
before(async done => {
22+
try {
23+
database = new Database(settings);
24+
pool = database.pool;
25+
done();
26+
} catch (err) {
27+
done(err);
28+
}
29+
});
30+
31+
after(async done => {
32+
try {
33+
await startPostgresql();
34+
await database.close();
35+
done();
36+
} catch (err) {
37+
done(err);
38+
}
39+
});
40+
41+
describe('Loss connection;', () => {
42+
43+
it('db pool should be empty', async done => {
44+
try {
45+
checkPoolState(0, 0, PoolState.active);
46+
47+
done();
48+
49+
} catch (err) {
50+
done(err);
51+
}
52+
});
53+
54+
it('should return result without an error', async done => {
55+
try {
56+
const session = await connectToDatabase(database);
57+
await session.startTransaction();
58+
const user = await getUsers(session, 1);
59+
60+
expect(user).to.not.be.undefined;
61+
expect(user.id).to.equal(1);
62+
63+
checkPoolState(1, 0, PoolState.active);
64+
65+
await session.close('commit');
66+
67+
done();
68+
} catch (err) {
69+
done(err);
70+
}
71+
});
72+
73+
it('db pool should be empty after idleTimeout time', async done => {
74+
try {
75+
await wait(settings.pool.idleTimeout);
76+
77+
checkPoolState(0, 0, PoolState.active);
78+
79+
done();
80+
81+
} catch (err) {
82+
done(err);
83+
}
84+
});
85+
86+
it('should return an error when connection is terminated', async done => {
87+
process.on('uncaughtException', done);
88+
89+
database.on(ERROR_EVENT, function dbErrorHandler(err) {
90+
try {
91+
expect(err).to.be.an.instanceof(Error);
92+
expect(err.message).to.include('terminating connection');
93+
94+
checkPoolState(0, 0, PoolState.active);
95+
96+
done();
97+
} catch (error) {
98+
done(error);
99+
} finally {
100+
process.removeListener('uncaughtException', done);
101+
database.removeListener('error', dbErrorHandler);
102+
}
103+
});
104+
105+
try {
106+
const session = await connectToDatabase(database);
107+
await session.startTransaction();
108+
109+
checkPoolState(1, 0, PoolState.active);
110+
111+
await stopPostgresql();
112+
} catch (err) {
113+
done(err);
114+
}
115+
});
116+
117+
it('should return ECONNREFUSED error', async done => {
118+
let session;
119+
120+
try {
121+
checkPoolState(0, 0, PoolState.active);
122+
123+
session = await connectToDatabase(database);
124+
} catch (err) {
125+
try {
126+
expect(session).to.be.undefined;
127+
expect(err).to.be.an.instanceof(Error);
128+
expect(err.message).to.include('ECONNREFUSED');
129+
130+
checkPoolState(0, 0, PoolState.active);
131+
132+
done();
133+
} catch (error) {
134+
done(error);
135+
}
136+
}
137+
});
138+
139+
it('should return result without an error', async done => {
140+
try {
141+
await startPostgresql();
142+
const session = await connectToDatabase(database);
143+
await session.startTransaction();
144+
const user = await getUsers(session, 1);
145+
146+
expect(user).to.not.be.undefined;
147+
expect(user.id).to.equal(1);
148+
149+
checkPoolState(1, 0, PoolState.active);
150+
151+
await session.close('commit');
152+
153+
done();
154+
} catch (err) {
155+
done(err);
156+
}
157+
});
158+
159+
it('db pool should be empty after idleTimeout time', async done => {
160+
try {
161+
await wait(settings.pool.idleTimeout);
162+
163+
checkPoolState(0, 0, PoolState.active);
164+
165+
done();
166+
167+
} catch (err) {
168+
done(err);
169+
}
170+
});
171+
});
172+
});
173+
174+
// helpers
175+
function checkPoolState(totalCount, idleCount, state) {
176+
expect(pool.idleCount).to.equal(idleCount);
177+
expect(pool.totalCount).to.equal(totalCount);
178+
expect(pool.state).to.equal(state);
179+
}
180+
181+
function connectToDatabase(db: Database): Promise<Session> {
182+
return db.connect();
183+
}
184+
185+
async function getUsers(session: Session, userId: number): Promise<any> {
186+
await prepareDatabase(session);
187+
188+
const query = {
189+
text: `SELECT * FROM tmp_users WHERE id=${userId};`,
190+
mask: 'single'
191+
};
192+
193+
return await session.execute(query);
194+
}
195+
196+
function execCommand(command: string): Promise<void> {
197+
return new Promise((resolve, reject) => {
198+
exec(command, (err, stdout, stderr) => {
199+
const error = err || stderr;
200+
201+
error ? reject(error) : resolve();
202+
});
203+
});
204+
}
205+
206+
async function startPostgresql(): Promise<void> {
207+
const command = isWin
208+
? ''
209+
: 'brew services start postgresql';
210+
211+
await execCommand(command);
212+
await wait(1000);
213+
}
214+
215+
async function stopPostgresql(): Promise<void> {
216+
const command = isWin
217+
? ''
218+
: 'brew services stop postgresql';
219+
220+
await execCommand(command);
221+
await wait(1000);
222+
}

tests/testFile.js

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)