Skip to content

Commit

Permalink
fix: connection ids are now scoped
Browse files Browse the repository at this point in the history
Some tests are failing but they were failing before the changes I made.
  • Loading branch information
IslandRhythms committed Mar 24, 2021
1 parent ccf5b16 commit 6deb668
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/connection.js
Expand Up @@ -70,7 +70,12 @@ function Connection(base) {
this._closeCalled = false;
this._hasOpened = false;
this.plugins = [];
this.id = id++;
// this.id = id++;
if (typeof base === 'undefined' || !base.connections.length) {
this.id = 0;
} else {
this.id = base.connections.length;
}
}

/*!
Expand Down
14 changes: 12 additions & 2 deletions test/connection.test.js
Expand Up @@ -1301,9 +1301,19 @@ describe('connections:', function() {
});
it('Connection id should be scoped per Mongoose Instance (gh-10025)', function() {
const m = new mongoose.Mongoose;
console.log(m.connection.id);
// console.log('First Instance', m.connection.id); // should be x
const conn = m.createConnection();
// console.log(conn.id); // should be x + 1
const m1 = new mongoose.Mongoose;
console.log(m1.connection.id);
// console.log('Second Instance', m1.connection.id); // should be y
const conn2 = m1.createConnection();
// console.log(conn2.id); // should be y + 1;
const conn3 = m.createConnection();
// console.log('Back to First Instance', conn3.id); // should be x + 2
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 6deb668

Please sign in to comment.