Add message router component

This commit is contained in:
Nick O'Leary
2016-10-29 21:46:19 +01:00
parent 1fd87bf664
commit cebddc0237
6 changed files with 223 additions and 128 deletions

View File

@@ -149,11 +149,18 @@ describe('Node', function() {
});
describe('#send', function() {
var flowGet;
afterEach(function() {
if (flowGet && flowGet.restore) {
flowGet.restore();
flowGet = null;
}
});
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 flowGet = sinon.stub(flows,"get",function(id) {
flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
var message = {payload:"hello world"};
@@ -172,7 +179,7 @@ describe('Node', function() {
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 flowGet = sinon.stub(flows,"get",function(id) {
flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
@@ -206,7 +213,7 @@ describe('Node', function() {
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 flowGet = sinon.stub(flows,"get",function(id) {
flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2,'n3':n3,'n4':n4,'n5':n5}[id];
});
@@ -264,7 +271,7 @@ describe('Node', function() {
it('emits no messages', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
var n2 = new RedNode({id:'n2',type:'abc'});
var flowGet = sinon.stub(flows,"get",function(id) {
flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
@@ -283,7 +290,7 @@ describe('Node', function() {
it('emits messages ignoring non-existent nodes', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n9'],['n2']]});
var n2 = new RedNode({id:'n2',type:'abc'});
var flowGet = sinon.stub(flows,"get",function(id) {
flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
@@ -307,7 +314,7 @@ describe('Node', function() {
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 flowGet = sinon.stub(flows,"get",function(id) {
flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2,'n3':n3}[id];
});
@@ -349,7 +356,7 @@ describe('Node', function() {
});
it("logs the uuid for all messages sent", function(done) {
var flowGet = sinon.stub(flows,"get",function(id) {
flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':sender,'n2':receiver1,'n3':receiver2}[id];
});
var logHandler = {

View File

@@ -0,0 +1,79 @@
/**
* Copyright 2016 IBM Corp.
*
* 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.
**/
var should = require("should");
var sinon = require('sinon');
var RedNode = require("../../../../../red/runtime/nodes/Node");
var router = require("../../../../../red/runtime/nodes/router");
var flows = require("../../../../../red/runtime/nodes/flows");
describe('Router', function() {
var flowGet;
afterEach(function() {
if (flowGet && flowGet.restore) {
flowGet.restore();
flowGet = null;
}
})
describe('#add',function() {
it('adds a route for a node', function(done) {
var senderNode = {id:'123',metric:function(){}};
var receiver = sinon.stub();
flowGet = sinon.stub(flows,"get",function(id) {
if (id === '456') {
return {receive:receiver};
}
return null;
});
router.send(senderNode,{});
flowGet.called.should.be.false();
router.add(senderNode,[['456']]);
router.send(senderNode,{});
flowGet.called.should.be.true();
receiver.called.should.be.true();
done();
});
})
describe('#remove',function() {
it('removes a route for a node', function(done) {
var senderNode = {id:'123',metric:function(){}};
var receiver = sinon.stub();
flowGet = sinon.stub(flows,"get",function(id) {
if (id === '456') {
return {receive:receiver};
}
return null;
});
router.add(senderNode,[['456']]);
router.send(senderNode,{});
flowGet.called.should.be.true();
receiver.called.should.be.true();
flowGet.reset();
receiver.reset();
router.remove(senderNode);
router.send(senderNode,{});
flowGet.called.should.be.false();
done();
});
})
})