Skip to content

Commit cfc2893

Browse files
committed
chore: add test to demonstrate check in
1 parent 0b418bb commit cfc2893

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { once } from 'node:events';
22

33
import { expect } from 'chai';
44

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

714
describe('Connection Pool', function () {
815
let client: MongoClient;
@@ -64,5 +71,59 @@ describe('Connection Pool', function () {
6471
});
6572
});
6673
});
74+
75+
describe.only('ConnectionCheckedInEvent', function () {
76+
let client;
77+
78+
beforeEach(async function () {
79+
client = this.configuration.newClient();
80+
configureFailPoint(this.configuration, {
81+
configureFailPoint: 'failCommand',
82+
mode: 'alwaysOn',
83+
data: {
84+
failCommands: ['insert'],
85+
blockConnection: true,
86+
blockTimeMS: 60_000
87+
}
88+
});
89+
});
90+
91+
afterEach(async function () {
92+
clearFailPoint(this.configuration);
93+
await client.close();
94+
});
95+
96+
describe('when a MongoClient is closed', function () {
97+
it('a connection pool emits checked in events for closed connections', async () => {
98+
const connectionCheckedOutEvents: ConnectionCheckedOutEvent[] = [];
99+
client.on('connectionCheckedOut', event => connectionCheckedOutEvents.push(event));
100+
const connectionCheckedInEvents: ConnectionCheckedInEvent[] = [];
101+
client.on('connectionCheckedIn', event => connectionCheckedInEvents.push(event));
102+
103+
const pingPromises = Promise.allSettled([
104+
client.db('test').collection('test').insertOne({ a: 1 }),
105+
client.db('test').collection('test').insertOne({ a: 1 }),
106+
client.db('test').collection('test').insertOne({ a: 1 })
107+
]);
108+
109+
// wait until all pings are pending on the server
110+
while (connectionCheckedOutEvents.length < 3) await sleep(1);
111+
112+
const pingConnectionIds = connectionCheckedOutEvents.map(
113+
({ connectionId }) => connectionId
114+
);
115+
116+
await client.close();
117+
118+
const pingCheckIns = connectionCheckedInEvents.filter(checkIn =>
119+
pingConnectionIds.includes(checkIn.connectionId)
120+
);
121+
122+
expect(pingCheckIns).to.have.lengthOf(3);
123+
124+
await pingPromises;
125+
});
126+
});
127+
});
67128
});
68129
});

0 commit comments

Comments
 (0)