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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: some cleanup re: #12890 #12919

Merged
merged 7 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/cursor/ChangeStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class ChangeStream extends EventEmitter {

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

['close', 'change', 'end', 'error'].forEach(ev => {
this.driverChangeStream.on(ev, data => {
// Sometimes Node driver still polls after close, so
// avoid any uncaught exceptions due to closed change streams
// See tests for gh-7022
if (ev === 'error' && this.closed) {
return;
}
this.emit(ev, data);
});
});
Expand Down
18 changes: 8 additions & 10 deletions test/collection.capped.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ const Schema = mongoose.Schema;
describe('collections: capped:', function() {
let db;

const connectionsToClose = [];

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

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

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

db = start();

await db.dropCollection('Test').catch(() => {});

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

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

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

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

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

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

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

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

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

collection.find({}, {}, function(err, res) {
Expand Down
16 changes: 8 additions & 8 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,29 +773,29 @@ describe('connections:', function() {
db2.close();
});

it('cache connections to the same db', function(done) {
it('cache connections to the same db', function() {
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(done);
return db.close();
});
});

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

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

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

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

db.close(done);
return db.close();
});
});
describe('when both username and password are defined', function() {
Expand All @@ -823,14 +823,14 @@ describe('connections:', function() {
});
describe('when using MONGODB-X509 authentication', function() {
describe('when username and password are undefined', function() {
it('should return false', function(done) {
it('should return false', function() {
const db = mongoose.createConnection(start.uri, {});
db.on('error', function() {
});

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

db.close(done);
return db.close();
});
});
describe('when only username is defined', function() {
Expand Down
Loading