Skip to content

Commit d341c01

Browse files
committed
refactor: use most recently seen error on second monitor failure
1 parent 8d9c290 commit d341c01

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

lib/core/sdam/monitor.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,15 @@ function monitorServer(monitor) {
182182
// TODO: the next line is a legacy event, remove in v4
183183
process.nextTick(() => monitor.emit('monitoring', monitor[kServer]));
184184

185-
checkServer(monitor, (err, isMaster) => {
185+
checkServer(monitor, (e0, isMaster) => {
186186
if (isMaster) {
187187
successHandler(monitor, start, isMaster);
188188
return;
189189
}
190190

191191
// otherwise an error occured on initial discovery, also bail
192192
if (monitor[kServer].description.type === ServerType.Unknown) {
193-
failureHandler(monitor, start, err);
193+
failureHandler(monitor, start, e0);
194194
return;
195195
}
196196

@@ -199,10 +199,9 @@ function monitorServer(monitor) {
199199
// change its type to `Unknown` only after retrying once.
200200
monitor.emit('resetConnectionPool');
201201

202-
checkServer(monitor, (error, isMaster) => {
203-
if (error) {
204-
// NOTE: using the _first_ error encountered here
205-
failureHandler(monitor, start, err);
202+
checkServer(monitor, (e1, isMaster) => {
203+
if (e1) {
204+
failureHandler(monitor, start, e1);
206205
return;
207206
}
208207

test/unit/sdam/monitoring.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,57 @@ describe('monitoring', function() {
256256

257257
monitor.connect();
258258
});
259+
260+
it('should report the most recent error on second monitoring failure', function(done) {
261+
let failedCount = 0;
262+
let initialConnectCompleted = false;
263+
mockServer.setMessageHandler(request => {
264+
const doc = request.document;
265+
if (doc.ismaster) {
266+
if (!initialConnectCompleted) {
267+
request.reply(mock.DEFAULT_ISMASTER_36);
268+
initialConnectCompleted = true;
269+
return;
270+
}
271+
272+
if (failedCount === 0) {
273+
failedCount++;
274+
request.reply({ ok: 0, errmsg: 'first error message' });
275+
} else {
276+
failedCount++;
277+
request.reply({ ok: 0, errmsg: 'second error message' });
278+
}
279+
}
280+
});
281+
282+
const server = new MockServer(mockServer.address());
283+
const monitor = new Monitor(server, {
284+
heartbeatFrequencyMS: 250,
285+
minHeartbeatFrequencyMS: 50
286+
});
287+
this.defer(() => monitor.close());
288+
289+
let resetRequested = false;
290+
monitor.on('resetConnectionPool', () => (resetRequested = true));
291+
monitor.on('serverHeartbeatSucceeded', () => {
292+
if (server.description.type === ServerType.Unknown) {
293+
// this is the first successful heartbeat, set the server type
294+
server.description.type = ServerType.Standalone;
295+
return;
296+
}
297+
298+
done(new Error('unexpected heartbeat success'));
299+
});
300+
301+
monitor.on('serverHeartbeatFailed', event => {
302+
expect(resetRequested).to.be.true;
303+
expect(event)
304+
.property('failure')
305+
.to.match(/second error message/);
306+
done();
307+
});
308+
309+
monitor.connect();
310+
});
259311
});
260312
});

0 commit comments

Comments
 (0)