diff --git a/src/sdam/topology_description.ts b/src/sdam/topology_description.ts index c9d5736693..34db44f0eb 100644 --- a/src/sdam/topology_description.ts +++ b/src/sdam/topology_description.ts @@ -63,7 +63,7 @@ export class TopologyDescription { this.stale = false; this.compatible = true; this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 0; - this.localThresholdMS = options.localThresholdMS ?? 0; + this.localThresholdMS = options.localThresholdMS ?? 15; if (setName) { this.setName = setName; diff --git a/test/integration/server-discovery-and-monitoring/topology_description.test.ts b/test/integration/server-discovery-and-monitoring/topology_description.test.ts new file mode 100644 index 0000000000..9fdeeab95a --- /dev/null +++ b/test/integration/server-discovery-and-monitoring/topology_description.test.ts @@ -0,0 +1,32 @@ +import { expect } from 'chai'; + +import { MongoClient, MongoClientOptions } from '../../../src/mongo_client'; +import { getTopology } from '../../../src/utils'; + +describe('TopologyDescription (integration tests)', function () { + let client: MongoClient; + + afterEach(async function () { + await client.close(); + }); + + context('options', function () { + context('localThresholdMS', function () { + it('should default to 15ms', async function () { + const options: MongoClientOptions = {}; + client = await this.configuration.newClient(options).connect(); + const topologyDescription = getTopology(client).description; + expect(topologyDescription).to.have.ownProperty('localThresholdMS').to.equal(15); + }); + + it('should be set to the localThresholdMS option when it is passed in', async function () { + const options: MongoClientOptions = { + localThresholdMS: 30 + }; + client = await this.configuration.newClient(options).connect(); + const topologyDescription = getTopology(client).description; + expect(topologyDescription).to.have.ownProperty('localThresholdMS').to.equal(30); + }); + }); + }); +}); diff --git a/test/unit/assorted/server_selection.spec.test.ts b/test/unit/assorted/server_selection.spec.test.ts index 2939c546b4..7e81eed3ac 100644 --- a/test/unit/assorted/server_selection.spec.test.ts +++ b/test/unit/assorted/server_selection.spec.test.ts @@ -11,11 +11,6 @@ describe('Server Selection Logic (spec)', function () { this.currentTest.skipReason = 'Nodejs driver does not support PossiblePrimary'; this.skip(); } - - if (this.currentTest.title.match(/nearest_multiple/i)) { - this.currentTest.skipReason = 'TODO(NODE-4188): localThresholdMS should default to 15ms'; - this.skip(); - } }); const selectionSpecDir = join(__dirname, '../../spec/server-selection/server_selection'); diff --git a/test/unit/sdam/server_selection.test.js b/test/unit/sdam/server_selection.test.js index 4f32651be2..03c3188823 100644 --- a/test/unit/sdam/server_selection.test.js +++ b/test/unit/sdam/server_selection.test.js @@ -435,5 +435,78 @@ describe('server selection', function () { }); }); }); + + context('localThresholdMS is respected as an option', function () { + let serverDescription1, serverDescription2, serverDescription3, serverDescriptions; + beforeEach(() => { + serverDescription1 = new ServerDescription( + '127.0.0.1:27017', + { + setName: 'test', + isWritablePrimary: true, + ok: 1 + }, + { roundTripTime: 15 } + ); + serverDescription2 = new ServerDescription( + '127.0.0.1:27018', + { + setName: 'test', + secondary: true, + ok: 1 + }, + { roundTripTime: 25 } + ); + serverDescription3 = new ServerDescription( + '127.0.0.1:27019', + { + setName: 'test', + secondary: true, + ok: 1 + }, + { roundTripTime: 35 } + ); + serverDescriptions = new Map(); + serverDescriptions.set(serverDescription1.address, serverDescription1); + serverDescriptions.set(serverDescription2.address, serverDescription2); + serverDescriptions.set(serverDescription3.address, serverDescription3); + }); + it('includes servers inside the latency window with default localThresholdMS', function () { + const topologyDescription = new TopologyDescription( + TopologyType.Single, + serverDescriptions, + 'test', + MIN_SECONDARY_WRITE_WIRE_VERSION, + new ObjectId(), + MIN_SECONDARY_WRITE_WIRE_VERSION + ); + const selector = secondaryWritableServerSelector(); + const servers = selector(topologyDescription, Array.from(serverDescriptions.values())); + expect(servers).to.have.lengthOf(2); + const selectedAddresses = new Set(servers.map(({ address }) => address)); + expect(selectedAddresses.has(serverDescription1.address)).to.be.true; + expect(selectedAddresses.has(serverDescription2.address)).to.be.true; + expect(selectedAddresses.has(serverDescription3.address)).to.be.false; + }); + + it('includes servers inside the latency window with custom localThresholdMS', function () { + const topologyDescription = new TopologyDescription( + TopologyType.Single, + serverDescriptions, + 'test', + MIN_SECONDARY_WRITE_WIRE_VERSION, + new ObjectId(), + MIN_SECONDARY_WRITE_WIRE_VERSION, + { localThresholdMS: 5 } + ); + const selector = secondaryWritableServerSelector(); + const servers = selector(topologyDescription, Array.from(serverDescriptions.values())); + expect(servers).to.have.lengthOf(1); + const selectedAddresses = new Set(servers.map(({ address }) => address)); + expect(selectedAddresses.has(serverDescription1.address)).to.be.true; + expect(selectedAddresses.has(serverDescription2.address)).to.be.false; + expect(selectedAddresses.has(serverDescription3.address)).to.be.false; + }); + }); }); });