diff --git a/lib/core/sdam/monitor.js b/lib/core/sdam/monitor.js index 35ce6b4e15..095e54b3d6 100644 --- a/lib/core/sdam/monitor.js +++ b/lib/core/sdam/monitor.js @@ -213,33 +213,35 @@ function rescheduleMonitoring(monitor, ms) { return; } + stateTransition(monitor, STATE_IDLE); + monitor[kLastCheckTime] = process.hrtime(); monitor[kMonitorId] = setTimeout(() => { monitor[kMonitorId] = undefined; monitor.requestCheck(); }, ms || heartbeatFrequencyMS); - - stateTransition(monitor, STATE_IDLE); } function successHandler(monitor, start, isMaster) { + rescheduleMonitoring(monitor); + process.nextTick(() => monitor.emit( 'serverHeartbeatSucceeded', new ServerHeartbeatSucceededEvent(calculateDurationInMs(start), isMaster, monitor.address) ) ); - - rescheduleMonitoring(monitor); } function failureHandler(monitor, start, err) { - monitor.emit( - 'serverHeartbeatFailed', - new ServerHeartbeatFailedEvent(calculateDurationInMs(start), err, monitor.address) - ); - rescheduleMonitoring(monitor); + + process.nextTick(() => + monitor.emit( + 'serverHeartbeatFailed', + new ServerHeartbeatFailedEvent(calculateDurationInMs(start), err, monitor.address) + ) + ); } module.exports = { diff --git a/test/unit/sdam/monitoring.test.js b/test/unit/sdam/monitoring.test.js index 23dc1155b1..8bac318883 100644 --- a/test/unit/sdam/monitoring.test.js +++ b/test/unit/sdam/monitoring.test.js @@ -78,8 +78,7 @@ describe('monitoring', function() { acceptConnections = true; }, 250); - // set `heartbeatFrequencyMS` to 250ms to force a quick monitoring check, and wait 500ms to validate below - const topology = new Topology(mockServer.uri(), { heartbeatFrequencyMS: 250 }); + const topology = new Topology(mockServer.uri()); topology.connect(err => { expect(err).to.not.exist;