Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(connection): bubble up connected/disconnected events with unified topology #8338

Merged
merged 2 commits into from Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/connection.js
Expand Up @@ -643,11 +643,14 @@ 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('reconnect', () => {
server.s.pool.on('close', () => {
_this.readyState = STATES.disconnected;
});
server.s.topology.on('serverHeartbeatSucceeded', () => {
_handleReconnect();
});
server.s.pool.on('reconnectFailed', () => {
_this.emit('reconnectFailed');
server.s.pool.on('reconnect', () => {
_handleReconnect();
});
server.s.pool.on('timeout', () => {
_this.emit('timeout');
Expand Down
4 changes: 2 additions & 2 deletions test/connection.test.js
Expand Up @@ -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
});
Expand Down Expand Up @@ -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() {
Expand Down