Skip to content

Commit 91a3103

Browse files
committed
test: check-in before close
1 parent 11853d4 commit 91a3103

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/cmap/connection_pool.ts

-4
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

+25-19
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import { once } from 'node:events';
22

33
import { expect } from 'chai';
44

5-
import {
6-
type ConnectionCheckedInEvent,
7-
type ConnectionCheckedOutEvent,
8-
type ConnectionPoolCreatedEvent,
9-
type Db,
10-
type MongoClient
11-
} from '../../mongodb';
5+
import { type ConnectionPoolCreatedEvent, type Db, type MongoClient } from '../../mongodb';
126
import { clearFailPoint, configureFailPoint, sleep } from '../../tools/utils';
137

148
describe('Connection Pool', function () {
@@ -104,10 +98,13 @@ describe('Connection Pool', function () {
10498
'a connection pool emits checked in events for closed connections',
10599
{ requires: { mongodb: '>=4.4', topology: 'single' } },
106100
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));
101+
const allClientEvents = [];
102+
const pushToClientEvents = e => allClientEvents.push(e);
103+
104+
client
105+
.on('connectionCheckedOut', pushToClientEvents)
106+
.on('connectionCheckedIn', pushToClientEvents)
107+
.on('connectionClosed', pushToClientEvents);
111108

112109
const inserts = Promise.allSettled([
113110
client.db('test').collection('test').insertOne({ a: 1 }),
@@ -116,19 +113,28 @@ describe('Connection Pool', function () {
116113
]);
117114

118115
// wait until all pings are pending on the server
119-
while (connectionCheckedOutEvents.length < 3) await sleep(1);
116+
while (allClientEvents.filter(e => e.name === 'connectionCheckedOut').length < 3) {
117+
await sleep(1);
118+
}
120119

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

125124
await client.close();
126125

127-
const insertCheckIns = connectionCheckedInEvents.filter(({ address, connectionId }) =>
128-
insertConnectionIds.includes(`${address} + ${connectionId}`)
129-
);
126+
const insertCheckInAndCloses = allClientEvents
127+
.filter(e => e.name === 'connectionCheckedIn' || e.name === 'connectionClosed')
128+
.filter(({ address, connectionId }) =>
129+
insertConnectionIds.includes(`${address} + ${connectionId}`)
130+
);
130131

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

133139
await inserts;
134140
}

0 commit comments

Comments
 (0)