From 4ec852d28817e01f3119b2eb04618adc6d79e90e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 13 Nov 2019 17:49:07 -0500 Subject: [PATCH 1/2] fix(connection): bubble up connected/disconnected events with unified topology --- lib/connection.js | 6 ++++++ test/connection.test.js | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index 785796c1f50..6cdd4e64dc5 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -643,6 +643,12 @@ Connection.prototype.openUri = function(uri, options, callback) { if (options.useUnifiedTopology) { if (type === 'Single') { const server = Array.from(db.s.topology.s.servers.values())[0]; + server.s.pool.on('close', () => { + _this.readyState = STATES.disconnected; + }); + server.s.topology.on('serverHeartbeatSucceeded', () => { + _handleReconnect(); + }); server.s.pool.on('reconnect', () => { _handleReconnect(); }); diff --git a/test/connection.test.js b/test/connection.test.js index 6a4a61a4697..f7c4a4e4852 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -189,7 +189,7 @@ describe('connections:', function() { let numReconnected = 0; let numReconnect = 0; let numClose = 0; - const conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest', { + const conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest?heartbeatfrequencyms=1000', { useNewUrlParser: true, useUnifiedTopology: true }); From e5c6ded7996953b4fe146e6ce9a75041a7849509 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 14 Nov 2019 10:22:55 -0500 Subject: [PATCH 2/2] test(connection): fix tests re: #8337 --- lib/connection.js | 3 --- test/connection.test.js | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 6cdd4e64dc5..a8e418f49a9 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -652,9 +652,6 @@ Connection.prototype.openUri = function(uri, options, callback) { server.s.pool.on('reconnect', () => { _handleReconnect(); }); - server.s.pool.on('reconnectFailed', () => { - _this.emit('reconnectFailed'); - }); server.s.pool.on('timeout', () => { _this.emit('timeout'); }); diff --git a/test/connection.test.js b/test/connection.test.js index f7c4a4e4852..16b05ac3305 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -260,7 +260,7 @@ describe('connections:', function() { reconnectTries: 3, reconnectInterval: 100, useNewUrlParser: true, - useUnifiedTopology: true + useUnifiedTopology: false // reconnectFailed doesn't get emitted with 'useUnifiedTopology' }); conn.on('connected', function() {