Permalink
Browse files

Added warnings to setClient/getClient if the redis client is not conn…

…ected.
  • Loading branch information...
1 parent 732ba04 commit 3f34652cb6898dcd500327c588a00178534ffa82 @maritz committed Jun 6, 2012
Showing with 21 additions and 8 deletions.
  1. +14 −4 lib/nohm.js
  2. +3 −0 test/featureTests.js
  3. +4 −4 test/pubsub/child.js
View
@@ -143,6 +143,9 @@ Nohm.setPrefix = function (prefix) {
*/
Nohm.setClient = function (client) {
Nohm.client = client;
+ if ( ! client.connected) {
+ Nohm.logError('Warning: setClient() received a redis client that is not connected yet. Consider waiting for an established connection before setting it.');
+ }
};
@@ -185,7 +188,11 @@ Nohm.prototype.getHashKey = function (id) {
* Returns the client of either the model (if set) or the global Nohm object.
*/
Nohm.prototype.getClient = function () {
- return this.client || Nohm.client;
+ var client = this.client || Nohm.client;
+ if ( ! client.connected) {
+ Nohm.logError('Warning: Tried accessing a redis client that is not connected to a database. The redis client should buffer the commands and send them once connected. But if it can\'t connect they are lost.');
+ }
+ return client;
};
var addMethods = function (methods) {
@@ -200,11 +207,14 @@ var addMethods = function (methods) {
};
Nohm.prototype.init = function (name, options) {
- if ( ! name )
+ if ( ! name ) {
this.logError('When creating a new model you have to provide a name!');
+ }
- if (typeof(options.client) === 'undefined' && Nohm.client === null)
- return Nohm.logError('Did not find a viable redis client in Nohm or the model: '+name) && false;
+ if (typeof(options.client) === 'undefined' && Nohm.client === null) {
+ Nohm.logError('Did not find a viable redis client in Nohm or the model: '+name);
+ return false;
+ }
if (typeof(options.client) !== 'undefined') {
this.client = options.client;
View
@@ -155,6 +155,9 @@ exports.prepare = {
nohm.client = null;
var user = new UserMockup();
t.same(user, {}, 'Creating a model without having a nohm client set did not return false.');
+
+ console.log('Note: there should be an error message in the next line. (intended behaviour)');
+ nohm.setClient(require('redis').createClient(80, '123.123.123.123')); // this should not connect.
nohm.setClient(redis);
user = new UserMockup();
View
@@ -3,7 +3,7 @@ var nohm = require(__dirname+'/../../lib/nohm').Nohm;
var args = require(__dirname+'/../testArgs.js');
nohm.setPrefix(args.prefix);
-nohm.setClient(args.setClient);
+nohm.setClient(args.redis);
require(__dirname+'/Model.js');
process.on('message', function (msg) {
@@ -35,7 +35,7 @@ process.on('message', function (msg) {
question: 'subscribe',
answer: change,
event: event
- })
+ });
});
break;
@@ -46,7 +46,7 @@ process.on('message', function (msg) {
process.send({
question: 'subscribeOnce',
answer: change
- })
+ });
});
break;
@@ -58,7 +58,7 @@ process.on('message', function (msg) {
process.send({
question: 'unsubscribe',
answer: true
- })
+ });
break;
}
});

0 comments on commit 3f34652

Please sign in to comment.