Skip to content

Commit

Permalink
Merge branch 'main' into NODE-4929
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken authored May 30, 2023
2 parents 453008b + 1e58a4c commit 968ee46
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
20 changes: 17 additions & 3 deletions src/sdam/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ export class Server extends TypedEventEmitter<ServerEvents> {
return;
}

this.s.operationCount += 1;
this.incrementOperationCount();

this.pool.withConnection(
conn,
(err, conn, cb) => {
if (err || !conn) {
this.s.operationCount -= 1;
this.decrementOperationCount();
if (!err) {
return cb(new MongoRuntimeError('Failed to create connection without error'));
}
Expand All @@ -364,7 +364,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
cmd,
finalOptions,
makeOperationHandler(this, conn, cmd, finalOptions, (error, response) => {
this.s.operationCount -= 1;
this.decrementOperationCount();
cb(error, response);
})
);
Expand Down Expand Up @@ -420,6 +420,20 @@ export class Server extends TypedEventEmitter<ServerEvents> {
}
}
}

/**
* Decrement the operation count, returning the new count.
*/
private decrementOperationCount(): number {
return (this.s.operationCount -= 1);
}

/**
* Increment the operation count, returning the new count.
*/
private incrementOperationCount(): number {
return (this.s.operationCount += 1);
}
}

function calculateRoundTripTime(oldRtt: number, duration: number): number {
Expand Down
17 changes: 9 additions & 8 deletions test/integration/server-selection/operation_count.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import * as sinon from 'sinon';

import { AbstractCursor, Collection, ConnectionPool, MongoClient } from '../../mongodb';
import { FailPoint, sleep } from '../../tools/utils';
import { FailPoint } from '../../tools/utils';

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

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

// operation count is incremented after connection checkout, which happens asynchronously (even though there are plenty of connections in the pool).
// we sleep to give the event loop a turn so that all the commands check out a connection before asserting the operation count
await sleep(1);

expect(server.s.operationCount).to.equal(10);

await Promise.all(operationPromises);
await Promise.allSettled(operationPromises);

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

0 comments on commit 968ee46

Please sign in to comment.