Skip to content

Commit 59e5338

Browse files
authored
Merge pull request Automattic#12919 from Automattic/vkarpov15/test-cleanup-12890
test: some cleanup re: Automattic#12890
2 parents 8752e78 + d4433f5 commit 59e5338

File tree

7 files changed

+168
-145
lines changed

7 files changed

+168
-145
lines changed

lib/cursor/ChangeStream.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class ChangeStream extends EventEmitter {
5454

5555
['close', 'change', 'end', 'error'].forEach(ev => {
5656
this.driverChangeStream.on(ev, data => {
57+
// Sometimes Node driver still polls after close, so
58+
// avoid any uncaught exceptions due to closed change streams
59+
// See tests for gh-7022
60+
if (ev === 'error' && this.closed) {
61+
return;
62+
}
5763
if (data != null && data.fullDocument != null && this.options && this.options.hydrate) {
5864
data.fullDocument = this.options.model.hydrate(data.fullDocument);
5965
}
@@ -71,6 +77,12 @@ class ChangeStream extends EventEmitter {
7177

7278
['close', 'change', 'end', 'error'].forEach(ev => {
7379
this.driverChangeStream.on(ev, data => {
80+
// Sometimes Node driver still polls after close, so
81+
// avoid any uncaught exceptions due to closed change streams
82+
// See tests for gh-7022
83+
if (ev === 'error' && this.closed) {
84+
return;
85+
}
7486
this.emit(ev, data);
7587
});
7688
});

test/collection.capped.test.js

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

22-
const connectionsToClose = [];
23-
24-
before(function() {
25-
db = start();
26-
});
27-
28-
after(async function() {
22+
afterEach(async function() {
23+
if (db == null) {
24+
return;
25+
}
2926
await db.close();
30-
await Promise.all(connectionsToClose.map((v) => v.close()));
27+
db = null;
3128
});
3229

3330
it('schemas should have option size', function() {
@@ -41,6 +38,8 @@ describe('collections: capped:', function() {
4138
it('creation', async function() {
4239
this.timeout(15000);
4340

41+
db = start();
42+
4443
await db.dropCollection('Test').catch(() => {});
4544

4645
const capped = new Schema({ key: String });
@@ -54,8 +53,7 @@ describe('collections: capped:', function() {
5453
});
5554

5655
it('skips when setting autoCreate to false (gh-8566)', async function() {
57-
const db = start();
58-
connectionsToClose.push(db);
56+
db = start();
5957
this.timeout(30000);
6058
await db.dropDatabase();
6159

test/collection.test.js

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

1010
describe('collections:', function() {
11-
const connectionsToClose = [];
11+
let db = null;
1212

13-
after(async function() {
14-
await Promise.all(connectionsToClose.map((v) => v.close()));
13+
afterEach(async function() {
14+
if (db == null) {
15+
return;
16+
}
17+
await db.close();
18+
db = null;
1519
});
1620

1721
it('should buffer commands until connection is established', function(done) {
18-
const db = mongoose.createConnection();
19-
connectionsToClose.push(db);
22+
db = mongoose.createConnection();
2023
const collection = db.collection('test-buffering-collection');
2124
let connected = false;
2225
let insertedId = undefined;
@@ -49,8 +52,7 @@ describe('collections:', function() {
4952
});
5053

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

5658
const promise = collection.insertOne({ foo: 'bar' }, {})
@@ -152,8 +154,7 @@ describe('collections:', function() {
152154
});
153155

154156
it('buffers for sync methods (gh-10610)', function(done) {
155-
const db = mongoose.createConnection();
156-
connectionsToClose.push(db);
157+
db = mongoose.createConnection();
157158
const collection = db.collection('gh10610');
158159

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

test/connection.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -773,29 +773,29 @@ describe('connections:', function() {
773773
db2.close();
774774
});
775775

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

781781
assert.strictEqual(db2, db3);
782-
db.close(done);
782+
return db.close();
783783
});
784784
});
785785

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

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

794-
db.close(done);
794+
return db.close();
795795
});
796796
});
797797
describe('when username and password are empty strings', function() {
798-
it('should return false', function(done) {
798+
it('should return false', function() {
799799
const db = mongoose.createConnection(start.uri, {
800800
user: '',
801801
pass: ''
@@ -804,7 +804,7 @@ describe('connections:', function() {
804804

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

807-
db.close(done);
807+
return db.close();
808808
});
809809
});
810810
describe('when both username and password are defined', function() {
@@ -823,14 +823,14 @@ describe('connections:', function() {
823823
});
824824
describe('when using MONGODB-X509 authentication', function() {
825825
describe('when username and password are undefined', function() {
826-
it('should return false', function(done) {
826+
it('should return false', function() {
827827
const db = mongoose.createConnection(start.uri, {});
828828
db.on('error', function() {
829829
});
830830

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

833-
db.close(done);
833+
return db.close();
834834
});
835835
});
836836
describe('when only username is defined', function() {

0 commit comments

Comments
 (0)