Skip to content

Commit 30e9f5b

Browse files
committed
Merge branch 'master' into vkarpov15/gh-14353
2 parents 8656ede + ec6a999 commit 30e9f5b

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

lib/connection.js

+24
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,30 @@ Connection.prototype.openUri = async function openUri(uri, options) {
829829
return this;
830830
};
831831

832+
/*!
833+
* Treat `on('error')` handlers as handling the initialConnection promise
834+
* to avoid uncaught exceptions when using `on('error')`. See gh-14377.
835+
*/
836+
837+
Connection.prototype.on = function on(event, callback) {
838+
if (event === 'error' && this.$initialConnection) {
839+
this.$initialConnection.catch(() => {});
840+
}
841+
return EventEmitter.prototype.on.call(this, event, callback);
842+
};
843+
844+
/*!
845+
* Treat `once('error')` handlers as handling the initialConnection promise
846+
* to avoid uncaught exceptions when using `on('error')`. See gh-14377.
847+
*/
848+
849+
Connection.prototype.once = function on(event, callback) {
850+
if (event === 'error' && this.$initialConnection) {
851+
this.$initialConnection.catch(() => {});
852+
}
853+
return EventEmitter.prototype.once.call(this, event, callback);
854+
};
855+
832856
/*!
833857
* ignore
834858
*/

lib/helpers/schema/applyWriteConcern.js

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ module.exports = function applyWriteConcern(schema, options) {
66
if (options.writeConcern != null) {
77
return;
88
}
9+
// Don't apply default write concern to operations in transactions,
10+
// because setting write concern on an operation in a transaction is an error
11+
// See: https://www.mongodb.com/docs/manual/reference/write-concern/
12+
if (options && options.session && options.session.transaction) {
13+
return;
14+
}
915
const writeConcern = get(schema, 'options.writeConcern', {});
1016
if (Object.keys(writeConcern).length != 0) {
1117
options.writeConcern = {};

test/connection.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,21 @@ describe('connections:', function() {
916916
assert.equal(err.name, 'MongooseServerSelectionError');
917917
});
918918

919+
it('avoids unhandled error on createConnection() if error handler registered (gh-14377)', async function() {
920+
const opts = {
921+
serverSelectionTimeoutMS: 100
922+
};
923+
const uri = 'mongodb://baddomain:27017/test';
924+
925+
const conn = mongoose.createConnection(uri, opts);
926+
await new Promise(resolve => {
927+
conn.on('error', err => {
928+
assert.equal(err.name, 'MongoServerSelectionError');
929+
resolve();
930+
});
931+
});
932+
});
933+
919934
it('`watch()` on a whole collection (gh-8425)', async function() {
920935
this.timeout(10000);
921936
if (!process.env.REPLICA_SET) {

test/docs/lean.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ describe('Lean Tutorial', function() {
4242
const leanDoc = await MyModel.findOne().lean();
4343

4444
v8Serialize(normalDoc).length; // approximately 180
45-
v8Serialize(leanDoc).length; // 32, about 5x smaller!
45+
v8Serialize(leanDoc).length; // approximately 55, about 3x smaller!
4646

4747
// In case you were wondering, the JSON form of a Mongoose doc is the same
4848
// as the POJO. This additional memory only affects how much memory your
4949
// Node.js process uses, not how much data is sent over the network.
5050
JSON.stringify(normalDoc).length === JSON.stringify(leanDoc).length; // true
5151
// acquit:ignore:start
5252
assert.ok(v8Serialize(normalDoc).length >= 150 && v8Serialize(normalDoc).length <= 200, v8Serialize(normalDoc).length);
53-
assert.equal(v8Serialize(leanDoc).length, 32);
53+
assert.ok(v8Serialize(leanDoc).length === 55 || v8Serialize(leanDoc).length === 32, v8Serialize(leanDoc).length);
5454
assert.equal(JSON.stringify(normalDoc).length, JSON.stringify(leanDoc).length);
5555
// acquit:ignore:end
5656
});

test/docs/transactions.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,20 @@ describe('transactions', function() {
477477

478478
assert.equal(i, 3);
479479
});
480+
481+
it('doesnt apply schema write concern to transaction operations (gh-11382)', async function() {
482+
db.deleteModel(/Test/);
483+
const Test = db.model('Test', Schema({ status: String }, { writeConcern: { w: 'majority' } }));
484+
485+
await Test.createCollection();
486+
await Test.deleteMany({});
487+
488+
const session = await db.startSession();
489+
490+
await session.withTransaction(async function() {
491+
await Test.findOneAndUpdate({}, { name: 'test' }, { session });
492+
});
493+
494+
await session.endSession();
495+
});
480496
});

0 commit comments

Comments
 (0)