Skip to content

Commit

Permalink
Merge pull request #10062 from Automattic/gh-10025
Browse files Browse the repository at this point in the history
Gh 10025
  • Loading branch information
vkarpov15 committed Mar 29, 2021
2 parents 9e98cd8 + 6f0133a commit 2aef528
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/connection.js
Expand Up @@ -25,8 +25,6 @@ const parseConnectionString = require('mongodb/lib/core').parseConnectionString;
const arrayAtomicsSymbol = require('./helpers/symbols').arrayAtomicsSymbol;
const sessionNewDocuments = require('./helpers/symbols').sessionNewDocuments;

let id = 0;

/*!
* A list of authentication mechanisms that don't require a password for authentication.
* This is used by the authMechanismDoesNotRequirePassword method.
Expand Down Expand Up @@ -70,7 +68,11 @@ function Connection(base) {
this._closeCalled = false;
this._hasOpened = false;
this.plugins = [];
this.id = id++;
if (typeof base === 'undefined' || !base.connections.length) {
this.id = 0;
} else {
this.id = base.connections.length;
}
}

/*!
Expand Down
12 changes: 12 additions & 0 deletions test/connection.test.js
Expand Up @@ -1299,4 +1299,16 @@ describe('connections:', function() {
assert.ifError(errorOnDisconnect);
});
});
it('Connection id should be scoped per Mongoose Instance (gh-10025)', function() {
const m = new mongoose.Mongoose;
const conn = m.createConnection();
const m1 = new mongoose.Mongoose;
const conn2 = m1.createConnection();
const conn3 = m.createConnection();
assert.deepStrictEqual(m.connection.id, 0);
assert.deepStrictEqual(conn.id, m.connection.id + 1);
assert.deepStrictEqual(m1.connection.id, 0);
assert.deepStrictEqual(conn2.id, m1.connection.id + 1);
assert.deepStrictEqual(conn3.id, m.connection.id + 2);
});
});

0 comments on commit 2aef528

Please sign in to comment.