Skip to content

Commit 7a91714

Browse files
authored
fix(NODE-5228)!: remove unneeded fields from ConnectionPoolCreatedEvent.options (#3772)
1 parent c44af11 commit 7a91714

File tree

5 files changed

+155
-3
lines changed

5 files changed

+155
-3
lines changed

Diff for: package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/cmap/connection_pool_events.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,19 @@ export abstract class ConnectionPoolMonitoringEvent {
5454
*/
5555
export class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
5656
/** The options used to create this connection pool */
57-
options?: ConnectionPoolOptions;
57+
options: Pick<
58+
ConnectionPoolOptions,
59+
'maxPoolSize' | 'minPoolSize' | 'maxConnecting' | 'maxIdleTimeMS' | 'waitQueueTimeoutMS'
60+
>;
5861
/** @internal */
5962
name = CONNECTION_POOL_CREATED;
6063

6164
/** @internal */
6265
constructor(pool: ConnectionPool) {
6366
super(pool);
64-
this.options = pool.options;
67+
const { maxConnecting, maxPoolSize, minPoolSize, maxIdleTimeMS, waitQueueTimeoutMS } =
68+
pool.options;
69+
this.options = { maxConnecting, maxPoolSize, minPoolSize, maxIdleTimeMS, waitQueueTimeoutMS };
6570
}
6671
}
6772

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { once } from 'node:events';
2+
3+
import { expect } from 'chai';
4+
5+
import { type ConnectionPoolCreatedEvent, type Db, type MongoClient } from '../../mongodb';
6+
7+
describe('Connection Pool', function () {
8+
let client: MongoClient;
9+
let db: Db;
10+
afterEach(async function () {
11+
if (client) {
12+
if (db) {
13+
await db.dropDatabase();
14+
}
15+
await client.close();
16+
}
17+
});
18+
describe('Events', function () {
19+
describe('ConnectionPoolCreatedEvent', function () {
20+
context('when no connection pool options are passed in', function () {
21+
let pConnectionPoolCreated: Promise<ConnectionPoolCreatedEvent[]>;
22+
let connectionPoolCreated: ConnectionPoolCreatedEvent;
23+
beforeEach(async function () {
24+
client = this.configuration.newClient({}, {});
25+
pConnectionPoolCreated = once(client, 'connectionPoolCreated');
26+
await client.connect();
27+
28+
connectionPoolCreated = (await pConnectionPoolCreated)[0];
29+
});
30+
31+
it('the options field matches the default options', function () {
32+
expect(connectionPoolCreated).to.have.deep.property('options', {
33+
waitQueueTimeoutMS: 0,
34+
maxIdleTimeMS: 0,
35+
maxConnecting: 2,
36+
minPoolSize: 0,
37+
maxPoolSize: 100
38+
});
39+
});
40+
});
41+
42+
context('when valid non-default connection pool options are passed in', function () {
43+
let pConnectionPoolCreated: Promise<ConnectionPoolCreatedEvent[]>;
44+
let connectionPoolCreated: ConnectionPoolCreatedEvent;
45+
const options = {
46+
waitQueueTimeoutMS: 2000,
47+
maxIdleTimeMS: 1,
48+
maxConnecting: 3,
49+
minPoolSize: 1,
50+
maxPoolSize: 101
51+
};
52+
beforeEach(async function () {
53+
client = this.configuration.newClient({}, options);
54+
pConnectionPoolCreated = once(client, 'connectionPoolCreated');
55+
await client.connect();
56+
57+
connectionPoolCreated = (await pConnectionPoolCreated)[0];
58+
});
59+
60+
it('the options field only contains keys and values matching the non-default options', function () {
61+
expect(connectionPoolCreated).to.have.deep.property('options', options);
62+
});
63+
});
64+
});
65+
});
66+
});

Diff for: test/types/connection_pool_events.test-d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { once } from 'events';
2+
import { expectType } from 'tsd';
3+
4+
import type { ConnectionPoolCreatedEvent } from '../mongodb';
5+
import { MongoClient } from '../mongodb';
6+
7+
const client: MongoClient = new MongoClient('');
8+
const p = once(client, 'connectionPoolCreated');
9+
await client.connect();
10+
11+
const ev: ConnectionPoolCreatedEvent = (await p)[0];
12+
expectType<ConnectionPoolCreatedEvent>(ev);
13+
14+
expectType<{
15+
maxPoolSize: number;
16+
minPoolSize: number;
17+
maxConnecting: number;
18+
maxIdleTimeMS: number;
19+
waitQueueTimeoutMS: number;
20+
}>(ev.options);

Diff for: test/unit/cmap/connection_pool_events.test.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { expect } from 'chai';
2+
3+
import { type ConnectionPool, ConnectionPoolCreatedEvent } from '../../mongodb';
4+
5+
describe('Connection Pool Events', function () {
6+
const connectionPoolMock = {
7+
address: 'localhost:9000',
8+
time: new Date()
9+
};
10+
11+
describe('ConnectionPoolCreatedEvent', function () {
12+
describe('constructor', function () {
13+
context('when provided expected option fields', function () {
14+
it(`Sets the allowed fields appropriately`, function () {
15+
const options = {
16+
maxIdleTimeMS: 0,
17+
maxConnecting: 2,
18+
minPoolSize: 0,
19+
maxPoolSize: 100,
20+
waitQueueTimeoutMS: 1000
21+
};
22+
const event = new ConnectionPoolCreatedEvent({
23+
...connectionPoolMock,
24+
options
25+
} as unknown as ConnectionPool);
26+
27+
expect(event).to.have.deep.property('options', options);
28+
});
29+
});
30+
context('when provided unallowed fields', function () {
31+
it('only stores expected fields', function () {
32+
const options = {
33+
maxIdleTimeMS: 0,
34+
maxConnecting: 2,
35+
minPoolSize: 0,
36+
maxPoolSize: 100,
37+
waitQueueTimeoutMS: 1000,
38+
credentials: {
39+
user: 'user',
40+
pass: 'pass'
41+
},
42+
foo: 'foo',
43+
hello: 'world'
44+
};
45+
const event = new ConnectionPoolCreatedEvent({
46+
...connectionPoolMock,
47+
options
48+
} as unknown as ConnectionPool);
49+
50+
expect(event).to.have.deep.property('options', {
51+
maxIdleTimeMS: 0,
52+
maxConnecting: 2,
53+
minPoolSize: 0,
54+
maxPoolSize: 100,
55+
waitQueueTimeoutMS: 1000
56+
});
57+
});
58+
});
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)