2014-05-03 23:26:35 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-05-03 23:26:35 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
var should = require("should");
|
2014-07-30 11:39:48 +02:00
|
|
|
var sinon = require('sinon');
|
2018-08-20 17:17:24 +02:00
|
|
|
var NR_TEST_UTILS = require("nr-test-utils");
|
|
|
|
var RedNode = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/Node");
|
|
|
|
var Log = NR_TEST_UTILS.require("@node-red/util").log;
|
|
|
|
var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/flows");
|
2013-10-20 22:42:01 +02:00
|
|
|
|
|
|
|
describe('Node', function() {
|
|
|
|
describe('#constructor',function() {
|
|
|
|
it('is called with an id and a type',function() {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
n.should.have.property('id','123');
|
|
|
|
n.should.have.property('type','abc');
|
|
|
|
n.should.not.have.property('name');
|
2016-10-10 14:27:43 +02:00
|
|
|
n.wires.should.be.empty();
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
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');
|
|
|
|
n.should.have.property('type','abc');
|
|
|
|
n.should.have.property('name','barney');
|
2016-10-10 14:27:43 +02:00
|
|
|
n.wires.should.be.empty();
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
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');
|
|
|
|
n.should.have.property('type','abc');
|
|
|
|
n.should.not.have.property('name');
|
2013-11-10 23:19:01 +01:00
|
|
|
n.wires.should.have.length(2);
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
describe('#close', function() {
|
|
|
|
it('emits close event when closed',function(done) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
2014-05-14 22:46:07 +02:00
|
|
|
n.on('close',function() {
|
|
|
|
done();
|
|
|
|
});
|
2019-01-17 14:18:26 +01:00
|
|
|
n.close();
|
2014-05-14 22:46:07 +02:00
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2014-05-14 22:46:07 +02:00
|
|
|
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) {
|
|
|
|
setTimeout(function() {
|
|
|
|
done();
|
2017-04-22 00:36:21 +02:00
|
|
|
},50);
|
2014-05-14 22:46:07 +02:00
|
|
|
});
|
|
|
|
var p = n.close();
|
|
|
|
should.exist(p);
|
|
|
|
p.then(function() {
|
|
|
|
testdone();
|
|
|
|
});
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2017-04-22 00:36:21 +02:00
|
|
|
it('accepts a callback with "removed" and "done" parameters', function(testdone) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
var receivedRemoved;
|
|
|
|
n.on('close',function(removed,done) {
|
|
|
|
receivedRemoved = removed;
|
|
|
|
setTimeout(function() {
|
|
|
|
done();
|
|
|
|
},50);
|
|
|
|
});
|
|
|
|
var p = n.close(true);
|
|
|
|
should.exist(p);
|
|
|
|
(receivedRemoved).should.be.true();
|
|
|
|
p.then(function() {
|
|
|
|
testdone();
|
|
|
|
});
|
|
|
|
})
|
2015-03-04 22:42:11 +01:00
|
|
|
it('allows multiple close handlers to be registered',function(testdone) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
var callbacksClosed = 0;
|
|
|
|
n.on('close',function(done) {
|
|
|
|
setTimeout(function() {
|
|
|
|
callbacksClosed++;
|
|
|
|
done();
|
2017-04-22 00:36:21 +02:00
|
|
|
},50);
|
2015-03-04 22:42:11 +01:00
|
|
|
});
|
|
|
|
n.on('close',function(done) {
|
|
|
|
setTimeout(function() {
|
|
|
|
callbacksClosed++;
|
|
|
|
done();
|
2017-04-22 00:36:21 +02:00
|
|
|
},75);
|
2015-03-04 22:42:11 +01:00
|
|
|
});
|
|
|
|
n.on('close',function() {
|
|
|
|
callbacksClosed++;
|
|
|
|
});
|
|
|
|
var p = n.close();
|
|
|
|
should.exist(p);
|
|
|
|
p.then(function() {
|
|
|
|
callbacksClosed.should.eql(3);
|
|
|
|
testdone();
|
2018-04-24 16:01:49 +02:00
|
|
|
}).catch(function(e) {
|
2015-03-04 22:42:11 +01:00
|
|
|
testdone(e);
|
|
|
|
});
|
|
|
|
});
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
describe('#receive', function() {
|
|
|
|
it('emits input event when called', function(done) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
var message = {payload:"hello world"};
|
|
|
|
n.on('input',function(msg) {
|
|
|
|
should.deepEqual(msg,message);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
n.receive(message);
|
|
|
|
});
|
2015-03-08 16:26:24 +01:00
|
|
|
|
2015-01-27 15:41:20 +01:00
|
|
|
it('writes metric info with undefined msg', function(done){
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
n.on('input',function(msg) {
|
|
|
|
(typeof msg).should.not.be.equal("undefined");
|
2015-02-06 17:36:32 +01:00
|
|
|
(typeof msg._msgid).should.not.be.equal("undefined");
|
2015-01-27 15:41:20 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
n.receive();
|
|
|
|
});
|
2015-03-08 16:26:24 +01:00
|
|
|
|
2015-01-27 15:41:20 +01:00
|
|
|
it('writes metric info with null msg', function(done){
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
n.on('input',function(msg) {
|
|
|
|
(typeof msg).should.not.be.equal("undefined");
|
2015-02-06 17:36:32 +01:00
|
|
|
(typeof msg._msgid).should.not.be.equal("undefined");
|
2015-01-27 15:41:20 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
n.receive(null);
|
|
|
|
});
|
2015-08-19 22:35:54 +02:00
|
|
|
|
2015-05-02 23:15:33 +02:00
|
|
|
it('handles thrown errors', function(done) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
sinon.stub(n,"error",function(err,msg) {});
|
|
|
|
var message = {payload:"hello world"};
|
|
|
|
n.on('input',function(msg) {
|
|
|
|
throw new Error("test error");
|
|
|
|
});
|
|
|
|
n.receive(message);
|
2019-07-09 00:23:33 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
n.error.called.should.be.true();
|
|
|
|
n.error.firstCall.args[1].should.equal(message);
|
|
|
|
done();
|
|
|
|
},50);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls parent flow handleComplete when callback provided', function(done) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc', _flow: {
|
|
|
|
handleComplete: function(node,msg) {
|
|
|
|
try {
|
|
|
|
msg.should.deepEqual(message);
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}});
|
2015-08-19 22:35:54 +02:00
|
|
|
|
2019-07-09 00:23:33 +02:00
|
|
|
var message = {payload:"hello world"};
|
2019-07-09 12:40:55 +02:00
|
|
|
n.on('input',function(msg, nodeSend, nodeDone) {
|
2019-07-09 00:23:33 +02:00
|
|
|
nodeDone();
|
|
|
|
});
|
|
|
|
n.receive(message);
|
|
|
|
});
|
|
|
|
it('logs error if callback provides error', function(done) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
sinon.stub(n,"error",function(err,msg) {});
|
|
|
|
|
|
|
|
var message = {payload:"hello world"};
|
2019-07-09 12:40:55 +02:00
|
|
|
n.on('input',function(msg, nodeSend, nodeDone) {
|
2019-07-09 00:23:33 +02:00
|
|
|
nodeDone(new Error("test error"));
|
|
|
|
setTimeout(function() {
|
|
|
|
try {
|
|
|
|
n.error.called.should.be.true();
|
|
|
|
n.error.firstCall.args[0].toString().should.equal("Error: test error");
|
|
|
|
n.error.firstCall.args[1].should.equal(message);
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
},50);
|
|
|
|
});
|
|
|
|
n.receive(message);
|
2015-05-02 23:15:33 +02:00
|
|
|
});
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
describe('#send', function() {
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
it('emits a single message', function(done) {
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},
|
|
|
|
};
|
|
|
|
var n1 = new RedNode({_flow:flow,id:'n1',type:'abc',wires:[['n2']]});
|
|
|
|
var n2 = new RedNode({_flow:flow,id:'n2',type:'abc'});
|
2013-10-20 22:42:01 +02:00
|
|
|
var message = {payload:"hello world"};
|
2019-07-09 00:23:33 +02:00
|
|
|
var messageReceived = false;
|
2013-10-20 22:42:01 +02:00
|
|
|
n2.on('input',function(msg) {
|
2014-10-10 11:50:54 +02:00
|
|
|
// msg equals message, and is not a new copy
|
2019-07-09 00:23:33 +02:00
|
|
|
messageReceived = true;
|
2020-09-04 10:20:30 +02:00
|
|
|
should.strictEqual(this,n2);
|
2013-10-20 22:42:01 +02:00
|
|
|
should.deepEqual(msg,message);
|
2014-10-10 11:50:54 +02:00
|
|
|
should.strictEqual(msg,message);
|
2013-10-20 22:42:01 +02:00
|
|
|
done();
|
|
|
|
});
|
2019-07-09 00:23:33 +02:00
|
|
|
n1.send(message);
|
|
|
|
messageReceived.should.be.false();
|
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2020-09-04 10:20:30 +02:00
|
|
|
it('emits a single message - multiple input event listeners', function(done) {
|
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},
|
|
|
|
};
|
|
|
|
var n1 = new RedNode({_flow:flow,id:'n1',type:'abc',wires:[['n2']]});
|
|
|
|
var n2 = new RedNode({_flow:flow,id:'n2',type:'abc'});
|
|
|
|
var message = {payload:"hello world"};
|
|
|
|
var messageReceived = 0;
|
|
|
|
n2.on('input',function(msg) {
|
|
|
|
// msg equals message, and is not a new copy
|
|
|
|
messageReceived++;
|
|
|
|
messageReceived.should.be.exactly(1);
|
|
|
|
should.strictEqual(this,n2);
|
|
|
|
should.deepEqual(msg,message);
|
|
|
|
should.strictEqual(msg,message);
|
|
|
|
});
|
2020-09-04 10:52:30 +02:00
|
|
|
n2.on('input',function(msg) {
|
2020-09-04 10:20:30 +02:00
|
|
|
messageReceived++;
|
|
|
|
messageReceived.should.be.exactly(2);
|
|
|
|
should.strictEqual(this,n2);
|
|
|
|
should.deepEqual(msg,message);
|
|
|
|
should.strictEqual(msg,message);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
n1.send(message);
|
|
|
|
messageReceived.should.be.exactly(0);
|
|
|
|
});
|
|
|
|
|
2019-07-09 00:23:33 +02:00
|
|
|
it('emits a single message - synchronous mode', function(done) {
|
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},
|
|
|
|
asyncMessageDelivery: false
|
|
|
|
};
|
|
|
|
var n1 = new RedNode({_flow:flow,id:'n1',type:'abc',wires:[['n2']]});
|
|
|
|
var n2 = new RedNode({_flow:flow,id:'n2',type:'abc'});
|
|
|
|
var message = {payload:"hello world"};
|
|
|
|
var messageReceived = false;
|
|
|
|
var notSyncErr;
|
|
|
|
n2.on('input',function(msg) {
|
|
|
|
try {
|
|
|
|
// msg equals message, and is not a new copy
|
|
|
|
messageReceived = true;
|
|
|
|
should.deepEqual(msg,message);
|
|
|
|
should.strictEqual(msg,message);
|
|
|
|
done(notSyncErr);
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
2013-10-20 22:42:01 +02:00
|
|
|
n1.send(message);
|
2019-07-09 00:23:33 +02:00
|
|
|
try {
|
|
|
|
messageReceived.should.be.true();
|
|
|
|
} catch(err) {
|
|
|
|
notSyncErr = err;
|
|
|
|
}
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2019-07-09 12:40:55 +02:00
|
|
|
it('emits a message with callback provided send', function(done) {
|
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},
|
|
|
|
handleComplete: (node,msg) => {}
|
|
|
|
};
|
|
|
|
var n1 = new RedNode({_flow:flow,id:'n1',type:'abc',wires:[['n2']]});
|
|
|
|
var n2 = new RedNode({_flow:flow,id:'n2',type:'abc'});
|
|
|
|
var message = {payload:"hello world"};
|
|
|
|
var messageReceived = false;
|
|
|
|
n1.on('input',function(msg,nodeSend,nodeDone) {
|
|
|
|
nodeSend(msg);
|
|
|
|
nodeDone();
|
|
|
|
});
|
|
|
|
n2.on('input',function(msg) {
|
|
|
|
// msg equals message, and is not a new copy
|
|
|
|
messageReceived = true;
|
|
|
|
should.deepEqual(msg,message);
|
|
|
|
should.strictEqual(msg,message);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
n1.receive(message);
|
|
|
|
messageReceived.should.be.false();
|
|
|
|
});
|
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
it('emits multiple messages on a single output', function(done) {
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},
|
|
|
|
};
|
|
|
|
var n1 = new RedNode({_flow:flow,id:'n1',type:'abc',wires:[['n2']]});
|
|
|
|
var n2 = new RedNode({_flow:flow,id:'n2',type:'abc'});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
var messages = [
|
|
|
|
{payload:"hello world"},
|
|
|
|
{payload:"hello world again"}
|
|
|
|
];
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
var rcvdCount = 0;
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2015-03-08 16:26:24 +01:00
|
|
|
n2.on('input',function(msg) {
|
2014-10-10 11:50:54 +02:00
|
|
|
if (rcvdCount === 0) {
|
|
|
|
// first msg sent, don't clone
|
2015-01-27 15:41:20 +01:00
|
|
|
should.deepEqual(msg,messages[rcvdCount]);
|
2014-10-10 11:50:54 +02:00
|
|
|
should.strictEqual(msg,messages[rcvdCount]);
|
2015-01-27 15:41:20 +01:00
|
|
|
rcvdCount += 1;
|
2014-10-10 11:50:54 +02:00
|
|
|
} else {
|
|
|
|
// second msg sent, clone
|
2015-01-27 15:41:20 +01:00
|
|
|
msg.payload.should.equal(messages[rcvdCount].payload);
|
2014-10-10 11:50:54 +02:00
|
|
|
should.notStrictEqual(msg,messages[rcvdCount]);
|
2013-10-20 22:42:01 +02:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
n1.send([messages]);
|
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
it('emits messages to multiple outputs', function(done) {
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':n1,'n2':n2,'n3':n3,'n4':n4,'n5':n5}[id]},
|
|
|
|
};
|
|
|
|
var n1 = new RedNode({_flow:flow, id:'n1',type:'abc',wires:[['n2'],['n3'],['n4','n5']]});
|
|
|
|
var n2 = new RedNode({_flow:flow, id:'n2',type:'abc'});
|
|
|
|
var n3 = new RedNode({_flow:flow, id:'n3',type:'abc'});
|
|
|
|
var n4 = new RedNode({_flow:flow, id:'n4',type:'abc'});
|
|
|
|
var n5 = new RedNode({_flow:flow, id:'n5',type:'abc'});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
var messages = [
|
|
|
|
{payload:"hello world"},
|
|
|
|
null,
|
|
|
|
{payload:"hello world again"}
|
|
|
|
];
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
var rcvdCount = 0;
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2015-03-08 16:26:24 +01:00
|
|
|
// first message sent, don't clone
|
2015-01-27 15:41:20 +01:00
|
|
|
// message uuids should match
|
2013-10-20 22:42:01 +02:00
|
|
|
n2.on('input',function(msg) {
|
|
|
|
should.deepEqual(msg,messages[0]);
|
2014-10-10 11:50:54 +02:00
|
|
|
should.strictEqual(msg,messages[0]);
|
2013-10-20 22:42:01 +02:00
|
|
|
rcvdCount += 1;
|
|
|
|
if (rcvdCount == 3) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
n3.on('input',function(msg) {
|
|
|
|
should.fail(null,null,"unexpected message");
|
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
|
|
|
// second message sent, clone
|
2015-01-27 15:41:20 +01:00
|
|
|
// message uuids wont match since we've cloned
|
2013-10-20 22:42:01 +02:00
|
|
|
n4.on('input',function(msg) {
|
2015-01-27 15:41:20 +01:00
|
|
|
msg.payload.should.equal(messages[2].payload);
|
2013-10-20 22:42:01 +02:00
|
|
|
should.notStrictEqual(msg,messages[2]);
|
|
|
|
rcvdCount += 1;
|
|
|
|
if (rcvdCount == 3) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
|
|
|
// third message sent, clone
|
2015-01-27 15:41:20 +01:00
|
|
|
// message uuids wont match since we've cloned
|
2013-10-20 22:42:01 +02:00
|
|
|
n5.on('input',function(msg) {
|
2015-01-27 15:41:20 +01:00
|
|
|
msg.payload.should.equal(messages[2].payload);
|
2013-10-20 22:42:01 +02:00
|
|
|
should.notStrictEqual(msg,messages[2]);
|
|
|
|
rcvdCount += 1;
|
|
|
|
if (rcvdCount == 3) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
n1.send(messages);
|
|
|
|
});
|
2014-07-09 08:42:09 +02:00
|
|
|
|
2014-07-09 09:00:35 +02:00
|
|
|
it('emits no messages', function(done) {
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},
|
|
|
|
};
|
|
|
|
var n1 = new RedNode({_flow:flow,id:'n1',type:'abc',wires:[['n2']]});
|
|
|
|
var n2 = new RedNode({_flow:flow,id:'n2',type:'abc'});
|
2014-07-09 09:00:35 +02:00
|
|
|
|
|
|
|
n2.on('input',function(msg) {
|
|
|
|
should.fail(null,null,"unexpected message");
|
|
|
|
});
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2014-07-09 09:00:35 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
done();
|
|
|
|
}, 200);
|
2014-10-10 11:50:54 +02:00
|
|
|
|
2014-07-09 09:00:35 +02:00
|
|
|
n1.send();
|
|
|
|
});
|
|
|
|
|
2014-07-14 22:17:14 +02:00
|
|
|
it('emits messages ignoring non-existent nodes', function(done) {
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},
|
|
|
|
};
|
|
|
|
var n1 = new RedNode({_flow:flow,id:'n1',type:'abc',wires:[['n9'],['n2']]});
|
|
|
|
var n2 = new RedNode({_flow:flow,id:'n2',type:'abc'});
|
2015-03-08 16:26:24 +01:00
|
|
|
|
2014-07-14 22:17:14 +02:00
|
|
|
var messages = [
|
|
|
|
{payload:"hello world"},
|
|
|
|
{payload:"hello world again"}
|
|
|
|
];
|
|
|
|
|
2014-10-10 11:50:54 +02:00
|
|
|
// only one message sent, so no copy needed
|
2014-07-14 22:17:14 +02:00
|
|
|
n2.on('input',function(msg) {
|
|
|
|
should.deepEqual(msg,messages[1]);
|
2014-10-10 11:50:54 +02:00
|
|
|
should.strictEqual(msg,messages[1]);
|
2014-07-14 22:17:14 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
n1.send(messages);
|
|
|
|
});
|
|
|
|
|
2014-07-30 11:28:25 +02:00
|
|
|
it('emits messages without cloning req or res', function(done) {
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':n1,'n2':n2,'n3':n3}[id]},
|
|
|
|
};
|
|
|
|
var n1 = new RedNode({_flow:flow,id:'n1',type:'abc',wires:[[['n2'],['n3']]]});
|
|
|
|
var n2 = new RedNode({_flow:flow,id:'n2',type:'abc'});
|
|
|
|
var n3 = new RedNode({_flow:flow,id:'n3',type:'abc'});
|
2014-07-30 11:28:25 +02:00
|
|
|
|
|
|
|
var req = {};
|
|
|
|
var res = {};
|
|
|
|
var cloned = {};
|
|
|
|
var message = {payload: "foo", cloned: cloned, req: req, res: res};
|
|
|
|
|
2015-02-02 14:21:19 +01:00
|
|
|
var rcvdCount = 0;
|
2015-03-08 16:26:24 +01:00
|
|
|
|
2014-10-10 11:50:54 +02:00
|
|
|
// first message to be sent, so should not be cloned
|
2014-07-30 11:28:25 +02:00
|
|
|
n2.on('input',function(msg) {
|
2014-10-10 11:50:54 +02:00
|
|
|
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);
|
2015-02-02 14:21:19 +01:00
|
|
|
rcvdCount += 1;
|
|
|
|
if (rcvdCount == 2) {
|
|
|
|
done();
|
|
|
|
}
|
2014-10-10 11:50:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// second message to be sent, so should be cloned
|
2015-02-02 14:21:19 +01:00
|
|
|
// message uuids wont match since we've cloned
|
2014-10-10 11:50:54 +02:00
|
|
|
n3.on('input',function(msg) {
|
2015-02-02 14:21:19 +01:00
|
|
|
msg.payload.should.equal(message.payload);
|
2014-07-30 11:28:25 +02:00
|
|
|
msg.cloned.should.not.be.exactly(message.cloned);
|
|
|
|
msg.req.should.be.exactly(message.req);
|
|
|
|
msg.res.should.be.exactly(message.res);
|
2015-02-02 14:21:19 +01:00
|
|
|
rcvdCount += 1;
|
|
|
|
if (rcvdCount == 2) {
|
|
|
|
done();
|
|
|
|
}
|
2014-07-30 11:28:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
n1.send(message);
|
|
|
|
});
|
|
|
|
|
2015-02-03 23:02:26 +01:00
|
|
|
it("logs the uuid for all messages sent", function(done) {
|
|
|
|
var logHandler = {
|
2019-07-09 00:23:33 +02:00
|
|
|
msgIds:[],
|
2015-02-03 23:02:26 +01:00
|
|
|
messagesSent: 0,
|
|
|
|
emit: function(event, msg) {
|
|
|
|
if (msg.event == "node.abc.send" && msg.level == Log.METRIC) {
|
|
|
|
this.messagesSent++;
|
2019-07-09 00:23:33 +02:00
|
|
|
this.msgIds.push(msg.msgid);
|
2015-02-03 23:02:26 +01:00
|
|
|
(typeof msg.msgid).should.not.be.equal("undefined");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Log.addHandler(logHandler);
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
getNode: (id) => { return {'n1':sender,'n2':receiver1,'n3':receiver2}[id]},
|
|
|
|
};
|
2015-02-03 23:02:26 +01:00
|
|
|
|
2019-01-16 17:27:19 +01:00
|
|
|
var sender = new RedNode({_flow:flow,id:'n1',type:'abc', wires:[['n2', 'n3']]});
|
|
|
|
var receiver1 = new RedNode({_flow:flow,id:'n2',type:'abc'});
|
|
|
|
var receiver2 = new RedNode({_flow:flow,id:'n3',type:'abc'});
|
2015-02-03 23:02:26 +01:00
|
|
|
sender.send({"some": "message"});
|
2019-07-09 00:23:33 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
try {
|
|
|
|
logHandler.messagesSent.should.equal(1);
|
|
|
|
should.exist(logHandler.msgIds[0])
|
|
|
|
Log.removeHandler(logHandler);
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
Log.removeHandler(logHandler);
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
},50)
|
2015-02-03 23:02:26 +01:00
|
|
|
})
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2014-07-09 08:42:09 +02:00
|
|
|
|
2015-01-27 15:41:20 +01:00
|
|
|
|
2014-07-09 08:42:09 +02:00
|
|
|
describe('#log', function() {
|
2015-01-27 15:41:20 +01:00
|
|
|
it('produces a log message', function(done) {
|
2018-04-18 00:29:56 +02:00
|
|
|
var n = new RedNode({id:'123',type:'abc',z:'789'});
|
2015-01-27 15:41:20 +01:00
|
|
|
var loginfo = {};
|
|
|
|
sinon.stub(Log, 'log', function(msg) {
|
|
|
|
loginfo = msg;
|
2014-07-09 08:42:09 +02:00
|
|
|
});
|
|
|
|
n.log("a log message");
|
2015-02-03 23:02:26 +01:00
|
|
|
should.deepEqual({level:Log.INFO, id:n.id,
|
2018-04-18 11:28:51 +02:00
|
|
|
type:n.type, msg:"a log message",z:'789'}, loginfo);
|
2015-01-27 15:41:20 +01:00
|
|
|
Log.log.restore();
|
|
|
|
done();
|
2014-07-09 08:42:09 +02:00
|
|
|
});
|
2015-01-27 15:41:20 +01:00
|
|
|
it('produces a log message with a name', function(done) {
|
2018-04-18 00:29:56 +02:00
|
|
|
var n = new RedNode({id:'123', type:'abc', name:"barney", z:'789'});
|
2015-01-27 15:41:20 +01:00
|
|
|
var loginfo = {};
|
|
|
|
sinon.stub(Log, 'log', function(msg) {
|
|
|
|
loginfo = msg;
|
2014-07-09 08:42:09 +02:00
|
|
|
});
|
|
|
|
n.log("a log message");
|
2015-02-03 23:02:26 +01:00
|
|
|
should.deepEqual({level:Log.INFO, id:n.id, name: "barney",
|
2018-04-18 11:28:51 +02:00
|
|
|
type:n.type, msg:"a log message",z:'789'}, loginfo);
|
2015-01-27 15:41:20 +01:00
|
|
|
Log.log.restore();
|
|
|
|
done();
|
2014-07-09 08:42:09 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#warn', function() {
|
2015-01-27 15:41:20 +01:00
|
|
|
it('produces a warning message', function(done) {
|
2018-04-18 00:29:56 +02:00
|
|
|
var n = new RedNode({id:'123',type:'abc',z:'789'});
|
2015-01-27 15:41:20 +01:00
|
|
|
var loginfo = {};
|
|
|
|
sinon.stub(Log, 'log', function(msg) {
|
|
|
|
loginfo = msg;
|
2014-07-09 08:42:09 +02:00
|
|
|
});
|
|
|
|
n.warn("a warning");
|
2015-02-03 23:02:26 +01:00
|
|
|
should.deepEqual({level:Log.WARN, id:n.id,
|
2018-04-18 11:28:51 +02:00
|
|
|
type:n.type, msg:"a warning",z:'789'}, loginfo);
|
2015-01-27 15:41:20 +01:00
|
|
|
Log.log.restore();
|
|
|
|
done();
|
2014-07-09 08:42:09 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#error', function() {
|
2015-03-08 16:26:24 +01:00
|
|
|
it('handles a null error message', function(done) {
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
handleError: sinon.stub()
|
|
|
|
}
|
|
|
|
var n = new RedNode({_flow:flow, id:'123',type:'abc',z:'789'});
|
2015-02-20 20:54:19 +01:00
|
|
|
var message = {a:1};
|
2015-03-06 23:58:30 +01:00
|
|
|
n.error(null,message);
|
|
|
|
|
2019-01-16 17:27:19 +01:00
|
|
|
flow.handleError.called.should.be.true();
|
|
|
|
flow.handleError.args[0][0].should.eql(n);
|
|
|
|
flow.handleError.args[0][1].should.eql("");
|
|
|
|
flow.handleError.args[0][2].should.eql(message);
|
2015-03-08 16:26:24 +01:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('produces an error message', function(done) {
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
handleError: sinon.stub()
|
|
|
|
}
|
|
|
|
var n = new RedNode({_flow:flow, id:'123',type:'abc',z:'789'});
|
2015-03-08 16:26:24 +01:00
|
|
|
var message = {a:2};
|
|
|
|
|
|
|
|
n.error("This is an error",message);
|
|
|
|
|
2019-01-16 17:27:19 +01:00
|
|
|
flow.handleError.called.should.be.true();
|
|
|
|
flow.handleError.args[0][0].should.eql(n);
|
|
|
|
flow.handleError.args[0][1].should.eql("This is an error");
|
|
|
|
flow.handleError.args[0][2].should.eql(message);
|
2015-03-08 16:26:24 +01:00
|
|
|
|
2015-01-27 15:41:20 +01:00
|
|
|
done();
|
2014-07-09 08:42:09 +02:00
|
|
|
});
|
2015-03-08 16:26:24 +01:00
|
|
|
|
2014-07-09 08:42:09 +02:00
|
|
|
});
|
|
|
|
|
2015-01-27 15:41:20 +01:00
|
|
|
describe('#metric', function() {
|
|
|
|
it('produces a metric message', function(done) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
var loginfo = {};
|
|
|
|
sinon.stub(Log, 'log', function(msg) {
|
|
|
|
loginfo = msg;
|
|
|
|
});
|
2015-02-06 17:36:32 +01:00
|
|
|
var msg = {payload:"foo", _msgid:"987654321"};
|
2015-02-03 14:51:05 +01:00
|
|
|
n.metric("test.metric",msg,"15mb");
|
2015-02-04 23:28:17 +01:00
|
|
|
should.deepEqual({value:"15mb", level:Log.METRIC, nodeid:n.id,
|
2015-02-03 23:02:26 +01:00
|
|
|
event:"node.abc.test.metric",msgid:"987654321"}, loginfo);
|
2015-01-27 15:41:20 +01:00
|
|
|
Log.log.restore();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-06 23:58:30 +01:00
|
|
|
|
|
|
|
describe('#metric', function() {
|
|
|
|
it('returns metric value if eventname undefined', function(done) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
var loginfo = {};
|
|
|
|
sinon.stub(Log, 'log', function(msg) {
|
|
|
|
loginfo = msg;
|
|
|
|
});
|
|
|
|
var msg = {payload:"foo", _msgid:"987654321"};
|
|
|
|
var m = n.metric(undefined,msg,"15mb");
|
2016-10-10 14:27:43 +02:00
|
|
|
m.should.be.a.Boolean();
|
2015-03-06 23:58:30 +01:00
|
|
|
Log.log.restore();
|
|
|
|
done();
|
|
|
|
});
|
2015-03-08 17:53:48 +01:00
|
|
|
it('returns not defined if eventname defined', function(done) {
|
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
|
|
|
var loginfo = {};
|
|
|
|
sinon.stub(Log, 'log', function(msg) {
|
|
|
|
loginfo = msg;
|
|
|
|
});
|
|
|
|
var msg = {payload:"foo", _msgid:"987654321"};
|
|
|
|
var m = n.metric("info",msg,"15mb");
|
|
|
|
should(m).be.undefined;
|
|
|
|
Log.log.restore();
|
|
|
|
done();
|
|
|
|
});
|
2015-03-06 23:58:30 +01:00
|
|
|
});
|
|
|
|
|
2014-07-30 11:39:48 +02:00
|
|
|
describe('#status', function() {
|
|
|
|
it('publishes status', function(done) {
|
2019-01-16 17:27:19 +01:00
|
|
|
var flow = {
|
|
|
|
handleStatus: sinon.stub()
|
|
|
|
}
|
|
|
|
var n = new RedNode({_flow:flow,id:'123',type:'abc'});
|
2014-07-30 11:39:48 +02:00
|
|
|
var status = {fill:"green",shape:"dot",text:"connected"};
|
|
|
|
|
|
|
|
n.status(status);
|
2015-08-19 22:35:54 +02:00
|
|
|
|
2019-01-16 17:27:19 +01:00
|
|
|
flow.handleStatus.called.should.be.true();
|
|
|
|
flow.handleStatus.args[0][0].should.eql(n);
|
|
|
|
flow.handleStatus.args[0][1].should.eql(status);
|
2015-08-19 22:35:54 +02:00
|
|
|
done();
|
2014-07-30 11:39:48 +02:00
|
|
|
});
|
2019-10-14 14:06:59 +02:00
|
|
|
it('publishes status for plain string', function(done) {
|
|
|
|
var flow = { handleStatus: sinon.stub() }
|
|
|
|
var n = new RedNode({_flow:flow,id:'123',type:'abc'});
|
|
|
|
n.status("text status");
|
|
|
|
flow.handleStatus.called.should.be.true();
|
|
|
|
flow.handleStatus.args[0][0].should.eql(n);
|
|
|
|
flow.handleStatus.args[0][1].should.eql({text:"text status"});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
it('publishes status for plain boolean', function(done) {
|
|
|
|
var flow = { handleStatus: sinon.stub() }
|
|
|
|
var n = new RedNode({_flow:flow,id:'123',type:'abc'});
|
|
|
|
n.status(false);
|
|
|
|
flow.handleStatus.called.should.be.true();
|
|
|
|
flow.handleStatus.args[0][0].should.eql(n);
|
|
|
|
flow.handleStatus.args[0][1].should.eql({text:"false"});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
it('publishes status for plain number', function(done) {
|
|
|
|
var flow = { handleStatus: sinon.stub() }
|
|
|
|
var n = new RedNode({_flow:flow,id:'123',type:'abc'});
|
|
|
|
n.status(123);
|
|
|
|
flow.handleStatus.called.should.be.true();
|
|
|
|
flow.handleStatus.args[0][0].should.eql(n);
|
|
|
|
flow.handleStatus.args[0][1].should.eql({text:"123"});
|
|
|
|
done();
|
|
|
|
});
|
2014-07-30 11:39:48 +02:00
|
|
|
});
|
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|