Skip to content

Commit

Permalink
fix: allow client connect after close (#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
HanaPearlman authored Oct 16, 2020
1 parent d7e505a commit 1aecf96
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,29 @@ export class MongoClient extends EventEmitter implements OperationParent {
const force = typeof forceOrCallback === 'boolean' ? forceOrCallback : false;

return maybePromise(callback, cb => {
const completeClose = (err?: AnyError) => {
// clear out references to old topology
this.topology = undefined;
this.s.dbCache = new Map();
this.s.sessions = new Set();

cb(err);
};

if (this.topology == null) {
return cb();
completeClose();
return;
}

const topology = this.topology;
topology.close({ force }, err => {
const autoEncrypter = topology.s.options.autoEncrypter;
if (!autoEncrypter) {
cb(err);
completeClose(err);
return;
}

autoEncrypter.teardown(force, err2 => cb(err || err2));
autoEncrypter.teardown(force, err2 => completeClose(err || err2));
});
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/operations/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ export function connect(
throw new Error('no callback function provided');
}

// Has a connection already been established?
if (mongoClient.topology && mongoClient.topology.isConnected()) {
throw new MongoError(`'connect' cannot be called when already connected`);
}

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

Expand Down
31 changes: 31 additions & 0 deletions test/functional/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const test = require('./shared').assert,
setupDatabase = require('./shared').setupDatabase,
expect = require('chai').expect;
const withClient = require('./shared').withClient;

describe('Connection', function () {
before(function () {
Expand Down Expand Up @@ -273,4 +274,34 @@ describe('Connection', function () {
done();
}
});

it(
'should be able to connect again after close',
withClient(function (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;
expect(client.isConnected()).to.be.false;

client.connect(err => {
expect(err).to.not.exist;
expect(client.isConnected()).to.be.true;

collection().insertOne({ b: 2 }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;
expect(client.topology.isDestroyed()).to.be.false;
done();
});
});
});
});
})
);
});
4 changes: 2 additions & 2 deletions test/functional/sessions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('Sessions', function () {
// verify that the `endSessions` command was sent
const lastCommand = test.commands.started[test.commands.started.length - 1];
expect(lastCommand.commandName).to.equal('endSessions');
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
expect(client.topology).to.not.exist;
});
});
});
Expand All @@ -143,7 +143,7 @@ describe('Sessions', function () {
// verify that the `endSessions` command was sent
const lastCommand = test.commands.started[test.commands.started.length - 1];
expect(lastCommand.commandName).to.equal('endSessions');
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
expect(client.topology).to.not.exist;
});
});
}
Expand Down

0 comments on commit 1aecf96

Please sign in to comment.