Skip to content

Commit

Permalink
test: all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed May 17, 2022
1 parent da6392e commit 6296ade
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 82 deletions.
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
185 changes: 106 additions & 79 deletions test/integration/node-specific/mongo_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,95 +510,122 @@ describe('class MongoClient', function () {
expect(client).to.have.property('topology').that.is.instanceOf(Topology);
}
);
});

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

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(
'does not auto reconnect after client.close',
{ 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 () => {
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 () => {
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 () => {
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 () => {
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 () => {
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 () => {
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);
});
});
});

0 comments on commit 6296ade

Please sign in to comment.