Skip to content

Commit

Permalink
Add pubsub silent tests
Browse files Browse the repository at this point in the history
Fix some bugs
  • Loading branch information
maritz committed Jan 25, 2012
1 parent 8e8845f commit e49d8c4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 35 deletions.
39 changes: 21 additions & 18 deletions lib/pubsub.js
Expand Up @@ -20,6 +20,8 @@ var pub_sub_event_emitter = false;

var do_publish = false;

var is_subscribed = false;

/**
* Publish something on the nohm client.
*/
Expand All @@ -42,23 +44,18 @@ var publish = function (channel, payload, parse) {

var initializePubSub = function initializePubSub (callback) {


if (!pub_sub_client) {
return Nohm.logError('A second redis client must be specified to use pub/sub methods. Please declare one.');
} else if (pub_sub_client.subscriptions === true) {
/**
* This will break in the next node_redis version and we'll have to update to
* pub_sub_client.pub_sub_mode
*
* Should cause all pubsub tests to fail, so will be relatively easy to spot.
*/
} else if (is_subscribed === true) {
// already in pubsub mode, don't need to initialize it again.
if (typeof(callback) === 'function') {
callback();
}
return;
}


is_subscribed = true;

pub_sub_all_pattern = Nohm.prefix.channel + '*:*';
pub_sub_event_emitter = new EventEmitter();
pub_sub_event_emitter.setMaxListeners(0);
Expand Down Expand Up @@ -88,9 +85,9 @@ var initializePubSub = function initializePubSub (callback) {
Nohm.logError('A published message is not valid JSON. Was : "'+message+'"');
return;
}

pub_sub_event_emitter.emit(modelName+':'+action, payload);
pub_sub_event_emitter.emit(modelName+':all', action, payload);
//pub_sub_event_emitter.emit(modelName+':all', action, payload);

});
};
Expand All @@ -105,7 +102,9 @@ var initialize = function () {
*/
Nohm.setPubSubClient = function (client, callback) {
pub_sub_client = client;
initializePubSub(callback);
Nohm.closePubSub(function () {
initializePubSub(callback);
});
};

/**
Expand All @@ -121,7 +120,8 @@ var initialize = function () {
* @param {Function} callback Called after the unsubscibe. Parameters: redisClient
*/
Nohm.closePubSub = function closePubSub (callback) {
if (pub_sub_client.subscriptions === true) {
if (is_subscribed === true) {
is_subscribed = false;
pub_sub_client.punsubscribe(pub_sub_all_pattern, function () {
callback(null, pub_sub_client);
});
Expand Down Expand Up @@ -201,7 +201,6 @@ var initialize = function () {
composer = messageComposers[event] || messageComposers.defaultComposer;

payload = composer.apply(this, arguments);

publish(channel, payload);
};

Expand Down Expand Up @@ -236,8 +235,10 @@ var initialize = function () {
* @param {Function} callback Called every time an event of the provided name is published on this model.
*/
Nohm.prototype.subscribe = function (event_name, callback) {
initializePubSub();
pub_sub_event_emitter.on(this.modelName+':'+event_name, callback);
var self = this;
initializePubSub(function () {
pub_sub_event_emitter.on(self.modelName+':'+event_name, callback);
});
};


Expand All @@ -248,8 +249,10 @@ var initialize = function () {
* @param {Function} callback Called once when an event of the provided name is published on this model and then never again.
*/
Nohm.prototype.subscribeOnce = function (event_name, callback) {
initializePubSub();
pub_sub_event_emitter.once(this.modelName+':'+event_name, callback);
var self = this;
initializePubSub(function () {
pub_sub_event_emitter.once(self.modelName+':'+event_name, callback);
});
};

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/relations.js
Expand Up @@ -72,8 +72,8 @@ exports.getAll = function getAll(objName, name) {
* @param {Function} Callback Callback called with (err, num_relations
*/
exports.numLinks = function numLinks(obj_name, relation_name, callback) {
callback = h.getCallback(arguments),
self = this;
callback = h.getCallback(arguments);
var self = this;
relation_name = relation_name && typeof relation_name !== 'function' ? relation_name : 'child';
if (!this.id) {
Nohm.logError('Calling numLinks() even though either the object itself or the relation does not have an id.');
Expand Down
3 changes: 2 additions & 1 deletion test/pubsub/child.js
Expand Up @@ -33,7 +33,8 @@ process.on('message', function (msg) {
nohm.factory(modelName).subscribe(event, function (change) {
process.send({
question: 'subscribe',
answer: change
answer: change,
event: event
})
});
break;
Expand Down
82 changes: 68 additions & 14 deletions test/pubsubTests.js
Expand Up @@ -16,8 +16,6 @@ var after = function (times, fn) {

var error_callback = function (t) {
return function (err) {
if (err)
console.log(arguments);
t.ok(!err, 'Callback received an error');
};
};
Expand Down Expand Up @@ -109,7 +107,7 @@ module.exports = {
child.send(request);
child.on('message', function (msg) {
if (msg.question === request.question) {
callback(msg.answer);
callback(msg);
}
});
};
Expand All @@ -135,11 +133,12 @@ module.exports = {
event: 'create',
modelName: 'Tester'
}
}, function (answer) {
}, function (msg) {
var target = msg.answer.target;
t.ok(instance.id.length > 0, 'ID was not set properly before the child returned the event.');
t.same(instance.id, answer.target.id, 'Id from create event wrong');
t.same(instance.modelName, answer.target.modelName, 'Modelname from create event wrong');
t.same(instance.allProperties(), answer.target.properties, 'Properties from create event wrong');
t.same(instance.id, target.id, 'Id from create event wrong');
t.same(instance.modelName, target.modelName, 'Modelname from create event wrong');
t.same(instance.allProperties(), target.properties, 'Properties from create event wrong');
t.done();
});

Expand All @@ -158,7 +157,8 @@ module.exports = {
event: 'update',
modelName: 'Tester'
}
}, function (answer) {
}, function (msg) {
var answer = msg.answer;
t.ok(instance.id.length > 0, 'ID was not set properly before the child returned the event.');
t.same(instance.id, answer.target.id, 'Id from update event wrong');
t.same(instance.modelName, answer.target.modelName, 'Modelname from update event wrong');
Expand Down Expand Up @@ -190,7 +190,8 @@ module.exports = {
event: 'save',
modelName: 'Tester'
}
}, function (answer) {
}, function (msg) {
var answer = msg.answer;
t.ok(instance.id.length > 0, 'ID was not set properly before the child returned the event.');
t.same(instance.id, answer.target.id, 'Id from save event wrong');
t.same(instance.modelName, answer.target.modelName, 'Modelname from save event wrong');
Expand Down Expand Up @@ -222,7 +223,8 @@ module.exports = {
event: 'remove',
modelName: 'Tester'
}
}, function (answer) {
}, function (msg) {
var answer = msg.answer;
t.same(instance.id, 0, 'ID was not reset properly before the child returned the event.');
t.same(old_id, answer.target.id, 'Id from remove event wrong');
t.same(instance.modelName, answer.target.modelName, 'Modelname from remove event wrong');
Expand Down Expand Up @@ -251,7 +253,8 @@ module.exports = {
event: 'link',
modelName: 'Tester'
}
}, function (answer) {
}, function (msg) {
var answer = msg.answer;
t.ok(instance_child.id.length > 0, 'ID was not set properly before the child returned the event.');
t.same(instance_child.id, answer.child.id, 'Id from link event wrong');
t.same(instance_child.modelName, answer.child.modelName, 'Modelname from link event wrong');
Expand Down Expand Up @@ -281,7 +284,8 @@ module.exports = {
event: 'unlink',
modelName: 'Tester'
}
}, function (answer) {
}, function (msg) {
var answer = msg.answer;
t.ok(instance_child.id.length > 0, 'ID was not set properly before the child returned the event.');
t.same(instance_child.id, answer.child.id, 'Id from unlink event wrong');
t.same(instance_child.modelName, answer.child.modelName, 'Modelname from unlink event wrong');
Expand All @@ -297,7 +301,7 @@ module.exports = {
instance_child.save(function (err) {
error_callback(t)(err);
instance_child.unlink(instance_parent);
instance_child.save(error_callback(t))
instance_child.save(error_callback(t));
});
},

Expand All @@ -314,7 +318,8 @@ module.exports = {
event: 'create',
modelName: 'Tester'
}
}, function (answer) {
}, function (msg) {
var answer = msg.answer;
once_done++;
t.ok(instance.id.length > 0, 'ID was not set properly before the child returned the event.');
t.same(instance.id, answer.target.id, 'Id from createOnce event wrong');
Expand All @@ -332,6 +337,55 @@ module.exports = {
});

instance.save(error_callback(t));
},

'silenced': function (t) {
t.expect(6);
var self = this;
var instance = nohm.factory('Tester');
instance.p('dummy', 'silenced');
var answered = false;

var events = ['create', 'update', 'save', 'remove', 'link', 'unlink'];

events.forEach(function (event) {
self.child.ask({
question: 'subscribe',
args: {
event: event,
modelName: 'Tester'
}
}, function (msg) {
if (msg.event === event) {
console.log(msg);
answered = true;
}
});
});

instance.save({silent: true}, function (err) {
t.ok(!err, 'There was an error while saving silenced.');
instance.p('dummy', 'updated');
instance.save({silent: true}, function (err) {
t.ok(!err, 'There was an error while updating silenced.');
var second = nohm.factory('Tester');
instance.link(second);
instance.save({silent: true}, function (err) {
t.ok(!err, 'There was an error while linking silenced.');
instance.unlink(second);
instance.save({silent: true}, function (err) {
t.ok(!err, 'There was an error while unlinking silenced.');
instance.remove({silent: true}, function (err) {
t.ok(!err, 'There was an error while removing silenced.');
setTimeout(function () {
t.same(answered, false, 'There was an event!');
t.done();
}, 150);
});
});
});
});
});
}
}
};

0 comments on commit e49d8c4

Please sign in to comment.