-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathconnection_pool.test.ts
More file actions
206 lines (172 loc) · 6.59 KB
/
connection_pool.test.ts
File metadata and controls
206 lines (172 loc) · 6.59 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
import { once } from 'node:events';
import { expect } from 'chai';
import * as sinon from 'sinon';
import {
type ConnectionPoolCreatedEvent,
type Db,
type MongoClient,
type Server
} from '../../../src';
import { clearFailPoint, configureFailPoint, sleep } from '../../tools/utils';
describe('Connection Pool', function () {
let client: MongoClient;
let db: Db;
afterEach(async function () {
if (client) {
if (db) {
await db.dropDatabase();
}
await client.close();
}
});
describe('Events', function () {
describe('ConnectionPoolCreatedEvent', function () {
context('when no connection pool options are passed in', function () {
let pConnectionPoolCreated: Promise<ConnectionPoolCreatedEvent[]>;
let connectionPoolCreated: ConnectionPoolCreatedEvent;
beforeEach(async function () {
client = this.configuration.newClient({}, {});
pConnectionPoolCreated = once(client, 'connectionPoolCreated');
await client.connect();
connectionPoolCreated = (await pConnectionPoolCreated)[0];
});
it('the options field matches the default options', function () {
expect(connectionPoolCreated).to.have.deep.property('options', {
waitQueueTimeoutMS: 0,
maxIdleTimeMS: 0,
maxConnecting: 2,
minPoolSize: 0,
maxPoolSize: 100
});
});
});
context('when valid non-default connection pool options are passed in', function () {
let pConnectionPoolCreated: Promise<ConnectionPoolCreatedEvent[]>;
let connectionPoolCreated: ConnectionPoolCreatedEvent;
const options = {
waitQueueTimeoutMS: 2000,
maxIdleTimeMS: 1,
maxConnecting: 3,
minPoolSize: 1,
maxPoolSize: 101
};
beforeEach(async function () {
client = this.configuration.newClient({}, options);
pConnectionPoolCreated = once(client, 'connectionPoolCreated');
await client.connect();
connectionPoolCreated = (await pConnectionPoolCreated)[0];
});
it('the options field only contains keys and values matching the non-default options', function () {
expect(connectionPoolCreated).to.have.deep.property('options', options);
});
});
});
const metadata: MongoDBMetadataUI = { requires: { mongodb: '>=4.4' } };
describe('ConnectionCheckedInEvent', metadata, function () {
let client: MongoClient;
beforeEach(async function () {
if (!this.configuration.filters.MongoDBVersionFilter.filter({ metadata })) {
return;
}
if (!this.configuration.filters.MongoDBTopologyFilter.filter({ metadata })) {
return;
}
await configureFailPoint(this.configuration, {
configureFailPoint: 'failCommand',
mode: 'alwaysOn',
data: {
failCommands: ['find'],
blockConnection: true,
blockTimeMS: 500
}
});
client = this.configuration.newClient({}, { readPreference: 'secondaryPreferred' });
await client.connect();
await Promise.all(Array.from({ length: 100 }, () => client.db().command({ ping: 1 })));
});
afterEach(async function () {
if (this.configuration.filters.MongoDBVersionFilter.filter({ metadata })) {
await clearFailPoint(this.configuration);
}
await client.close();
});
describe('when a MongoClient is closed', function () {
it(
'a connection pool emits checked in events for closed connections',
metadata,
async () => {
const allClientEvents = [];
const pushToClientEvents = e => allClientEvents.push(e);
client
.on('connectionCheckedOut', pushToClientEvents)
.on('connectionCheckedIn', pushToClientEvents)
.on('connectionClosed', pushToClientEvents);
const finds = Promise.allSettled([
client.db('test').collection('test').findOne({ a: 1 }),
client.db('test').collection('test').findOne({ a: 1 }),
client.db('test').collection('test').findOne({ a: 1 })
]);
// wait until all finds are pending on the server
while (allClientEvents.filter(e => e.name === 'connectionCheckedOut').length < 3) {
await sleep(1);
}
const findConnectionIds = allClientEvents
.filter(e => e.name === 'connectionCheckedOut')
.map(({ address, connectionId }) => `${address} + ${connectionId}`);
await client.close();
const findCheckInAndCloses = allClientEvents
.filter(e => e.name === 'connectionCheckedIn' || e.name === 'connectionClosed')
.filter(({ address, connectionId }) =>
findConnectionIds.includes(`${address} + ${connectionId}`)
);
expect(findCheckInAndCloses).to.have.lengthOf(6);
// check that each check-in is followed by a close (not proceeded by one)
expect(findCheckInAndCloses.map(e => e.name)).to.deep.equal(
Array.from({ length: 3 }, () => ['connectionCheckedIn', 'connectionClosed']).flat(1)
);
await finds;
}
);
});
});
});
describe(
'background task cleans up connections when minPoolSize=0',
{ requires: { topology: 'single' } },
function () {
let server: Server;
let ensureMinPoolSizeSpy: sinon.SinonSpy;
beforeEach(async function () {
client = this.configuration.newClient(
{},
{
maxConnecting: 10,
minPoolSize: 0,
maxIdleTimeMS: 100
}
);
await client.connect();
await Promise.all(
Array.from({ length: 10 }).map(() => {
return client.db('foo').collection('bar').insertOne({ a: 1 });
})
);
server = Array.from(client.topology.s.servers.entries())[0][1];
expect(
server.pool.availableConnectionCount,
'pool was not filled with connections'
).to.be.greaterThan(0);
ensureMinPoolSizeSpy = sinon.spy(server.pool, 'ensureMinPoolSize');
});
it(
'prunes idle connections when minPoolSize=0',
{ requires: { topology: 'single' } },
async function () {
await sleep(500);
expect(server.pool.availableConnectionCount).to.equal(0);
expect(ensureMinPoolSizeSpy).to.have.been.called;
}
);
}
);
});