Skip to content

Commit 5e730ff

Browse files
fix(NODE-4188): default localThresholdMS to 15ms (#3207)
1 parent 3f8765a commit 5e730ff

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

src/sdam/topology_description.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class TopologyDescription {
6363
this.stale = false;
6464
this.compatible = true;
6565
this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 0;
66-
this.localThresholdMS = options.localThresholdMS ?? 0;
66+
this.localThresholdMS = options.localThresholdMS ?? 15;
6767

6868
if (setName) {
6969
this.setName = setName;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { expect } from 'chai';
2+
3+
import { MongoClient, MongoClientOptions } from '../../../src/mongo_client';
4+
import { getTopology } from '../../../src/utils';
5+
6+
describe('TopologyDescription (integration tests)', function () {
7+
let client: MongoClient;
8+
9+
afterEach(async function () {
10+
await client.close();
11+
});
12+
13+
context('options', function () {
14+
context('localThresholdMS', function () {
15+
it('should default to 15ms', async function () {
16+
const options: MongoClientOptions = {};
17+
client = await this.configuration.newClient(options).connect();
18+
const topologyDescription = getTopology(client).description;
19+
expect(topologyDescription).to.have.ownProperty('localThresholdMS').to.equal(15);
20+
});
21+
22+
it('should be set to the localThresholdMS option when it is passed in', async function () {
23+
const options: MongoClientOptions = {
24+
localThresholdMS: 30
25+
};
26+
client = await this.configuration.newClient(options).connect();
27+
const topologyDescription = getTopology(client).description;
28+
expect(topologyDescription).to.have.ownProperty('localThresholdMS').to.equal(30);
29+
});
30+
});
31+
});
32+
});

test/unit/assorted/server_selection.spec.test.ts

-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ describe('Server Selection Logic (spec)', function () {
1111
this.currentTest.skipReason = 'Nodejs driver does not support PossiblePrimary';
1212
this.skip();
1313
}
14-
15-
if (this.currentTest.title.match(/nearest_multiple/i)) {
16-
this.currentTest.skipReason = 'TODO(NODE-4188): localThresholdMS should default to 15ms';
17-
this.skip();
18-
}
1914
});
2015

2116
const selectionSpecDir = join(__dirname, '../../spec/server-selection/server_selection');

test/unit/sdam/server_selection.test.js

+73
Original file line numberDiff line numberDiff line change
@@ -435,5 +435,78 @@ describe('server selection', function () {
435435
});
436436
});
437437
});
438+
439+
context('localThresholdMS is respected as an option', function () {
440+
let serverDescription1, serverDescription2, serverDescription3, serverDescriptions;
441+
beforeEach(() => {
442+
serverDescription1 = new ServerDescription(
443+
'127.0.0.1:27017',
444+
{
445+
setName: 'test',
446+
isWritablePrimary: true,
447+
ok: 1
448+
},
449+
{ roundTripTime: 15 }
450+
);
451+
serverDescription2 = new ServerDescription(
452+
'127.0.0.1:27018',
453+
{
454+
setName: 'test',
455+
secondary: true,
456+
ok: 1
457+
},
458+
{ roundTripTime: 25 }
459+
);
460+
serverDescription3 = new ServerDescription(
461+
'127.0.0.1:27019',
462+
{
463+
setName: 'test',
464+
secondary: true,
465+
ok: 1
466+
},
467+
{ roundTripTime: 35 }
468+
);
469+
serverDescriptions = new Map();
470+
serverDescriptions.set(serverDescription1.address, serverDescription1);
471+
serverDescriptions.set(serverDescription2.address, serverDescription2);
472+
serverDescriptions.set(serverDescription3.address, serverDescription3);
473+
});
474+
it('includes servers inside the latency window with default localThresholdMS', function () {
475+
const topologyDescription = new TopologyDescription(
476+
TopologyType.Single,
477+
serverDescriptions,
478+
'test',
479+
MIN_SECONDARY_WRITE_WIRE_VERSION,
480+
new ObjectId(),
481+
MIN_SECONDARY_WRITE_WIRE_VERSION
482+
);
483+
const selector = secondaryWritableServerSelector();
484+
const servers = selector(topologyDescription, Array.from(serverDescriptions.values()));
485+
expect(servers).to.have.lengthOf(2);
486+
const selectedAddresses = new Set(servers.map(({ address }) => address));
487+
expect(selectedAddresses.has(serverDescription1.address)).to.be.true;
488+
expect(selectedAddresses.has(serverDescription2.address)).to.be.true;
489+
expect(selectedAddresses.has(serverDescription3.address)).to.be.false;
490+
});
491+
492+
it('includes servers inside the latency window with custom localThresholdMS', function () {
493+
const topologyDescription = new TopologyDescription(
494+
TopologyType.Single,
495+
serverDescriptions,
496+
'test',
497+
MIN_SECONDARY_WRITE_WIRE_VERSION,
498+
new ObjectId(),
499+
MIN_SECONDARY_WRITE_WIRE_VERSION,
500+
{ localThresholdMS: 5 }
501+
);
502+
const selector = secondaryWritableServerSelector();
503+
const servers = selector(topologyDescription, Array.from(serverDescriptions.values()));
504+
expect(servers).to.have.lengthOf(1);
505+
const selectedAddresses = new Set(servers.map(({ address }) => address));
506+
expect(selectedAddresses.has(serverDescription1.address)).to.be.true;
507+
expect(selectedAddresses.has(serverDescription2.address)).to.be.false;
508+
expect(selectedAddresses.has(serverDescription3.address)).to.be.false;
509+
});
510+
});
438511
});
439512
});

0 commit comments

Comments
 (0)