Skip to content

Commit 256a11e

Browse files
authored
Merge pull request #12890 from hasezoey/closeAllConnections
Close all connections in all tests, also change some tests to be working stand-alone
2 parents 92fcc58 + 07378d3 commit 256a11e

21 files changed

Lines changed: 147 additions & 37 deletions

test/collection.capped.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ const Schema = mongoose.Schema;
1919
describe('collections: capped:', function() {
2020
let db;
2121

22+
const connectionsToClose = [];
23+
2224
before(function() {
2325
db = start();
2426
});
2527

2628
after(async function() {
2729
await db.close();
30+
await Promise.all(connectionsToClose.map((v) => v.close()));
2831
});
2932

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

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

test/collection.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ const assert = require('assert');
88
const mongoose = start.mongoose;
99

1010
describe('collections:', function() {
11+
const connectionsToClose = [];
12+
13+
after(async function() {
14+
await Promise.all(connectionsToClose.map((v) => v.close()));
15+
});
16+
1117
it('should buffer commands until connection is established', function(done) {
1218
const db = mongoose.createConnection();
19+
connectionsToClose.push(db);
1320
const collection = db.collection('test-buffering-collection');
1421
let connected = false;
1522
let insertedId = undefined;
@@ -43,6 +50,7 @@ describe('collections:', function() {
4350

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

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

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

150159
collection.find({}, {}, function(err, res) {

test/connection.test.js

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe('connections:', function() {
5656
}).asPromise();
5757

5858
assert.strictEqual(conn.config.autoIndex, false);
59+
await conn.close();
5960
});
6061

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

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

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

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

110113
await conn.model('Actor', schema).init();
114+
await conn.close();
111115
});
112116

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

134138
const _conn = await bootMongo.promise;
135139
assert.equal(_conn, conn);
140+
await conn.close();
136141
});
137142

138-
it('connection plugins (gh-7378)', function() {
143+
it('connection plugins (gh-7378)', async function() {
139144
const conn1 = mongoose.createConnection(start.uri);
140145
const conn2 = mongoose.createConnection(start.uri);
141146

@@ -149,6 +154,8 @@ describe('connections:', function() {
149154
conn1.model('Test', schema);
150155
assert.equal(called.length, 1);
151156
assert.equal(called[0], schema);
157+
await conn1.close();
158+
await conn2.close();
152159
});
153160
});
154161

@@ -511,7 +518,7 @@ describe('connections:', function() {
511518
}).
512519
then(() => {
513520
assert.ok(session.serverSession.lastUse > lastUse);
514-
conn.close();
521+
return conn.close();
515522
});
516523
});
517524

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

603+
await db.close();
596604
await db2.close();
597605
});
598606

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

753761
await db.openUri(start.uri);
754762
assert.strictEqual(db.client, db2.client);
763+
await db.close();
755764
});
756765

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

767-
it('cache connections to the same db', function() {
776+
it('cache connections to the same db', function(done) {
768777
const db = start();
769778
const db2 = db.useDb(start.databases[1], { useCache: true });
770779
const db3 = db.useDb(start.databases[1], { useCache: true });
771780

772781
assert.strictEqual(db2, db3);
773-
db.close();
782+
db.close(done);
774783
});
775784
});
776785

