Skip to content
12 changes: 12 additions & 0 deletions lib/operations/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ function connect(mongoClient, url, options, callback) {
throw new Error('no callback function provided');
}

// Has a connection already been established?
if (mongoClient.topology) {
if (mongoClient.topology.isConnected()) {
throw new Error('already connected');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this error message can be improved

}
// this is a reconnect, clear out old state
mongoClient.topology = undefined;
mongoClient.s.options = {};
mongoClient.s.dbCache = new Map();
mongoClient.s.sessions = new Set();
}

let didRequestAuthentication = false;
const logger = Logger('MongoClient', options);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"test": "npm run lint && mocha --recursive test/functional test/unit test/core",
"test-nolint": "mocha --recursive test/functional test/unit test/core",
"coverage": "istanbul cover mongodb-test-runner -- -t 60000 test/core test/unit test/functional",
"lint": "eslint lib test",
"lint": "eslint -v && eslint lib test",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR. I just sometimes get weird discrepancies between running eslint locally and what runs on evergreen. Was thinking adding this could help make things a bit more explicit around the version of eslint being used, when it is being used.

"format": "prettier --print-width 100 --tab-width 2 --single-quote --write 'test/**/*.js' 'lib/**/*.js'",
"bench": "node test/benchmarks/driverBench/",
"generate-evergreen": "node .evergreen/generate_evergreen_tasks.js",
Expand Down
30 changes: 27 additions & 3 deletions test/functional/connection.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
const test = require('./shared').assert,
setupDatabase = require('./shared').setupDatabase,
expect = require('chai').expect;
const test = require('./shared').assert;
const setupDatabase = require('./shared').setupDatabase;
const withClient = require('./shared').withClient;
const expect = require('chai').expect;

describe('Connection', function() {
before(function() {
Expand Down Expand Up @@ -514,4 +515,27 @@ describe('Connection', function() {
});
}
});

it('should be able to connect again after close', function() {
return withClient(this.configuration.newClient(), client => done => {
expect(client.isConnected()).to.be.true;
const collection = () => client.db('testReconnect').collection('test');
collection().insertOne({ a: 1 }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;
client.close(err => {
expect(err).to.not.exist;
client.connect(err => {
expect(err).to.not.exist;
collection().insertOne({ b: 2 }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;
expect(client.topology.isDestroyed()).to.be.false;
done();
});
});
});
});
});
});
});
13 changes: 12 additions & 1 deletion test/functional/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,20 @@ function makeCleanupFn(client) {
function withClient(client, operation, errorHandler) {
const cleanup = makeCleanupFn(client);

function operationHandler(client) {
// run the operation
const result = operation(client);
// if it returns a callback, wrap it in a Promise
if (typeof result === 'function') {
return new Promise(done => result(done));
}
// otherwise assume it returned a Promise
return result;
}

return client
.connect()
.then(operation, errorHandler)
.then(operationHandler, errorHandler)
.then(() => cleanup(), cleanup);
}

Expand Down