Permalink
Browse files

New API for the connection pool sharing

This seems to work and passes all of the test cases. Might be slight
issues related to events, but they all seem to be functioning properly
  • Loading branch information...
1 parent 247dda3 commit a0aaebe536bf25fb98e8cdbbe936a0240fc15a33 @ebensing ebensing committed with aheckmann Jul 29, 2013
Showing with 108 additions and 9 deletions.
  1. +5 −0 lib/connection.js
  2. +30 −1 lib/drivers/node-mongodb-native/connection.js
  3. +63 −0 test/connection.test.js
  4. +10 −8 test/dropdb.js
View
@@ -53,6 +53,7 @@ function Connection (base) {
this.pass = null;
this.name = null;
this.options = null;
+ this.otherDbs = [];
this._readyState = STATES.disconnected;
this._closeCalled = false;
this._hasOpened = false;
@@ -92,6 +93,10 @@ Object.defineProperty(Connection.prototype, 'readyState', {
if (this._readyState !== val) {
this._readyState = val;
+ // loop over the otherDbs on this connection and change their state
+ for (var i=0; i < this.otherDbs.length; i++) {
+ this.otherDbs[i].readyState = val;
+ }
if (STATES.connected === val)
this._hasOpened = true;
@@ -73,8 +73,37 @@ NativeConnection.prototype.doOpen = function (fn) {
NativeConnection.prototype.openNewDb = function (name) {
- var newConn = utils.clone(this);
+ // we have to manually copy all of the attributes...
+ var newConn = new this.constructor();
+ newConn.base = this.base;
+ newConn.collections = {};
+ newConn.models = {};
+ newConn.replica = this.replica;
+ newConn.hosts = this.hosts;
+ newConn.host = this.host;
+ newConn.port = this.port;
+ newConn.user = this.user;
+ newConn.pass = this.pass;
+ newConn.options = this.options;
+ newConn._readyState = this._readyState;
+ newConn._closeCalled = this._closeCalled;
+ newConn._hasOpened = this._hasOpened;
+ newConn._listening = false;
+
newConn.db = this.db.db(name);
+ // we have to call onOpen. This is the best way I can figure out how to do
+ // that
+ this.db.once('open', function () {
+ newConn.onOpen();
+ });
+ newConn.name = name;
+
+ // push onto the otherDbs stack, this is used when state changes
+ this.otherDbs.push(newConn);
+ newConn.otherDbs.push(this);
+
+ // setup the events appropriately
+ listen(newConn);
return newConn;
};
View
@@ -892,4 +892,67 @@ describe('connections:', function(){
done();
})
})
+
+ describe('connection pool sharing: ', function () {
+ it('works', function (done) {
+ var db = mongoose.createConnection('mongodb://localhost/mongoose1');
+
+ var db2 = db.openNewDb('mongoose2');
+
+ assert.equal(db2.name, 'mongoose2');
+ assert.equal(db.name, 'mongoose1');
+
+ assert.equal(db.port, db2.port);
+ assert.equal(db.replica, db2.replica);
+ assert.equal(db.hosts, db2.hosts);
+ assert.equal(db.host, db2.host);
+ assert.equal(db.port, db2.port);
+ assert.equal(db.user, db2.user);
+ assert.equal(db.pass, db2.pass);
+ assert.deepEqual(db.options, db2.options);
+
+ done();
+ });
+ it('saves correctly', function (done) {
+ var db = start();
+ var db2 = db.openNewDb('mongoose-test-2');
+
+ var schema = new Schema({
+ body : String,
+ thing : Number
+ });
+
+ var m1 = db.model('testMod', schema);
+ var m2 = db2.model('testMod', schema);
+
+ m1.create({ body : 'this is some text', thing : 1 }, function (err, i1) {
+ assert.ifError(err);
+ m2.create({ body : 'this is another body', thing : 2 }, function (err, i2) {
+ assert.ifError(err);
+
+ m1.count(function (err, num) {
+ assert.ifError(err);
+ assert.equal(num, 1);
+
+ m2.count(function (err, num) {
+ assert.ifError(err);
+ assert.equal(num, 1);
+ done();
+ });
+ });
+ });
+ });
+ });
+
+ it('closes correctly for all dbs', function (done) {
+ var db = start();
+ var db2 = db.openNewDb('mongoose-test-2');
+
+ db2.on('close', function () {
+ done();
+ });
+ db.close();
+
+ });
+ });
})
View
@@ -4,15 +4,17 @@ db.once('open', function () {
// drop the default test database
db.db.dropDatabase(function () {
+ var db2 = db.openNewDb('mongoose-test-2');
+ db2.db.dropDatabase(function() {
+ // drop mongos test db if exists
+ var mongos = process.env.MONGOOSE_MULTI_MONGOS_TEST_URI;
+ if (!mongos) return process.exit();
- // drop mongos test db if exists
- var mongos = process.env.MONGOOSE_MULTI_MONGOS_TEST_URI;
- if (!mongos) return process.exit();
-
- var db = start({ uri: mongos, mongos: true });
- db.once('open', function () {
- db.db.dropDatabase(process.exit);
- })
+ var db = start({ uri: mongos, mongos: true });
+ db.once('open', function () {
+ db.db.dropDatabase(process.exit);
+ });
+ });
});
});

0 comments on commit a0aaebe

Please sign in to comment.