Skip to content

Commit

Permalink
fix(NODE-4188): default localThresholdMS to 15ms (#3207)
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson authored Apr 22, 2022
1 parent 3f8765a commit 5e730ff
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/sdam/topology_description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
});
5 changes: 0 additions & 5 deletions test/unit/assorted/server_selection.spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
73 changes: 73 additions & 0 deletions test/unit/sdam/server_selection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});
});

0 comments on commit 5e730ff

Please sign in to comment.