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

@ -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

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var sinon = require('sinon');
var RedNode = require("../../../red/nodes/Node");
@ -28,7 +28,7 @@ describe('Node', function() {
n.should.not.have.property('name');
n.wires.should.be.empty;
});
it('is called with an id, a type and a name',function() {
var n = new RedNode({id:'123',type:'abc',name:'barney'});
n.should.have.property('id','123');
@ -36,7 +36,7 @@ describe('Node', function() {
n.should.have.property('name','barney');
n.wires.should.be.empty;
});
it('is called with an id, a type and some wires',function() {
var n = new RedNode({id:'123',type:'abc',wires:['123','456']});
n.should.have.property('id','123');
@ -44,9 +44,9 @@ describe('Node', function() {
n.should.not.have.property('name');
n.wires.should.have.length(2);
});
});
describe('#close', function() {
it('emits close event when closed',function(done) {
var n = new RedNode({id:'123',type:'abc'});
@ -56,7 +56,7 @@ describe('Node', function() {
var p = n.close();
should.not.exist(p);
});
it('returns a promise when provided a callback with a done parameter',function(testdone) {
var n = new RedNode({id:'123',type:'abc'});
n.on('close',function(done) {
@ -71,8 +71,8 @@ describe('Node', function() {
});
});
});
describe('#receive', function() {
it('emits input event when called', function(done) {
var n = new RedNode({id:'123',type:'abc'});
@ -84,74 +84,84 @@ describe('Node', function() {
n.receive(message);
});
});
describe('#send', function() {
it('emits a single message', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
var n2 = new RedNode({id:'n2',type:'abc'});
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();
});
n1.send(message);
});
it('emits multiple messages on a single output', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
var n2 = new RedNode({id:'n2',type:'abc'});
var messages = [
{payload:"hello world"},
{payload:"hello world again"}
];
var rcvdCount = 0;
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();
}
});
n1.send([messages]);
});
it('emits messages to multiple outputs', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2'],['n3'],['n4','n5']]});
var n2 = new RedNode({id:'n2',type:'abc'});
var n3 = new RedNode({id:'n3',type:'abc'});
var n4 = new RedNode({id:'n4',type:'abc'});
var n5 = new RedNode({id:'n5',type:'abc'});
var messages = [
{payload:"hello world"},
null,
{payload:"hello world again"}
];
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();
}
});
n3.on('input',function(msg) {
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]);
@ -160,7 +170,8 @@ describe('Node', function() {
done();
}
});
// third message sent, clone
n5.on('input',function(msg) {
should.deepEqual(msg,messages[2]);
should.notStrictEqual(msg,messages[2]);
@ -169,7 +180,7 @@ describe('Node', function() {
done();
}
});
n1.send(messages);
});
@ -180,11 +191,11 @@ describe('Node', function() {
n2.on('input',function(msg) {
should.fail(null,null,"unexpected message");
});
setTimeout(function() {
done();
}, 200);
n1.send();
});
@ -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);