Skip to content

Commit 233531a

Browse files
committed
test: check-in before close
1 parent 99d6a90 commit 233531a

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/cmap/connection_pool.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,6 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
492492

493493
closeCheckedOutConnections() {
494494
for (const conn of this.checkedOut) {
495-
this.emitAndLog(
496-
ConnectionPool.CONNECTION_CLOSED,
497-
new ConnectionClosedEvent(this, conn, 'poolClosed')
498-
);
499495
conn.onError(new MongoClientClosedError());
500496
}
501497
}

test/integration/connection-monitoring-and-pooling/connection_pool.test.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { once } from 'node:events';
33
import { expect } from 'chai';
44

55
import {
6-
type ConnectionCheckedInEvent,
7-
type ConnectionCheckedOutEvent,
86
type ConnectionPoolCreatedEvent,
97
type Db,
8+
MONGO_CLIENT_EVENTS,
109
type MongoClient
1110
} from '../../mongodb';
1211
import { clearFailPoint, configureFailPoint, sleep } from '../../tools/utils';
@@ -104,10 +103,13 @@ describe('Connection Pool', function () {
104103
'a connection pool emits checked in events for closed connections',
105104
{ requires: { mongodb: '>=4.4', topology: 'single' } },
106105
async () => {
107-
const connectionCheckedOutEvents: ConnectionCheckedOutEvent[] = [];
108-
client.on('connectionCheckedOut', event => connectionCheckedOutEvents.push(event));
109-
const connectionCheckedInEvents: ConnectionCheckedInEvent[] = [];
110-
client.on('connectionCheckedIn', event => connectionCheckedInEvents.push(event));
106+
const allClientEvents = [];
107+
const pushToClientEvents = e => allClientEvents.push(e);
108+
109+
client
110+
.on('connectionCheckedOut', pushToClientEvents)
111+
.on('connectionCheckedIn', pushToClientEvents)
112+
.on('connectionClosed', pushToClientEvents);
111113

112114
const inserts = Promise.allSettled([
113115
client.db('test').collection('test').insertOne({ a: 1 }),
@@ -116,19 +118,28 @@ describe('Connection Pool', function () {
116118
]);
117119

118120
// wait until all pings are pending on the server
119-
while (connectionCheckedOutEvents.length < 3) await sleep(1);
121+
while (allClientEvents.filter(e => e.name === 'connectionCheckedOut').length < 3) {
122+
await sleep(1);
123+
}
120124

121-
const insertConnectionIds = connectionCheckedOutEvents.map(
122-
({ address, connectionId }) => `${address} + ${connectionId}`
123-
);
125+
const insertConnectionIds = allClientEvents
126+
.filter(e => e.name === 'connectionCheckedOut')
127+
.map(({ address, connectionId }) => `${address} + ${connectionId}`);
124128

125129
await client.close();
126130

127-
const insertCheckIns = connectionCheckedInEvents.filter(({ address, connectionId }) =>
128-
insertConnectionIds.includes(`${address} + ${connectionId}`)
129-
);
131+
const insertCheckInAndCloses = allClientEvents
132+
.filter(e => e.name === 'connectionCheckedIn' || e.name === 'connectionClosed')
133+
.filter(({ address, connectionId }) =>
134+
insertConnectionIds.includes(`${address} + ${connectionId}`)
135+
);
130136

131-
expect(insertCheckIns).to.have.lengthOf(3);
137+
expect(insertCheckInAndCloses).to.have.lengthOf(6);
138+
139+
// check that each check-in is followed by a close (not proceeded by one)
140+
expect(insertCheckInAndCloses.map(e => e.name)).to.deep.equal(
141+
Array.from({ length: 3 }, () => ['connectionCheckedIn', 'connectionClosed']).flat(1)
142+
);
132143

133144
await inserts;
134145
}

0 commit comments

Comments
 (0)