Skip to content

fix(NODE-5228)!: remove unneeded fields from ConnectionPoolCreatedEvent.options #3772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/cmap/connection_pool_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ export abstract class ConnectionPoolMonitoringEvent {
*/
export class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
/** The options used to create this connection pool */
options?: ConnectionPoolOptions;
options: Pick<
ConnectionPoolOptions,
'maxPoolSize' | 'minPoolSize' | 'maxConnecting' | 'maxIdleTimeMS' | 'waitQueueTimeoutMS'
>;
/** @internal */
name = CONNECTION_POOL_CREATED;

/** @internal */
constructor(pool: ConnectionPool) {
super(pool);
this.options = pool.options;
const { maxConnecting, maxPoolSize, minPoolSize, maxIdleTimeMS, waitQueueTimeoutMS } =
pool.options;
this.options = { maxConnecting, maxPoolSize, minPoolSize, maxIdleTimeMS, waitQueueTimeoutMS };
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { once } from 'node:events';

import { expect } from 'chai';

import { type ConnectionPoolCreatedEvent, type Db, type MongoClient } from '../../mongodb';

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);
});
});
});
});
});
61 changes: 61 additions & 0 deletions test/unit/cmap/connection_pool_events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { expect } from 'chai';

import { type ConnectionPool, ConnectionPoolCreatedEvent } from '../../mongodb';

describe('Connection Pool Events', function () {
const connectionPoolMock = {
address: 'localhost:9000',
time: new Date()
};

describe('ConnectionPoolCreatedEvent', function () {
describe('constructor', function () {
context('when provided expected option fields', function () {
it(`Sets the allowed fields appropriately`, function () {
const options = {
maxIdleTimeMS: 0,
maxConnecting: 2,
minPoolSize: 0,
maxPoolSize: 100,
waitQueueTimeoutMS: 1000
};
const event = new ConnectionPoolCreatedEvent({
...connectionPoolMock,
options
} as unknown as ConnectionPool);

expect(event).to.have.deep.property('options', options);
});
});
context('when provided unallowed fields', function () {
it('only stores expected fields', function () {
const options = {
maxIdleTimeMS: 0,
maxConnecting: 2,
minPoolSize: 0,
maxPoolSize: 100,
waitQueueTimeoutMS: 1000,
credentials: {
user: 'user',
pass: 'pass'
},
foo: 'foo',
hello: 'world'
};
const event = new ConnectionPoolCreatedEvent({
...connectionPoolMock,
options
} as unknown as ConnectionPool);

expect(event).to.have.deep.property('options', {
maxIdleTimeMS: 0,
maxConnecting: 2,
minPoolSize: 0,
maxPoolSize: 100,
waitQueueTimeoutMS: 1000
});
});
});
});
});
});