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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-4192): make MongoClient.connect optional #3232

Merged
merged 9 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ declare global {
* An optional string the test author can attach to print out why a test is skipped
*
* @example
* ```
* ```ts
* it.skip('my test', () => {
* //...
* }).skipReason = 'TODO(NODE-XXXX): Feature implementation impending!';
* ```
*
* The reporter (`test/tools/reporter/mongodb_reporter.js`) will print out the skipReason
* indented directly below the test name.
* ```
* ```txt
* - my test
* - TODO(NODE-XXXX): Feature implementation impending!
* ```
*
* You can also skip a set of tests via beforeEach:
* ```
* ```ts
* beforeEach(() => {
* if ('some condition') {
* this.currentTest.skipReason = 'requires <run condition> to run';
Expand Down
186 changes: 106 additions & 80 deletions test/integration/node-specific/mongo_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,96 +510,122 @@ describe('class MongoClient', function () {
expect(client).to.have.property('topology').that.is.instanceOf(Topology);
}
);
});

it(
'client.close will not permit operations to auto reconnect',
{ requires: { auth: 'enabled' } },
async function () {
await client.db('test').collection('test').findOne();

expect(client).to.be.instanceOf(MongoClient);
expect(client).to.have.property('topology').that.is.instanceOf(Topology);

await client.close();

expect(client).to.have.property('topology', undefined);

const result = await client
.db('test')
.collection('test')
.findOne()
.catch(error => error);

expect(client).to.have.property('topology', undefined);
expect(result).to.be.instanceOf(MongoNotConnectedError);

await client.close();
}
);

it(
'client.close will not permit operations to auto reconnect permanently',
{ requires: { auth: 'enabled' } },
async function () {
expect(client.s).to.have.property('hasBeenClosed', false);

await client.db('test').collection('test').findOne();

expect(client).to.be.instanceOf(MongoClient);
expect(client).to.have.property('topology').that.is.instanceOf(Topology);

await client.close();

expect(client).to.have.property('topology', undefined);

const firstResult = await client
.db('test')
.collection('test')
.findOne()
.catch(error => error);
context('#close()', () => {
let client: MongoClient;
let db: Db;

const RD_ONLY_HAS_BEEN_CLOSED = {
value: true,
enumerable: true,
configurable: false,
writable: false
};

const INIT_HAS_BEEN_CLOSED = {
value: false,
enumerable: true,
configurable: true,
writable: true
};

expect(client).to.have.property('topology', undefined);
expect(firstResult).to.be.instanceOf(MongoNotConnectedError);
beforeEach(function () {
client = this.configuration.newClient();
db = client.db();
});

const postCloseHasBeenClosedDescriptor = {
value: true,
enumerable: true,
configurable: false,
writable: false
};
afterEach(async function () {
await client.close();
db = null;
});

expect(client.s).to.have.ownPropertyDescriptor(
'hasBeenClosed',
postCloseHasBeenClosedDescriptor
);
it('prevents automatic connection on a closed non-connected client', async () => {
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', INIT_HAS_BEEN_CLOSED);
await client.close();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', RD_ONLY_HAS_BEEN_CLOSED);
const error = await db.command({ ping: 1 }).catch(error => error);
expect(error).to.be.instanceOf(MongoNotConnectedError);
});

const pingCommandToBeStarted = once(client, 'commandStarted');
await client.connect(); // explicitly connect again
const [pingOnConnect] = await pingCommandToBeStarted;
it('allows explicit connection on a closed non-connected client', async () => {
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', INIT_HAS_BEEN_CLOSED);
await client.close();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', RD_ONLY_HAS_BEEN_CLOSED);
await client.connect();
const result = await db.command({ ping: 1 }).catch(error => error);
expect(result).to.not.be.instanceOf(MongoNotConnectedError);
expect(result).to.have.property('ok', 1);
});

expect(pingOnConnect).to.have.property('commandName', 'ping');
it('prevents automatic reconnect on a closed previously connected client', async () => {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
await client.connect();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', INIT_HAS_BEEN_CLOSED);
await client.close();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', RD_ONLY_HAS_BEEN_CLOSED);
const error = await db.command({ ping: 1 }).catch(error => error);
expect(error).to.be.instanceOf(MongoNotConnectedError);
});

expect(client.s).to.have.ownPropertyDescriptor(
'hasBeenClosed',
postCloseHasBeenClosedDescriptor
);
it('allows explicit reconnect on a previously closed but reconnected client', async () => {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
await client.connect();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', INIT_HAS_BEEN_CLOSED);
await client.close();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', RD_ONLY_HAS_BEEN_CLOSED);
await client.connect();
const result = await db.command({ ping: 1 }).catch(error => error);
expect(result).to.not.be.instanceOf(MongoNotConnectedError);
expect(result).to.have.property('ok', 1);
});

await client.close();
it('prevents auto reconnect on closed non-connected client', async () => {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', INIT_HAS_BEEN_CLOSED);
const result = await db.command({ ping: 1 }).catch(error => error); // auto connect
expect(result).to.not.be.instanceOf(MongoNotConnectedError);
expect(result).to.have.property('ok', 1);
await client.close();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', RD_ONLY_HAS_BEEN_CLOSED);
const error = await db.command({ ping: 1 }).catch(error => error);
expect(error).to.be.instanceOf(MongoNotConnectedError);
});

const secondResult = await client
.db('test')
.collection('test')
.findOne()
.catch(error => error);
it('allows explicit reconnect on closed non-connected client', async () => {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', INIT_HAS_BEEN_CLOSED);
const result = await db.command({ ping: 1 }).catch(error => error); // auto connect
expect(result).to.not.be.instanceOf(MongoNotConnectedError);
expect(result).to.have.property('ok', 1);
await client.close();
await client.connect();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', RD_ONLY_HAS_BEEN_CLOSED);
const result2 = await db.command({ ping: 1 }).catch(error => error);
expect(result2).to.not.be.instanceOf(MongoNotConnectedError);
expect(result2).to.have.property('ok', 1);
});

expect(client).to.have.property('topology', undefined);
expect(secondResult).to.be.instanceOf(MongoNotConnectedError);
it('prevents auto reconnect on closed explicitly connected client', async () => {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', INIT_HAS_BEEN_CLOSED);
await client.connect();
const result = await db.command({ ping: 1 }).catch(error => error);
expect(result).to.not.be.instanceOf(MongoNotConnectedError);
expect(result).to.have.property('ok', 1);
await client.close();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', RD_ONLY_HAS_BEEN_CLOSED);
const error = await db.command({ ping: 1 }).catch(error => error);
expect(error).to.be.instanceOf(MongoNotConnectedError);
});

expect(client.s).to.have.ownPropertyDescriptor(
'hasBeenClosed',
postCloseHasBeenClosedDescriptor
);
}
);
it('allows explicit reconnect on closed previously connected client', async () => {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', INIT_HAS_BEEN_CLOSED);
await client.connect();
const result = await db.command({ ping: 1 }).catch(error => error);
expect(result).to.not.be.instanceOf(MongoNotConnectedError);
expect(result).to.have.property('ok', 1);
await client.close();
await client.connect();
expect(client.s).to.have.ownPropertyDescriptor('hasBeenClosed', RD_ONLY_HAS_BEEN_CLOSED);
const result2 = await db.command({ ping: 1 }).catch(error => error);
expect(result2).to.not.be.instanceOf(MongoNotConnectedError);
expect(result2).to.have.property('ok', 1);
});
});
});
dariakp marked this conversation as resolved.
Show resolved Hide resolved