Skip to content

Commit

Permalink
fix(NODE-5228)!: remove unneeded fields from ConnectionPoolCreatedEve…
Browse files Browse the repository at this point in the history
…nt.options (#3772)
  • Loading branch information
W-A-James authored Jul 19, 2023
1 parent c44af11 commit 7a91714
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 3 deletions.
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);
});
});
});
});
});
20 changes: 20 additions & 0 deletions test/types/connection_pool_events.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { once } from 'events';
import { expectType } from 'tsd';

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

const client: MongoClient = new MongoClient('');
const p = once(client, 'connectionPoolCreated');
await client.connect();

const ev: ConnectionPoolCreatedEvent = (await p)[0];
expectType<ConnectionPoolCreatedEvent>(ev);

expectType<{
maxPoolSize: number;
minPoolSize: number;
maxConnecting: number;
maxIdleTimeMS: number;
waitQueueTimeoutMS: number;
}>(ev.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
});
});
});
});
});
});

0 comments on commit 7a91714

Please sign in to comment.