-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.mjs
216 lines (173 loc) · 6.52 KB
/
test.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'use strict';
import { assert } from 'chai';
import { setTimeout as delay } from 'node:timers/promises';
import net from 'node:net';
describe('database', () => {
let database, db;
beforeEach(async () => {
database = await import(`./index.js?cache-bust=${Date.now()}`);
db = undefined;
});
afterEach(function () {
this.timeout(10000);
if (db) { return db.end(); }
});
describe('when the connection times out', () => {
let queryError, badDbServer, queryDuration;
const connectTimeout = 500;
beforeEach(async () => {
badDbServer = net.createServer().listen(3306);
db = await database.connect({ connectTimeout });
const start = new Date();
try { await db.query('SELECT 1'); }
catch (e) { queryError = e; }
queryDuration = new Date() - start;
});
afterEach(() => badDbServer.close());
it('errors', async () => {
assert(queryError, 'query didn\'t error');
assert.equal(queryError.message, 'connect ETIMEDOUT');
assert.isAtLeast(queryDuration, connectTimeout);
assert.isAtMost(queryDuration, connectTimeout + 100); // 100ms grace
});
});
describe('when the default query timeout is reached', () => {
let queryError, queryDuration;
const defaultQueryTimeout = 500;
beforeEach(async () => {
db = await database.connect({
host: 'test-db',
user: 'root',
connectionLimit: 1,
defaultQueryTimeout
});
const start = new Date();
try { await db.query('DO SLEEP(5) -- timeout please'); }
catch (e) { queryError = e; }
queryDuration = new Date() - start;
await delay(250); // Give some time for fire-and-forget actions to finish
});
it('errors', () => {
assert(queryError, 'query didn\'t error');
assert.equal(queryError.message, 'Database query timed out after 500ms');
assert.isAtLeast(queryDuration, defaultQueryTimeout);
assert.isAtMost(queryDuration, defaultQueryTimeout + 100); // 100ms grace
});
it('kills the query', async () => {
const [queries] = await db.query('SHOW PROCESSLIST');
const isRunning = queries.some(q => /timeout please/.test(q.Info));
assert.isFalse(isRunning, 'query still running');
});
it('frees-up the connection', async () => {
assert.isDefined(await db.query('SELECT 1'));
});
});
describe('when the query timeout override is reached', () => {
let queryError, queryDuration;
const timeout = 500;
beforeEach(async () => {
db = await database.connect({
host: 'test-db',
user: 'root',
connectionLimit: 1,
defaultQueryTimeout: 100
});
const start = new Date();
try {
await db.query({ sql: 'DO SLEEP(5) -- timeout please', timeout });
}
catch (e) { queryError = e; }
queryDuration = new Date() - start;
await delay(250); // Give some time for fire-and-forget actions to finish
});
it('errors', () => {
assert(queryError, 'query didn\'t error');
assert.equal(queryError.message, 'Database query timed out after 500ms');
assert.isAtLeast(queryDuration, timeout);
assert.isAtMost(queryDuration, timeout + 100); // 100ms grace
});
it('kills the query', async () => {
const [queries] = await db.query('SHOW PROCESSLIST');
const isRunning = queries.some(q => /timeout please/.test(q.Info));
assert.isFalse(isRunning, 'query still running');
});
it('frees-up the connection', async () => {
assert.isDefined(await db.query('SELECT 1'));
});
});
describe('when querying while all connections are in use', () => {
let queryError, queryDuration, connectionHog, queuedQuery;
const acquireTimeout = 500;
beforeEach(async () => {
db = await database.connect({
host: 'test-db',
user: 'root',
defaultQueryTimeout: 10000,
acquireTimeout,
connectionLimit: 1
});
connectionHog = db.query('DO SLEEP(2)'); // Hog the only available connection
db.once('enqueue', () => queuedQuery = true);
const start = new Date();
try { await db.query('SELECT 1'); } // Query while connection is hogged
catch (e) { queryError = e; }
queryDuration = new Date() - start;
});
it('times out getting a connection', () => {
assert(queryError, 'query didn\'t error');
assert.equal(queryError.message, 'Database connect timed out after 500ms');
assert.isAtLeast(queryDuration, acquireTimeout - 1); // Sometimes comes in early?!
assert.isAtMost(queryDuration, acquireTimeout + 100); // 100ms grace
});
it('makes the timed out connection available for use', async () => {
assert.isTrue(queuedQuery, 'Timed out query was not queued');
await connectionHog;
assert.isDefined(await db.query('SELECT 1'));
}).timeout(5000);
});
describe('when acquiring the connection times out', () => {
let queryError, badDbServer, queryDuration;
const acquireTimeout = 500;
beforeEach(async () => {
badDbServer = net.createServer().listen(3306);
db = await database.connect({ acquireTimeout });
const start = new Date();
try { await db.query('SELECT 1'); }
catch (e) { queryError = e; }
queryDuration = new Date() - start;
});
afterEach(function (done) {
this.timeout(10000);
if (badDbServer.listening) { badDbServer.close(done); }
else { done(); }
});
it('times out', () => {
assert(queryError, 'query didn\'t error');
assert.equal(queryError.message, 'Database connect timed out after 500ms');
assert.isAtLeast(queryDuration, acquireTimeout - 1); // Sometimes comes in early?!
assert.isAtMost(queryDuration, acquireTimeout + 100); // 100ms grace
});
describe('when the connection then errors', () => {
let connError;
const unhandledError = err => connError = err;
beforeEach(() => process.once('unhandledRejection', unhandledError));
afterEach(() => process.off('unhandledRejection', unhandledError));
it('swallows the connection error', done => {
badDbServer.close(() => {
if (connError) { assert.fail(`Did not expect an error, but got '${connError}'`); }
done();
});
}).timeout(10000);
});
});
it('reuses connections', async () => {
db = await database.connect({
host: 'test-db',
user: 'root',
connectionLimit: 1
});
await db.query('SELECT 1');
const results = await db.query('SELECT 2');
assert.isDefined(results);
});
});