Skip to content

Commit 9460f9f

Browse files
address comments
1 parent a880ecc commit 9460f9f

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/sdam/server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ export class Server extends TypedEventEmitter<ServerEvents> {
328328
// NOTE: This is a hack! We can't retrieve the connections used for executing an operation
329329
// (and prevent them from being checked back in) at the point of operation execution.
330330
// This should be considered as part of the work for NODE-2882
331+
// NOTE:
332+
// When incrementing operation count, it's important that we increment it before we
333+
// attempt to check out a connection from the pool. This ensures that operations that
334+
// are waiting for a connection are included in the operation count. Load balanced
335+
// mode will only ever have a single server, so the operation count doesn't matter.
331336
if (this.loadBalanced && session && conn == null && isPinnableCommand(cmd, session)) {
332337
this.s.pool.checkOut((err, checkedOut) => {
333338
if (err || checkedOut == null) {

test/integration/server-selection/server_selection_operation_count.prose.test.ts renamed to test/integration/server-selection/server_selection.prose.operation_count.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ async function ensurePoolIsFull(client: MongoClient) {
4141
throw new Error('Connection pool did not fill up');
4242
}
4343
}
44+
45+
// Step 1: Configure a sharded cluster with two mongoses. Use a 4.2.9 or newer server version.
4446
const TEST_METADATA: MongoDBMetadataUI = { requires: { mongodb: '>=4.2.9', topology: 'sharded' } };
4547

46-
describe('Server Selection Operation Count Prose', function () {
48+
describe('operationCount-based Selection Within Latency Window - Prose Test', function () {
4749
let client: MongoClient;
4850
let seeds: Array<string>;
4951
let counts: Record<string, number> = {};

test/unit/assorted/server_selection_latency_window_utils.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,26 @@ export function loadLatencyWindowTests(directory: string) {
5151

5252
function compareResultsToExpected(
5353
{ tolerance, expected_frequencies }: Outcome,
54-
observed_frequencies: FrequencyMap
54+
observedFrequencies: FrequencyMap
5555
) {
56-
const expectedFrequencies = Object.entries(expected_frequencies);
5756
expect(
58-
expectedFrequencies,
57+
Object.entries(expected_frequencies),
5958
'Encountered an empty set of frequencies to assert on. Is there something wrong with the test or the runner?'
6059
).to.have.length.greaterThan(0);
6160
for (const [address, frequency] of Object.entries(expected_frequencies)) {
6261
if (frequency === 0) {
63-
expect(observed_frequencies).not.to.haveOwnProperty(address);
62+
expect(observedFrequencies).not.to.haveOwnProperty(address);
6463
} else {
65-
expect(observed_frequencies).to.haveOwnProperty(address).to.exist;
66-
const actual_frequency = observed_frequencies[address];
67-
const is_too_low = actual_frequency < frequency - tolerance;
68-
expect(
69-
is_too_low,
70-
`expected frequency of ${frequency}+/-${tolerance} but received ${actual_frequency}`
71-
).to.be.false;
72-
const is_too_high = actual_frequency > frequency + tolerance;
73-
expect(
74-
is_too_high,
75-
`expected frequency of ${frequency}+/-${tolerance} but received ${actual_frequency}`
76-
).to.be.false;
64+
expect(observedFrequencies).to.haveOwnProperty(address).that.is.a('number');
65+
const actualFrequency = observedFrequencies[address];
66+
const isTooLow = actualFrequency < frequency - tolerance;
67+
const isTooHigh = actualFrequency > frequency + tolerance;
68+
69+
if (isTooHigh || isTooLow) {
70+
expect.fail(
71+
`expected frequency of ${frequency}+/-${tolerance} but received ${actualFrequency}`
72+
);
73+
}
7774
}
7875
}
7976
}

0 commit comments

Comments
 (0)