Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert "fix: allow client connect after close (#2581)" #2591

Merged
merged 1 commit into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,29 +349,19 @@ 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) {
completeClose();
return;
return cb();
}

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

autoEncrypter.teardown(force, err2 => completeClose(err || err2));
autoEncrypter.teardown(force, err2 => cb(err || err2));
});
});
}
Expand Down
5 changes: 0 additions & 5 deletions src/operations/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,6 @@ 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: 0 additions & 31 deletions test/functional/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
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 @@ -274,34 +273,4 @@ 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).to.not.exist;
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
});
});
});
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).to.not.exist;
expect(client.topology.s.sessionPool.sessions).to.have.length(0);
});
});
}
Expand Down