Skip to content

Commit 968ee46

Browse files
authored
Merge branch 'main' into NODE-4929
2 parents 453008b + 1e58a4c commit 968ee46

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/sdam/server.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,13 @@ export class Server extends TypedEventEmitter<ServerEvents> {
343343
return;
344344
}
345345

346-
this.s.operationCount += 1;
346+
this.incrementOperationCount();
347347

348348
this.pool.withConnection(
349349
conn,
350350
(err, conn, cb) => {
351351
if (err || !conn) {
352-
this.s.operationCount -= 1;
352+
this.decrementOperationCount();
353353
if (!err) {
354354
return cb(new MongoRuntimeError('Failed to create connection without error'));
355355
}
@@ -364,7 +364,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
364364
cmd,
365365
finalOptions,
366366
makeOperationHandler(this, conn, cmd, finalOptions, (error, response) => {
367-
this.s.operationCount -= 1;
367+
this.decrementOperationCount();
368368
cb(error, response);
369369
})
370370
);
@@ -420,6 +420,20 @@ export class Server extends TypedEventEmitter<ServerEvents> {
420420
}
421421
}
422422
}
423+
424+
/**
425+
* Decrement the operation count, returning the new count.
426+
*/
427+
private decrementOperationCount(): number {
428+
return (this.s.operationCount -= 1);
429+
}
430+
431+
/**
432+
* Increment the operation count, returning the new count.
433+
*/
434+
private incrementOperationCount(): number {
435+
return (this.s.operationCount += 1);
436+
}
423437
}
424438

425439
function calculateRoundTripTime(oldRtt: number, duration: number): number {

test/integration/server-selection/operation_count.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from 'chai';
22
import * as sinon from 'sinon';
33

44
import { AbstractCursor, Collection, ConnectionPool, MongoClient } from '../../mongodb';
5-
import { FailPoint, sleep } from '../../tools/utils';
5+
import { FailPoint } from '../../tools/utils';
66

77
const testMetadata: MongoDBMetadataUI = {
88
requires: {
@@ -118,20 +118,21 @@ describe('Server Operation Count Tests', function () {
118118
const server = Array.from(client.topology.s.servers.values())[0];
119119
expect(server.s.operationCount).to.equal(0);
120120
const commandSpy = sinon.spy(server, 'command');
121+
const incrementSpy = sinon.spy(server, 'incrementOperationCount');
122+
const decrementSpy = sinon.spy(server, 'decrementOperationCount');
121123

122124
const operationPromises = Array.from({ length: 10 }, () =>
123125
collection.insertOne({ count: 1 })
124126
);
125127

126-
// operation count is incremented after connection checkout, which happens asynchronously (even though there are plenty of connections in the pool).
127-
// we sleep to give the event loop a turn so that all the commands check out a connection before asserting the operation count
128-
await sleep(1);
129-
130-
expect(server.s.operationCount).to.equal(10);
131-
132-
await Promise.all(operationPromises);
128+
await Promise.allSettled(operationPromises);
133129

134130
expect(commandSpy.called).to.be.true;
131+
// This test is flaky when sleeping and asserting the operation count after the sleep but before the
132+
// promise execution, so we assert instead that the count was incremented 10 times and decremented 10
133+
// times - the total number of operations.
134+
expect(incrementSpy.callCount).to.equal(10);
135+
expect(decrementSpy.callCount).to.equal(10);
135136
expect(server.s.operationCount).to.equal(0);
136137
});
137138
});

0 commit comments

Comments
 (0)