forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection_pool.test.js
More file actions
235 lines (194 loc) · 7.03 KB
/
connection_pool.test.js
File metadata and controls
235 lines (194 loc) · 7.03 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'use strict';
const { ConnectionPool, MongoError } = require('../../mongodb');
const { WaitQueueTimeoutError } = require('../../mongodb');
const mock = require('../../tools/mongodb-mock/index');
const sinon = require('sinon');
const { expect } = require('chai');
const { setImmediate } = require('timers/promises');
const { ns, isHello } = require('../../mongodb');
const { createTimerSandbox } = require('../timer_sandbox');
const { topologyWithPlaceholderClient } = require('../../tools/utils');
const { MongoClientAuthProviders } = require('../../mongodb');
const { TimeoutContext } = require('../../mongodb');
describe('Connection Pool', function () {
let timeoutContext;
let mockMongod;
let pool;
const stubServer = {
topology: {
client: {
mongoLogger: {
debug: () => null,
willLog: () => null
},
s: {
authProviders: new MongoClientAuthProviders()
},
options: {
extendedMetadata: {}
}
},
s: {
serverSelectionTimeoutMS: 0
}
}
};
after(() => mock.cleanup());
before(() =>
mock.createServer().then(s => {
mockMongod = s;
mockMongod.s = {
topology: topologyWithPlaceholderClient([], {})
};
})
);
beforeEach(() => {
timeoutContext = TimeoutContext.create({ waitQueueTimeoutMS: 0, serverSelectionTimeoutMS: 0 });
});
afterEach(() => pool?.close());
it('should destroy connections which have been closed', async function () {
mockMongod.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(mock.HELLO);
} else {
// destroy on any other command
request.connection.destroy();
}
});
pool = new ConnectionPool(stubServer, {
maxPoolSize: 1,
hostAddress: mockMongod.hostAddress()
});
pool.ready();
const events = [];
pool.on('connectionClosed', event => events.push(event));
const conn = await pool.checkOut({ timeoutContext });
const error = await conn
.command(ns('admin.$cmd'), { ping: 1 }, { timeoutContext })
.catch(error => error);
expect(error).to.be.instanceOf(Error);
pool.checkIn(conn);
expect(events).to.have.length(1);
const closeEvent = events[0];
expect(closeEvent).have.property('reason').equal('error');
conn.destroy();
});
it('should propagate socket timeouts to connections', async function () {
mockMongod.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(mock.HELLO);
} else {
// blackhole other requests
}
});
pool = new ConnectionPool(stubServer, {
maxPoolSize: 1,
socketTimeoutMS: 200,
hostAddress: mockMongod.hostAddress()
});
pool.ready();
const conn = await pool.checkOut({ timeoutContext });
const maybeError = await conn.command(ns('admin.$cmd'), { ping: 1 }, undefined).catch(e => e);
expect(maybeError).to.be.instanceOf(MongoError);
expect(maybeError).to.match(/timed out/);
pool.checkIn(conn);
});
it('should clear timed out wait queue members if no connections are available', async function () {
mockMongod.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(mock.HELLO);
}
});
pool = new ConnectionPool(stubServer, {
maxPoolSize: 1,
waitQueueTimeoutMS: 200,
hostAddress: mockMongod.hostAddress()
});
const timeoutContext = TimeoutContext.create({
waitQueueTimeoutMS: 200,
serverSelectionTimeoutMS: 0
});
pool.ready();
const conn = await pool.checkOut({ timeoutContext });
const err = await pool.checkOut({ timeoutContext }).catch(e => e);
expect(err).to.exist.and.be.instanceOf(WaitQueueTimeoutError);
sinon.stub(pool, 'availableConnectionCount').get(() => 0);
pool.checkIn(conn);
await setImmediate();
expect(pool).property('waitQueueSize').to.equal(0);
});
describe('minPoolSize population', function () {
let clock, timerSandbox;
beforeEach(() => {
timerSandbox = createTimerSandbox();
clock = sinon.useFakeTimers();
});
afterEach(() => {
if (clock) {
timerSandbox.restore();
clock.restore();
clock = undefined;
}
});
it('should respect the minPoolSizeCheckFrequencyMS option', function () {
pool = new ConnectionPool(stubServer, {
minPoolSize: 2,
minPoolSizeCheckFrequencyMS: 42,
hostAddress: mockMongod.hostAddress()
});
const ensureSpy = sinon.spy(pool, 'ensureMinPoolSize');
// return a fake connection that won't get identified as perished
const createConnStub = sinon
.stub(pool, 'createConnection')
.yields(null, { destroy: () => null, generation: 0 });
pool.ready();
// expect ensureMinPoolSize to execute immediately
expect(ensureSpy).to.have.been.calledOnce;
expect(createConnStub).to.have.been.calledOnce;
// check that the successful connection return schedules another run
clock.tick(42);
expect(ensureSpy).to.have.been.calledTwice;
expect(createConnStub).to.have.been.calledTwice;
// check that the 2nd successful connection return schedules another run
// but don't expect to get a new connection since we are at minPoolSize
clock.tick(42);
expect(ensureSpy).to.have.been.calledThrice;
expect(createConnStub).to.have.been.calledTwice;
// check that the next scheduled check runs even after we're at minPoolSize
clock.tick(42);
expect(ensureSpy).to.have.callCount(4);
expect(createConnStub).to.have.been.calledTwice;
});
it('should default minPoolSizeCheckFrequencyMS to 100ms', function () {
pool = new ConnectionPool(stubServer, {
minPoolSize: 2,
hostAddress: mockMongod.hostAddress()
});
const ensureSpy = sinon.spy(pool, 'ensureMinPoolSize');
// return a fake connection that won't get identified as perished
const createConnStub = sinon
.stub(pool, 'createConnection')
.yields(null, { destroy: () => null, generation: 0 });
pool.ready();
// expect ensureMinPoolSize to execute immediately
expect(ensureSpy).to.have.been.calledOnce;
expect(createConnStub).to.have.been.calledOnce;
// check that the successful connection return schedules another run
clock.tick(100);
expect(ensureSpy).to.have.been.calledTwice;
expect(createConnStub).to.have.been.calledTwice;
// check that the 2nd successful connection return schedules another run
// but don't expect to get a new connection since we are at minPoolSize
clock.tick(100);
expect(ensureSpy).to.have.been.calledThrice;
expect(createConnStub).to.have.been.calledTwice;
// check that the next scheduled check runs even after we're at minPoolSize
clock.tick(100);
expect(ensureSpy).to.have.callCount(4);
expect(createConnStub).to.have.been.calledTwice;
});
});
});