Node does not clone first message sent

Tests updated to mirror this behaviour
    Annotated algorithm
This commit is contained in:
Anna Thomas 2014-10-10 10:50:54 +01:00
parent 7802939bb0
commit bc8e459ae6
2 changed files with 123 additions and 91 deletions

View File

@ -29,119 +29,128 @@ function Node(n) {
if (n.name) {
this.name = n.name;
}
this.wires = n.wires||[];
this.wires = n.wires || [];
}
util.inherits(Node,EventEmitter);
util.inherits(Node, EventEmitter);
Node.prototype._on = Node.prototype.on;
Node.prototype.on = function(event,callback) {
Node.prototype.on = function(event, callback) {
var node = this;
if (event == "close") {
if (callback.length == 1) {
this.close = function() {
return when.promise(function(resolve) {
callback.call(node,function() {
callback.call(node, function() {
resolve();
});
});
}
};
} else {
this.close = callback;
}
} else {
this._on(event,callback);
this._on(event, callback);
}
}
};
Node.prototype.close = function() {
Node.prototype.close = function() {};
function cloneMessage(msg) {
// Temporary fix for #97
// TODO: remove this http-node-specific fix somehow
var req = msg.req;
var res = msg.res;
delete msg.req;
delete msg.res;
var m = clone(msg);
if (req) {
m.req = req;
msg.req = req;
}
if (res) {
m.res = res;
msg.res = res;
}
return m;
}
Node.prototype.send = function(msg) {
var msgSent = false;
// instanceof doesn't work for some reason here
if (msg == null) {
if (msg === null || typeof msg === "undefined") {
return;
} else if (!util.isArray(msg)) {
msg = [msg];
}
for (var i=0;i<this.wires.length;i++) {
var wires = this.wires[i];
var numOutputs = this.wires.length;
// for each output of node eg. [msgs to output 0, msgs to output 1, ...]
for (var i = 0; i < numOutputs; i++) {
var wires = this.wires[i]; // wires leaving output i
if (i < msg.length) {
if (msg[i] != null) {
var msgs = msg[i];
if (!util.isArray(msg[i])) {
msgs = [msg[i]];
var msgs = msg[i]; // msgs going to output i
if (msgs !== null && typeof msgs !== "undefined") {
if (!util.isArray(msgs)) {
msgs = [msgs];
}
//if (wires.length == 1) {
// // Single recipient, don't need to clone the message
// var node = flows.get(wires[0]);
// if (node) {
// for (var k in msgs) {
// var mm = msgs[k];
// node.receive(mm);
// }
// }
//} else {
// Multiple recipients, must send message copies
for (var j=0;j<wires.length;j++) {
var node = flows.get(wires[j]);
if (node) {
for (var k=0;k<msgs.length;k++) {
var mm = msgs[k];
// Temporary fix for #97
// TODO: remove this http-node-specific fix somehow
var req = mm.req;
var res = mm.res;
delete mm.req;
delete mm.res;
var m = clone(mm);
if (req) {
m.req = req;
mm.req = req;
}
if (res) {
m.res = res;
mm.res = res;
}
node.receive(m);
var node;
var k = 0;
// for each recipent node of that output
for (var j = 0; j < wires.length; j++) {
node = flows.get(wires[j]); // node at end of wire j
if (node) {
// for each msg to send eg. [[m1, m2, ...], ...]
for (k = 0; k < msgs.length; k++) {
if (msgSent) {
// a msg has already been sent so clone
node.receive(cloneMessage(msgs[k]));
} else {
// first msg sent so don't clone
node.receive(msgs[k]);
msgSent = true;
}
}
}
//}
}
}
}
}
}
};
Node.prototype.receive = function(msg) {
this.emit("input",msg);
}
this.emit("input", msg);
};
function log_helper(self, level, msg) {
var o = {level:level, id:self.id, type:self.type, msg:msg};
var o = {
level: level,
id: self.id,
type: self.type,
msg: msg
};
if (self.name) {
o.name = self.name;
}
self.emit("log",o);
self.emit("log", o);
}
Node.prototype.log = function(msg) {
log_helper(this, 'log', msg);
}
};
Node.prototype.warn = function(msg) {
log_helper(this, 'warn', msg);
}
};
Node.prototype.error = function(msg) {
log_helper(this, 'error', msg);
}
};
/**
* status: { fill:"red|green", shape:"dot|ring", text:"blah" }
*/
Node.prototype.status = function(status) {
comms.publish("status/"+this.id,status,true);
}
comms.publish("status/" + this.id, status, true);
};
module.exports = Node;

View File

@ -93,9 +93,9 @@ describe('Node', function() {
var message = {payload:"hello world"};
n2.on('input',function(msg) {
// msg equals message, but is a new copy
// msg equals message, and is not a new copy
should.deepEqual(msg,message);
should.notStrictEqual(msg,message);
should.strictEqual(msg,message);
done();
});
@ -115,9 +115,17 @@ describe('Node', function() {
n2.on('input',function(msg) {
should.deepEqual(msg,messages[rcvdCount]);
should.notStrictEqual(msg,messages[rcvdCount]);
if (rcvdCount === 0) {
// first msg sent, don't clone
should.strictEqual(msg,messages[rcvdCount]);
} else {
// second msg sent, clone
should.notStrictEqual(msg,messages[rcvdCount]);
}
rcvdCount += 1;
if (rcvdCount == 2) {
if (rcvdCount === 2) {
done();
}
});
@ -139,9 +147,10 @@ describe('Node', function() {
var rcvdCount = 0;
// first message sent, don't clone
n2.on('input',function(msg) {
should.deepEqual(msg,messages[0]);
should.notStrictEqual(msg,messages[0]);
should.strictEqual(msg,messages[0]);
rcvdCount += 1;
if (rcvdCount == 3) {
done();
@ -152,6 +161,7 @@ describe('Node', function() {
should.fail(null,null,"unexpected message");
});
// second message sent, clone
n4.on('input',function(msg) {
should.deepEqual(msg,messages[2]);
should.notStrictEqual(msg,messages[2]);
@ -161,6 +171,7 @@ describe('Node', function() {
}
});
// third message sent, clone
n5.on('input',function(msg) {
should.deepEqual(msg,messages[2]);
should.notStrictEqual(msg,messages[2]);
@ -197,9 +208,10 @@ describe('Node', function() {
{payload:"hello world again"}
];
// only one message sent, so no copy needed
n2.on('input',function(msg) {
should.deepEqual(msg,messages[1]);
should.notStrictEqual(msg,messages[1]);
should.strictEqual(msg,messages[1]);
done();
});
@ -207,15 +219,26 @@ describe('Node', function() {
});
it('emits messages without cloning req or res', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2'],['n3']]});
var n2 = new RedNode({id:'n2',type:'abc'});
var n3 = new RedNode({id:'n3',type:'abc'});
var req = {};
var res = {};
var cloned = {};
var message = {payload: "foo", cloned: cloned, req: req, res: res};
// first message to be sent, so should not be cloned
n2.on('input',function(msg) {
should.deepEqual(msg, message);
msg.cloned.should.be.exactly(message.cloned);
msg.req.should.be.exactly(message.req);
msg.res.should.be.exactly(message.res);
done();
});
// second message to be sent, so should be cloned
n3.on('input',function(msg) {
should.deepEqual(msg, message);
msg.cloned.should.not.be.exactly(message.cloned);
msg.req.should.be.exactly(message.req);