Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'v0.5'

Conflicts:
	lib/adapters/sql/mysql.js
  • Loading branch information...
commit ed8b686ffb1e19468492f503092afb24fd9dcdd6 2 parents ce56a41 + 390b1c4
@mde mde authored
View
28 lib/adapters/sql/mysql.js
@@ -45,23 +45,29 @@ utils.mixin(Adapter.prototype, new (function () {
this.COLUMN_NAME_DELIMITER = '`';
- this.handleDisconnect = function () {
+ // Pseudo-private -- utility and for testing only
+ this._getClient = function () {
+ return mysql.createConnection(this.config);
+ };
+
+ this._handleDisconnect = function (callback) {
var self = this;
setTimeout(function() {
- self.client = mysql.createConnection(self.config);
- self.connect();
+ self.client = self._getClient();
+ self.connect(callback);
}, Math.pow(3, reconnectAttempts) * 1000);
reconnectAttempts++;
};
this.init = function () {
- var self = this;
- this.client = mysql.createConnection(this.config);
- this.client.on('error', function(err) {
- if (err.code === 'PROTOCOL_CONNECTION_LOST' && reconnectAttempts < RECONNECT_TRIES_LIMIT) {
- self.handleDisconnect();
- };
- });
+ var self = this;
+ this.client = this._getClient();
+ this.client.on('error', function(err) {
+ if (err.code === 'PROTOCOL_CONNECTION_LOST' &&
+ reconnectAttempts < RECONNECT_TRIES_LIMIT) {
+ self._handleDisconnect();
+ };
+ });
};
this.connect = function (callback) {
@@ -70,7 +76,7 @@ utils.mixin(Adapter.prototype, new (function () {
this.client.connect(function (err, data) {
if (err) {
if (reconnectAttempts < RECONNECT_TRIES_LIMIT) {
- self.handleDisconnect();
+ self._handleDisconnect(callback);
} else {
self.emit('error', err);
cb(err);
View
2  package.json
@@ -17,7 +17,7 @@
"leveldb",
"sqlite"
],
- "version": "0.5.6",
+ "version": "0.5.8",
"author": "Matthew Eernisse <mde@fleegix.org> (http://fleegix.org)",
"main": "./lib/index.js",
"scripts": {
View
1  test/fixtures/person.js
@@ -10,6 +10,7 @@ var Person = function () {
this.hasMany('Events', {through: 'Participations'});
this.hasMany('Frienders', {through: 'Friendships', model: 'People'});
this.hasMany('Friends', {through: 'Friendships', model: 'People'});
+ this.hasMany('Children', {model: 'People'});
};
Person.prototype.someMethod = function () {
View
19 test/integration/adapters/shared.js
@@ -923,6 +923,25 @@ tests = {
});
}
+, 'test named hasMany with same model (reflexive association)': function (next) {
+ model.Person.all(function (err, data) {
+ if (err) { throw err; }
+ var children = data.slice()
+ , person = children.shift();
+ children.forEach(function (c) {
+ person.addChild(c);
+ });
+ person.save(function (err, data) {
+ if (err) { throw err; }
+ person.getChildren(function (err, data) {
+ if (err) { throw err; }
+ assert.equal(19, data.length);
+ next();
+ });
+ });
+ });
+ }
+
, 'test named hasMany/through with same model (reflexive association)': function (next) {
model.Person.all(function (err, data) {
if (err) { throw err; }
View
19 test/integration/adapters/sql/eager_assn.js
@@ -27,6 +27,25 @@ tests = {
});
}
+, 'test includes eager-fetch of reflexive hasMany association': function (next) {
+ model.Person.all(function (err, data) {
+ if (err) { throw err; }
+ var children = data.slice()
+ , person = children.shift();
+ children.forEach(function (c) {
+ person.addChild(c);
+ });
+ person.save(function (err, data) {
+ if (err) { throw err; }
+ model.Person.first({id: person.id}, {includes: 'children'}, function (err, data) {
+ if (err) { throw err; }
+ assert.equal(19, data.children.length);
+ next();
+ });
+ });
+ });
+ }
+
, 'test includes eager-fetch of named hasMany/through association': function (next) {
model.Event.all(function (err, data) {
if (err) { throw err; }
View
29 test/integration/adapters/sql/mysql.js
@@ -60,6 +60,35 @@ tests = {
assert.ok(adapter instanceof Adapter);
}
+, 'test reconnect logic': function (next) {
+ // Get a reference to the original client func
+ var origGetClient = adapter._getClient;
+ // Punch out the client func with one that returns a client
+ // whose `connect` method always produces an error
+ adapter._getClient = function () {
+ var client = origGetClient.call(adapter);
+ // Always error
+ client.connect = function (cb) {
+ cb({}, null);
+ };
+ return client;
+ };
+ // Disconnect, and try to reconnect -- this will fail
+ adapter.disconnect(function (err, data) {
+ // Until the _getClient function is replaced, this will continue
+ // retrying, once we put it back, reconnect will succeed and
+ // tests will resume
+ adapter.connect(function (err, data) {
+ next();
+ });
+ // Wait three seconds, restore the _getClient func to return
+ // a good client
+ setTimeout(function () {
+ adapter._getClient = origGetClient;
+ }, 3000);
+ });
+ }
+
, 'test exec': function (next) {
adapter.exec('CREATE TABLE foo (bar varchar(256) ); DROP TABLE foo;',
function (err, data) {
Please sign in to comment.
Something went wrong with that request. Please try again.