Skip to content

Commit

Permalink
fix(connection): correctly bubble up timeout and disconnected events …
Browse files Browse the repository at this point in the history
…re: #8383
  • Loading branch information
vkarpov15 committed Nov 27, 2019
1 parent e6616d7 commit 02047ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,6 @@ Connection.prototype.openUri = function(uri, options, callback) {
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();
});
Expand All @@ -683,8 +680,11 @@ Connection.prototype.openUri = function(uri, options, callback) {
server.s.pool.on('timeout', () => {
_this.emit('timeout');
});
client.on('serverClosed', () => {
_this.readyState = STATES.disconnected;
});
server.s.pool.on('drain', err => {
if (err && err.name === 'MongoNetworkError') {
if (err && err.name === 'MongoNetworkError' && err.message.endsWith('timed out')) {
_this.emit('timeout');
}
});
Expand Down
5 changes: 5 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ describe('connections:', function() {
let numDisconnected = 0;
let numReconnected = 0;
let numReconnect = 0;
let numTimeout = 0;
let numClose = 0;
const conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest?heartbeatfrequencyms=1000', {
useNewUrlParser: true,
Expand All @@ -203,6 +204,9 @@ describe('connections:', function() {
conn.on('reconnect', function() {
++numReconnect;
});
conn.on('timeout', function() {
++timeout;
});
// Same as `reconnect`, just for backwards compat
conn.on('reconnected', function() {
++numReconnected;
Expand Down Expand Up @@ -241,6 +245,7 @@ describe('connections:', function() {
assert.equal(numDisconnected, 1);
assert.equal(numReconnected, 1);
assert.equal(numReconnect, 1);
assert.equal(numTimeout, 0);
assert.equal(numClose, 0);

conn.close();
Expand Down

0 comments on commit 02047ca

Please sign in to comment.