Skip to content

Commit 711763e

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

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

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

Lines changed: 66 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,63 @@ describe('Connection Pool', function () {
6471
});
6572
});
6673
});
74+
75+
describe('ConnectionCheckedInEvent', { requires: { mongodb: '>=4.4' } }, 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(
98+
'a connection pool emits checked in events for closed connections',
99+
{ requires: { mongodb: '>=4.4' } },
100+
async () => {
101+
const connectionCheckedOutEvents: ConnectionCheckedOutEvent[] = [];
102+
client.on('connectionCheckedOut', event => connectionCheckedOutEvents.push(event));
103+
const connectionCheckedInEvents: ConnectionCheckedInEvent[] = [];
104+
client.on('connectionCheckedIn', event => connectionCheckedInEvents.push(event));
105+
106+
const pingPromises = Promise.allSettled([
107+
client.db('test').collection('test').insertOne({ a: 1 }),
108+
client.db('test').collection('test').insertOne({ a: 1 }),
109+
client.db('test').collection('test').insertOne({ a: 1 })
110+
]);
111+
112+
// wait until all pings are pending on the server
113+
while (connectionCheckedOutEvents.length < 3) await sleep(1);
114+
115+
const pingConnectionIds = connectionCheckedOutEvents.map(
116+
({ address, connectionId }) => `${address} + ${connectionId}`
117+
);
118+
119+
await client.close();
120+
121+
const pingCheckIns = connectionCheckedInEvents.filter(({ address, connectionId }) =>
122+
pingConnectionIds.includes(`${address} + ${connectionId}`)
123+
);
124+
125+
expect(pingCheckIns).to.have.lengthOf(3);
126+
127+
await pingPromises;
128+
}
129+
);
130+
});
131+
});
67132
});
68133
});

0 commit comments

Comments
 (0)