diff --git a/lib/connection/connection.js b/lib/connection/connection.js index d7fa5f680..cf1505446 100644 --- a/lib/connection/connection.js +++ b/lib/connection/connection.js @@ -600,7 +600,7 @@ Connection.prototype.connect = function(_options) { }); self.connection.setTimeout(self.connectionTimeout); } else { - self.connection.on('connect', function() { + self.connection.once('connect', function() { // Set socket timeout instead of connection timeout self.connection.setTimeout(self.socketTimeout); // Emit connect event diff --git a/test/tests/functional/pool_tests.js b/test/tests/functional/pool_tests.js index 077e50f3f..32e22dff8 100644 --- a/test/tests/functional/pool_tests.js +++ b/test/tests/functional/pool_tests.js @@ -39,6 +39,53 @@ describe('Pool tests', function() { } }); + it.only('Should only listen on connect once', { + metadata: { requires: { topology: 'single' } }, + + test: function(done) { + // Enable connections accounting + Connection.enableConnectionAccounting(); + + // Attempt to connect + var pool = new Pool(null, { + host: this.configuration.host, + port: this.configuration.port, + bson: new Bson(), + messageHandler: function() {} + }); + + let connection; + + // Add event listeners + pool.on('connect', function(_pool) { + var connections = _pool.allConnections(); + + process.nextTick(() => { + // Now that we are in next tick, connection should still exist, but there + // should be no connect listeners + expect(connection.connection.listenerCount('connect')).to.equal(0); + expect(connections).to.have.lengthOf(1); + + _pool.destroy(); + + // Connection should be gone after destroy + expect(_pool.allConnections()).to.have.lengthOf(0); + Connection.disableConnectionAccounting(); + done(); + }); + }); + + expect(pool.allConnections()).to.have.lengthOf(0); + + // Start connection + pool.connect(); + + expect(pool.allConnections()).to.have.lengthOf(1); + connection = pool.allConnections()[0]; + expect(connection.connection.listenerCount('connect')).to.equal(1); + } + }); + it('should correctly write ismaster operation to the server', { metadata: { requires: { topology: 'single' } },