777786
describe('shouldAuthenticate()', function() {
778787
describe('when using standard authentication', function() {
779788
describe('when username and password are undefined', function() {
780-
it('should return false', function() {
789+
it('should return false', function(done) {
781790
const db = mongoose.createConnection(start.uri, {});
782791

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

785-
db.close();
794+
db.close(done);
786795
});
787796
});
788797
describe('when username and password are empty strings', function() {
789-
it('should return false', function() {
798+
it('should return false', function(done) {
790799
const db = mongoose.createConnection(start.uri, {
791800
user: '',
792801
pass: ''
@@ -795,7 +804,7 @@ describe('connections:', function() {
795804

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

798-
db.close();
807+
db.close(done);
799808
});
800809
});
801810
describe('when both username and password are defined', function() {
@@ -808,20 +817,20 @@ describe('connections:', function() {
808817

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

811-
db.close();
820+
db.close(); // does not actually do anything
812821
});
813822
});
814823
});
815824
describe('when using MONGODB-X509 authentication', function() {
816825
describe('when username and password are undefined', function() {
817-
it('should return false', function() {
826+
it('should return false', function(done) {
818827
const db = mongoose.createConnection(start.uri, {});
819828
db.on('error', function() {
820829
});
821830

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

824-
db.close();
833+
db.close(done);
825834
});
826835
});
827836
describe('when only username is defined', function() {
@@ -833,7 +842,7 @@ describe('connections:', function() {
833842
db.asPromise().catch(() => {});
834843
assert.equal(db.shouldAuthenticate(), true);
835844

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

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

850-
db.close();
859+
db.close(); // does not actually do anything
851860
});
852861
});
853862
});
854863
});
855864

856865
describe('passing a function into createConnection', function() {
857-
it('should store the name of the function (gh-6517)', function() {
866+
it('should store the name of the function (gh-6517)', function(done) {
858867
const conn = mongoose.createConnection(start.uri);
859868
const schema = new Schema({ name: String });
860869
class Person extends mongoose.Model {}
861870
conn.model(Person, schema);
862871
assert.strictEqual(conn.modelNames()[0], 'Person');
872+
conn.close(done);
863873
});
864874
});
865875

866-
it('deleteModel()', function() {
876+
it('deleteModel()', async function() {
867877
const conn = mongoose.createConnection(start.uri);
868878

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

884894
Model = conn.model('gh6813', new Schema({ name: String }));
885895
assert.ok(Model);
886-
return Model.create({ name: 'test' });
896+
await Model.create({ name: 'test' });
897+
await conn.close();
887898
});
888899

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

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

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

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

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

10761089
assert.equal(conn2.get('autoIndex'), false);
10771090
assert.equal(conn2.get('autoCreate'), false);
1091+
await conn.close();
1092+
await conn2.close();
10781093
});
10791094

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

10831098
assert.equal(conn.get('autoIndex'), undefined);
10841099
assert.equal(conn.get('autoCreate'), undefined);
1100+
await conn.close();
10851101
});
10861102

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

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

12071224
describe('Connection#syncIndexes() (gh-10893) (gh-11039)', () => {
12081225
let connection;
1209-
this.beforeEach(async() => {
1210-
const mongooseInstance = new mongoose.Mongoose();
1226+
let mongooseInstance;
1227+
1228+
before(async() => {
1229+
mongooseInstance = new mongoose.Mongoose();
12111230
connection = mongooseInstance.createConnection(start.uri);
12121231
});
1213-
this.afterEach(async() => {
1232+
beforeEach(() => connection.deleteModel(/.*/));
1233+
afterEach(async() => {
12141234
await connection.dropDatabase();
12151235
});
1236+
after(async() => {
1237+
await connection.close();
1238+
});
1239+
12161240
it('Allows a syncIndexes option with connection mongoose.connection.syncIndexes (gh-10893)', async function() {
12171241
const coll = 'tests2';
12181242

@@ -1235,6 +1259,7 @@ describe('connections:', function() {
12351259
const indexesAfterDropping = await connection.syncIndexes();
12361260
assert.deepEqual(indexesAfterDropping, { Test: ['name_1'] });
12371261
});
1262+
12381263
it('does not sync indexes automatically when `autoIndex: true` (gh-11039)', async function() {
12391264
// Arrange
12401265
const buildingSchema = new Schema({ name: String }, { autoIndex: false });
@@ -1272,6 +1297,7 @@ describe('connections:', function() {
12721297
assert.deepEqual(floorIndexes.map(index => index.key), [{ _id: 1 }]);
12731298
assert.deepEqual(officeIndexes.map(index => index.key), [{ _id: 1 }]);
12741299
});
1300+
12751301
it('stops as soon as one model fails with `continueOnError: false` (gh-11039)', async function() {
12761302
// Arrange
12771303
const buildingSchema = new Schema({ name: String }, { autoIndex: false });
@@ -1356,6 +1382,7 @@ describe('connections:', function() {
13561382
assert.equal(err.errors['Book'].code, 11000);
13571383
assert.equal(err.errors['Book'].code, 11000);
13581384
});
1385+
13591386
it('when `continueOnError: true` it will continue to sync indexes even if one model fails', async() => {
13601387
// Arrange
13611388
const buildingSchema = new Schema({ name: String }, { autoIndex: false });
@@ -1451,6 +1478,8 @@ describe('connections:', function() {
14511478
assert.ok(Array.isArray(result['Building']));
14521479
assert.ok(result['Floor'].name, 'MongoServerError');
14531480
assert.ok(Array.isArray(result['Office']));
1481+
1482+
await m.disconnect();
14541483
});
14551484
});
14561485

test/docs/cast.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ describe('Cast Tutorial', function() {
2323
});
2424
});
2525

26+
after(async () => {
27+
await mongoose.disconnect();
28+
})
29+
2630
it('get and set', async function() {
2731
const query = Character.find({ name: 'Jean-Luc Picard' });
2832
query.getFilter(); // `{ name: 'Jean-Luc Picard' }`
@@ -150,4 +154,4 @@ describe('Cast Tutorial', function() {
150154
});
151155
// acquit:ignore:end
152156
});
153-
});
157+
});

0 commit comments

Comments
 (0)