Skip to content

Commit

Permalink
Merge pull request #12890 from hasezoey/closeAllConnections
Browse files Browse the repository at this point in the history
Close all connections in all tests, also change some tests to be working stand-alone
  • Loading branch information
vkarpov15 authored Jan 17, 2023
2 parents 92fcc58 + 07378d3 commit 256a11e
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 37 deletions.
4 changes: 4 additions & 0 deletions test/collection.capped.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ const Schema = mongoose.Schema;
describe('collections: capped:', function() {
let db;

const connectionsToClose = [];

before(function() {
db = start();
});

after(async function() {
await db.close();
await Promise.all(connectionsToClose.map((v) => v.close()));
});

it('schemas should have option size', function() {
Expand Down Expand Up @@ -52,6 +55,7 @@ describe('collections: capped:', function() {

it('skips when setting autoCreate to false (gh-8566)', async function() {
const db = start();
connectionsToClose.push(db);
this.timeout(30000);
await db.dropDatabase();

Expand Down
9 changes: 9 additions & 0 deletions test/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ const assert = require('assert');
const mongoose = start.mongoose;

describe('collections:', function() {
const connectionsToClose = [];

after(async function() {
await Promise.all(connectionsToClose.map((v) => v.close()));
});

it('should buffer commands until connection is established', function(done) {
const db = mongoose.createConnection();
connectionsToClose.push(db);
const collection = db.collection('test-buffering-collection');
let connected = false;
let insertedId = undefined;
Expand Down Expand Up @@ -43,6 +50,7 @@ describe('collections:', function() {

it('returns a promise if buffering and no callback (gh-7676)', function(done) {
const db = mongoose.createConnection();
connectionsToClose.push(db);
const collection = db.collection('gh7676');

const promise = collection.insertOne({ foo: 'bar' }, {})
Expand Down Expand Up @@ -145,6 +153,7 @@ describe('collections:', function() {

it('buffers for sync methods (gh-10610)', function(done) {
const db = mongoose.createConnection();
connectionsToClose.push(db);
const collection = db.collection('gh10610');

collection.find({}, {}, function(err, res) {
Expand Down
71 changes: 50 additions & 21 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('connections:', function() {
}).asPromise();

assert.strictEqual(conn.config.autoIndex, false);
await conn.close();
});

it('with autoCreate (gh-6489)', async function() {
Expand All @@ -80,6 +81,7 @@ describe('connections:', function() {
const res = await conn.collection('gh6489_Conn').
find({}).sort({ name: 1 }).toArray();
assert.deepEqual(res.map(v => v.name), ['alpha', 'Zeta']);
await conn.close();
});

it('with autoCreate = false (gh-8814)', async function() {
Expand All @@ -95,6 +97,7 @@ describe('connections:', function() {

const res = await conn.db.listCollections().toArray();
assert.ok(!res.map(c => c.name).includes('gh8814_Conn'));
await conn.close();
});

it('autoCreate when collection already exists does not fail (gh-7122)', async function() {
Expand All @@ -108,6 +111,7 @@ describe('connections:', function() {
}, { autoCreate: true });

await conn.model('Actor', schema).init();
await conn.close();
});

it('throws helpful error with legacy syntax (gh-6756)', function() {
Expand All @@ -133,9 +137,10 @@ describe('connections:', function() {

const _conn = await bootMongo.promise;
assert.equal(_conn, conn);
await conn.close();
});

it('connection plugins (gh-7378)', function() {
it('connection plugins (gh-7378)', async function() {
const conn1 = mongoose.createConnection(start.uri);
const conn2 = mongoose.createConnection(start.uri);

Expand All @@ -149,6 +154,8 @@ describe('connections:', function() {
conn1.model('Test', schema);
assert.equal(called.length, 1);
assert.equal(called[0], schema);
await conn1.close();
await conn2.close();
});
});

Expand Down Expand Up @@ -511,7 +518,7 @@ describe('connections:', function() {
}).
then(() => {
assert.ok(session.serverSession.lastUse > lastUse);
conn.close();
return conn.close();
});
});

Expand Down Expand Up @@ -593,6 +600,7 @@ describe('connections:', function() {
const nothing2 = await m2.findById(i1.id);
assert.strictEqual(null, nothing2);

await db.close();
await db2.close();
});

Expand Down Expand Up @@ -752,6 +760,7 @@ describe('connections:', function() {

await db.openUri(start.uri);
assert.strictEqual(db.client, db2.client);
await db.close();
});

it('closes correctly for all dbs, closing secondary db', function(done) {
Expand All @@ -764,29 +773,29 @@ describe('connections:', function() {
db2.close();
});

it('cache connections to the same db', function() {
it('cache connections to the same db', function(done) {
const db = start();
const db2 = db.useDb(start.databases[1], { useCache: true });
const db3 = db.useDb(start.databases[1], { useCache: true });

assert.strictEqual(db2, db3);
db.close();
db.close(done);
});
});

describe('shouldAuthenticate()', function() {
describe('when using standard authentication', function() {
describe('when username and password are undefined', function() {
it('should return false', function() {
it('should return false', function(done) {
const db = mongoose.createConnection(start.uri, {});

assert.equal(db.shouldAuthenticate(), false);

db.close();
db.close(done);
});
});
describe('when username and password are empty strings', function() {
it('should return false', function() {
it('should return false', function(done) {
const db = mongoose.createConnection(start.uri, {
user: '',
pass: ''
Expand All @@ -795,7 +804,7 @@ describe('connections:', function() {

assert.equal(db.shouldAuthenticate(), false);

db.close();
db.close(done);
});
});
describe('when both username and password are defined', function() {
Expand All @@ -808,20 +817,20 @@ describe('connections:', function() {

assert.equal(db.shouldAuthenticate(), true);

db.close();
db.close(); // does not actually do anything
});
});
});
describe('when using MONGODB-X509 authentication', function() {
describe('when username and password are undefined', function() {
it('should return false', function() {
it('should return false', function(done) {
const db = mongoose.createConnection(start.uri, {});
db.on('error', function() {
});

assert.equal(db.shouldAuthenticate(), false);

db.close();
db.close(done);
});
});
describe('when only username is defined', function() {
Expand All @@ -833,7 +842,7 @@ describe('connections:', function() {
db.asPromise().catch(() => {});
assert.equal(db.shouldAuthenticate(), true);

db.close();
db.close(); // does not actually do anything
});
});
describe('when both username and password are defined', function() {
Expand All @@ -847,23 +856,24 @@ describe('connections:', function() {

assert.equal(db.shouldAuthenticate(), true);

db.close();
db.close(); // does not actually do anything
});
});
});
});

describe('passing a function into createConnection', function() {
it('should store the name of the function (gh-6517)', function() {
it('should store the name of the function (gh-6517)', function(done) {
const conn = mongoose.createConnection(start.uri);
const schema = new Schema({ name: String });
class Person extends mongoose.Model {}
conn.model(Person, schema);
assert.strictEqual(conn.modelNames()[0], 'Person');
conn.close(done);
});
});

it('deleteModel()', function() {
it('deleteModel()', async function() {
const conn = mongoose.createConnection(start.uri);

let Model = conn.model('gh6813', new Schema({ name: String }));
Expand All @@ -883,7 +893,8 @@ describe('connections:', function() {

Model = conn.model('gh6813', new Schema({ name: String }));
assert.ok(Model);
return Model.create({ name: 'test' });
await Model.create({ name: 'test' });
await conn.close();
});

it('throws a MongooseServerSelectionError on server selection timeout (gh-8451)', function() {
Expand Down Expand Up @@ -926,6 +937,7 @@ describe('connections:', function() {
await nextChange;
assert.equal(changes.length, 1);
assert.equal(changes[0].operationType, 'insert');
await conn.close();
});

it('useDB inherits config from default connection (gh-8267)', async function() {
Expand All @@ -947,6 +959,7 @@ describe('connections:', function() {
await conn.createCollection('test');
const res = await conn.dropCollection('test');
assert.ok(res);
await conn.close();
});

it('connection.asPromise() resolves to a connection instance (gh-9496)', async function() {
Expand Down Expand Up @@ -1065,7 +1078,7 @@ describe('connections:', function() {
});

describe('mongoose.createConnection', function() {
it('forces autoIndex & autoCreate to be false if read preference is secondary or secondaryPreferred (gh-9374)', function() {
it('forces autoIndex & autoCreate to be false if read preference is secondary or secondaryPreferred (gh-9374)', async function() {
const conn = new mongoose.createConnection(start.uri, { readPreference: 'secondary' });

assert.equal(conn.get('autoIndex'), false);
Expand All @@ -1075,13 +1088,16 @@ describe('connections:', function() {

assert.equal(conn2.get('autoIndex'), false);
assert.equal(conn2.get('autoCreate'), false);
await conn.close();
await conn2.close();
});

it('keeps autoIndex & autoCreate as true by default if read preference is primaryPreferred (gh-9374)', function() {
it('keeps autoIndex & autoCreate as true by default if read preference is primaryPreferred (gh-9374)', async function() {
const conn = new mongoose.createConnection(start.uri, { readPreference: 'primaryPreferred' });

assert.equal(conn.get('autoIndex'), undefined);
assert.equal(conn.get('autoCreate'), undefined);
await conn.close();
});

it('throws if options try to set autoIndex to true', function() {
Expand Down Expand Up @@ -1164,6 +1180,7 @@ describe('connections:', function() {
indexes = await Test.collection.listIndexes().toArray();
assert.equal(indexes.length, 2);
assert.equal(indexes[1].name, 'name_1');
await conn.close();
});

it('re-runs init() if running setClient() after disconnecting (gh-12047)', async function() {
Expand Down Expand Up @@ -1206,13 +1223,20 @@ describe('connections:', function() {

describe('Connection#syncIndexes() (gh-10893) (gh-11039)', () => {
let connection;
this.beforeEach(async() => {
const mongooseInstance = new mongoose.Mongoose();
let mongooseInstance;

before(async() => {
mongooseInstance = new mongoose.Mongoose();
connection = mongooseInstance.createConnection(start.uri);
});
this.afterEach(async() => {
beforeEach(() => connection.deleteModel(/.*/));
afterEach(async() => {
await connection.dropDatabase();
});
after(async() => {
await connection.close();
});

it('Allows a syncIndexes option with connection mongoose.connection.syncIndexes (gh-10893)', async function() {
const coll = 'tests2';

Expand All @@ -1235,6 +1259,7 @@ describe('connections:', function() {
const indexesAfterDropping = await connection.syncIndexes();
assert.deepEqual(indexesAfterDropping, { Test: ['name_1'] });
});

it('does not sync indexes automatically when `autoIndex: true` (gh-11039)', async function() {
// Arrange
const buildingSchema = new Schema({ name: String }, { autoIndex: false });
Expand Down Expand Up @@ -1272,6 +1297,7 @@ describe('connections:', function() {
assert.deepEqual(floorIndexes.map(index => index.key), [{ _id: 1 }]);
assert.deepEqual(officeIndexes.map(index => index.key), [{ _id: 1 }]);
});

it('stops as soon as one model fails with `continueOnError: false` (gh-11039)', async function() {
// Arrange
const buildingSchema = new Schema({ name: String }, { autoIndex: false });
Expand Down Expand Up @@ -1356,6 +1382,7 @@ describe('connections:', function() {
assert.equal(err.errors['Book'].code, 11000);
assert.equal(err.errors['Book'].code, 11000);
});

it('when `continueOnError: true` it will continue to sync indexes even if one model fails', async() => {
// Arrange
const buildingSchema = new Schema({ name: String }, { autoIndex: false });
Expand Down Expand Up @@ -1451,6 +1478,8 @@ describe('connections:', function() {
assert.ok(Array.isArray(result['Building']));
assert.ok(result['Floor'].name, 'MongoServerError');
assert.ok(Array.isArray(result['Office']));

await m.disconnect();
});
});

Expand Down
6 changes: 5 additions & 1 deletion test/docs/cast.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ describe('Cast Tutorial', function() {
});
});

after(async () => {
await mongoose.disconnect();
})

it('get and set', async function() {
const query = Character.find({ name: 'Jean-Luc Picard' });
query.getFilter(); // `{ name: 'Jean-Luc Picard' }`
Expand Down Expand Up @@ -150,4 +154,4 @@ describe('Cast Tutorial', function() {
});
// acquit:ignore:end
});
});
});
Loading

0 comments on commit 256a11e

Please sign in to comment.