Skip to content

Commit cb107a8

Browse files
committed
fix: add calculated duration to server as roundTripTime
Round trip time was calculated and reported for servers in the unified topology, but not marked for consideration during server selection. NODE-2356
1 parent faab9ad commit cb107a8

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/core/sdam/monitoring.js

+3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ function monitorServer(server, options) {
172172
return callback(err, null);
173173
}
174174

175+
// save round trip time
176+
server.description.roundTripTime = duration;
177+
175178
const isMaster = result.result;
176179
server.emit(
177180
'serverHeartbeatSucceeded',

test/unit/sdam/monitoring.test.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
const mock = require('mongodb-mock-server');
3+
const Topology = require('../../../lib/core/sdam/topology').Topology;
4+
const expect = require('chai').expect;
5+
6+
describe('monitoring', function() {
7+
let server;
8+
9+
after(() => mock.cleanup());
10+
beforeEach(function() {
11+
return mock.createServer().then(_server => (server = _server));
12+
});
13+
14+
it('should record roundTripTime', function(done) {
15+
server.setMessageHandler(request => {
16+
const doc = request.document;
17+
if (doc.ismaster) {
18+
request.reply(Object.assign({}, mock.DEFAULT_ISMASTER));
19+
} else if (doc.endSessions) {
20+
request.reply({ ok: 1 });
21+
}
22+
});
23+
24+
// set `heartbeatFrequencyMS` to 250ms to force a quick monitoring check, and wait 500ms to validate below
25+
const topology = new Topology(server.uri(), { heartbeatFrequencyMS: 250 });
26+
topology.connect(err => {
27+
expect(err).to.not.exist;
28+
29+
setTimeout(() => {
30+
expect(topology)
31+
.property('description')
32+
.property('servers')
33+
.to.have.length(1);
34+
35+
const serverDescription = Array.from(topology.description.servers.values())[0];
36+
expect(serverDescription)
37+
.property('roundTripTime')
38+
.to.be.greaterThan(0);
39+
40+
topology.close(done);
41+
}, 500);
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)