Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(pool): ensure inUse and connecting queues are cleared on reauth
Browse files Browse the repository at this point in the history
NODE-1153
  • Loading branch information
mbroadst committed Oct 12, 2017
1 parent 8f8ad56 commit aa2840d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/connection/pool.js
Expand Up @@ -774,6 +774,8 @@ Pool.prototype.auth = function(mechanism) {
var connections = self.allConnections();
// Allow nothing else to use the connections while we authenticate them
self.availableConnections = [];
self.inUseConnections = [];
self.connectingConnections = [];

var connectionsCount = connections.length;
var error = null;
Expand Down Expand Up @@ -1489,6 +1491,7 @@ function _execute(self) {
}

if (writeSuccessful && workItem.immediateRelease && self.authenticating) {
removeConnection(self, connection);
self.nonAuthenticatedConnections.push(connection);
} else if (writeSuccessful === false) {
// If write not successful put back on queue
Expand Down
64 changes: 63 additions & 1 deletion test/tests/functional/pool_tests.js
Expand Up @@ -6,7 +6,9 @@ var expect = require('chai').expect,
Pool = require('../../../lib/connection/pool'),
Connection = require('../../../lib/connection/connection'),
Query = require('../../../lib/connection/commands').Query,
Bson = require('bson');
Bson = require('bson'),
co = require('co'),
mock = require('../../mock');

describe('Pool tests', function() {
it.skip('should correctly connect pool to single server', {
Expand Down Expand Up @@ -1183,4 +1185,64 @@ describe('Pool tests', function() {
pool.connect();
}
});

it('should remove all connections from further use during reauthentication of a pool', {
metadata: { requires: { topology: 'single' } },

test: function(done) {
co(function*() {
const server = yield mock.createServer(37017, 'localhost');
server.setMessageHandler(request => {
var doc = request.document;
if (doc.getnonce) {
request.reply({ ok: 1, result: { nonce: 'testing' } });
} else if (doc.authenticate) {
request.reply({ ok: 1 });
} else if (doc.ismaster) {
setTimeout(() => request.reply({ ok: 1 }), 10000);
}
});

var pool = new Pool(null, {
host: 'localhost',
port: 37017,
bson: new Bson(),
size: 10
});

var query = new Query(
new Bson(),
'system.$cmd',
{ ismaster: true },
{ numberToSkip: 0, numberToReturn: 1 }
);

pool.on('connect', function() {
pool.write(query, { monitoring: true }, function() {});

setTimeout(function() {
pool.auth('mongocr', 'test', 'admin', 'admin', function(err) {
expect(err).to.not.exist;

// ensure that there are no duplicates in the available connection queue
var availableIds = pool.availableConnections.map(conn => conn.id);
availableIds.forEach(function(id, pos, arr) {
expect(arr.indexOf(id)).to.equal(pos);
});

expect(pool.availableConnections).to.have.length(1);
expect(pool.inUseConnections).to.have.length(0);

pool.destroy(true);
expect(Object.keys(Connection.connections())).to.have.length(0);
Connection.disableConnectionAccounting();
done();
});
}, 500);
});

pool.connect();
});
}
});
});

0 comments on commit aa2840d

Please sign in to comment.