Skip to content

Commit 99d6a90

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

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

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

Lines changed: 73 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,70 @@ describe('Connection Pool', function () {
6471
});
6572
});
6673
});
74+
75+
describe(
76+
'ConnectionCheckedInEvent',
77+
{ requires: { mongodb: '>=4.4', topology: 'single' } },
78+
function () {
79+
let client: MongoClient;
80+
81+
beforeEach(async function () {
82+
await configureFailPoint(this.configuration, {
83+
configureFailPoint: 'failCommand',
84+
mode: 'alwaysOn',
85+
data: {
86+
failCommands: ['insert'],
87+
blockConnection: true,
88+
blockTimeMS: 500
89+
}
90+
});
91+
92+
client = this.configuration.newClient();
93+
await client.connect();
94+
await Promise.all(Array.from({ length: 100 }, () => client.db().command({ ping: 1 })));
95+
});
96+
97+
afterEach(async function () {
98+
await clearFailPoint(this.configuration);
99+
await client.close();
100+
});
101+
102+
describe('when a MongoClient is closed', function () {
103+
it(
104+
'a connection pool emits checked in events for closed connections',
105+
{ requires: { mongodb: '>=4.4', topology: 'single' } },
106+
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));
111+
112+
const inserts = Promise.allSettled([
113+
client.db('test').collection('test').insertOne({ a: 1 }),
114+
client.db('test').collection('test').insertOne({ a: 1 }),
115+
client.db('test').collection('test').insertOne({ a: 1 })
116+
]);
117+
118+
// wait until all pings are pending on the server
119+
while (connectionCheckedOutEvents.length < 3) await sleep(1);
120+
121+
const insertConnectionIds = connectionCheckedOutEvents.map(
122+
({ address, connectionId }) => `${address} + ${connectionId}`
123+
);
124+
125+
await client.close();
126+
127+
const insertCheckIns = connectionCheckedInEvents.filter(({ address, connectionId }) =>
128+
insertConnectionIds.includes(`${address} + ${connectionId}`)
129+
);
130+
131+
expect(insertCheckIns).to.have.lengthOf(3);
132+
133+
await inserts;
134+
}
135+
);
136+
});
137+
}
138+
);
67139
});
68140
});

0 commit comments

Comments
 (0)