1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

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

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

View File

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