Move comms from runtime to api component

This commit is contained in:
Nick O'Leary
2015-11-12 09:03:03 +00:00
parent 9f5e6a4b37
commit 8fb955e182
11 changed files with 145 additions and 109 deletions

View File

@@ -23,21 +23,26 @@ var express = require('express');
var app = express();
var WebSocket = require('ws');
var comms = require("../../../red/runtime/comms");
var comms = require("../../../red/api/comms");
var Users = require("../../../red/api/auth/users");
var Tokens = require("../../../red/api/auth/tokens");
var address = '127.0.0.1';
var listenPort = 0; // use ephemeral port
describe("runtime/comms", function() {
describe("api/comms", function() {
describe("with default keepalive", function() {
var server;
var url;
var port;
before(function(done) {
sinon.stub(Users,"default",function() { return when.resolve(null);});
server = http.createServer(function(req,res){app(req,res)});
comms.init(server, {});
comms.init(server, {
settings:{},
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
events:{on:function(){},removeListener:function(){}}
});
server.listen(listenPort, address);
server.on('listening', function() {
port = server.address().port;
@@ -48,6 +53,7 @@ describe("runtime/comms", function() {
});
after(function() {
Users.default.restore();
comms.stop();
});
@@ -119,29 +125,34 @@ describe("runtime/comms", function() {
// implementation. More test should be written to test topic
// matching once this one is passing
if (0) {
it('receives message on correct topic', function(done) {
var ws = new WebSocket(url);
ws.on('open', function() {
ws.send('{"subscribe":"topic4"}');
comms.publish('topic5', 'foo');
comms.publish('topic4', 'bar');
});
ws.on('message', function(msg) {
msg.should.equal('{"topic":"topic4","data":"bar"}');
ws.close();
done();
});
it.skip('receives message on correct topic', function(done) {
var ws = new WebSocket(url);
ws.on('open', function() {
ws.send('{"subscribe":"topic4"}');
comms.publish('topic5', 'foo');
comms.publish('topic4', 'bar');
});
}
ws.on('message', function(msg) {
msg.should.equal('{"topic":"topic4","data":"bar"}');
ws.close();
done();
});
});
it.skip('listens for node/status events');
});
describe("disabled editor", function() {
var server;
var url;
var port;
before(function(done) {
sinon.stub(Users,"default",function() { return when.resolve(null);});
server = http.createServer(function(req,res){app(req,res)});
comms.init(server, {disableEditor:true});
comms.init(server, {
settings:{disableEditor:true},
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
events:{on:function(){},removeListener:function(){}}
});
server.listen(listenPort, address);
server.on('listening', function() {
port = server.address().port;
@@ -152,6 +163,7 @@ describe("runtime/comms", function() {
});
after(function() {
Users.default.restore();
comms.stop();
});
@@ -173,8 +185,13 @@ describe("runtime/comms", function() {
var url;
var port;
before(function(done) {
sinon.stub(Users,"default",function() { return when.resolve(null);});
server = http.createServer(function(req,res){app(req,res)});
comms.init(server, {httpAdminRoot:"/adminPath"});
comms.init(server, {
settings:{httpAdminRoot:"/adminPath"},
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
events:{on:function(){},removeListener:function(){}}
});
server.listen(listenPort, address);
server.on('listening', function() {
port = server.address().port;
@@ -185,6 +202,7 @@ describe("runtime/comms", function() {
});
after(function() {
Users.default.restore();
comms.stop();
});
@@ -206,8 +224,13 @@ describe("runtime/comms", function() {
var url;
var port;
before(function(done) {
sinon.stub(Users,"default",function() { return when.resolve(null);});
server = http.createServer(function(req,res){app(req,res)});
comms.init(server, {httpAdminRoot:"/adminPath/"});
comms.init(server,{
settings:{httpAdminRoot:"/adminPath"},
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
events:{on:function(){},removeListener:function(){}}
});
server.listen(listenPort, address);
server.on('listening', function() {
port = server.address().port;
@@ -218,6 +241,7 @@ describe("runtime/comms", function() {
});
after(function() {
Users.default.restore();
comms.stop();
});
@@ -239,8 +263,13 @@ describe("runtime/comms", function() {
var url;
var port;
before(function(done) {
sinon.stub(Users,"default",function() { return when.resolve(null);});
server = http.createServer(function(req,res){app(req,res)});
comms.init(server, {httpAdminRoot:"adminPath"});
comms.init(server, {
settings:{httpAdminRoot:"adminPath"},
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
events:{on:function(){},removeListener:function(){}}
});
server.listen(listenPort, address);
server.on('listening', function() {
port = server.address().port;
@@ -251,6 +280,7 @@ describe("runtime/comms", function() {
});
after(function() {
Users.default.restore();
comms.stop();
});
@@ -272,8 +302,13 @@ describe("runtime/comms", function() {
var url;
var port;
before(function(done) {
sinon.stub(Users,"default",function() { return when.resolve(null);});
server = http.createServer(function(req,res){app(req,res)});
comms.init(server, {webSocketKeepAliveTime: 100});
comms.init(server, {
settings:{webSocketKeepAliveTime: 100},
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
events:{on:function(){},removeListener:function(){}}
});
server.listen(listenPort, address);
server.on('listening', function() {
port = server.address().port;
@@ -283,6 +318,7 @@ describe("runtime/comms", function() {
});
});
after(function() {
Users.default.restore();
comms.stop();
});
it('are sent', function(done) {
@@ -354,7 +390,11 @@ describe("runtime/comms", function() {
server = http.createServer(function(req,res){app(req,res)});
comms.init(server, {adminAuth:{}});
comms.init(server,{
settings:{adminAuth:{}},
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
events:{on:function(){},removeListener:function(){}}
});
server.listen(listenPort, address);
server.on('listening', function() {
port = server.address().port;
@@ -440,7 +480,11 @@ describe("runtime/comms", function() {
before(function(done) {
getDefaultUser = sinon.stub(Users,"default",function() { return when.resolve({permissions:"read"});});
server = http.createServer(function(req,res){app(req,res)});
comms.init(server, {adminAuth:{}});
comms.init(server, {
settings:{adminAuth:{}},
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
events:{on:function(){},removeListener:function(){}}
});
server.listen(listenPort, address);
server.on('listening', function() {
port = server.address().port;

View File

@@ -28,7 +28,7 @@ describe("api index", function() {
describe("disables editor", function() {
before(function() {
api.init({
api.init({},{
settings:{httpNodeRoot:true, httpAdminRoot: true,disableEditor:true},
events: {on:function(){},removeListener: function(){}}
});
@@ -67,9 +67,10 @@ describe("api index", function() {
})
});
before(function() {
api.init({
api.init({},{
settings:{httpNodeRoot:true, httpAdminRoot: true, adminAuth:{type: "credentials",users:[],default:{permissions:"read"}}},
storage:{getSessions:function(){return when.resolve({})}}
storage:{getSessions:function(){return when.resolve({})}},
events:{on:function(){},removeListener:function(){}}
});
app = api.adminApp();
});
@@ -103,7 +104,7 @@ describe("api index", function() {
});
before(function() {
api.init({
api.init({},{
log:{audit:function(){}},
settings:{httpNodeRoot:true, httpAdminRoot: true,disableEditor:false},
events:{on:function(){},removeListener:function(){}}

View File

@@ -21,19 +21,12 @@ var path = require("path");
var api = require("../../../red/api");
var runtime = require("../../../red/runtime");
var comms = require("../../../red/runtime/comms");
var redNodes = require("../../../red/runtime/nodes");
var storage = require("../../../red/runtime/storage");
var settings = require("../../../red/runtime/settings");
var log = require("../../../red/runtime/log");
describe("runtime", function() {
var commsMessages = [];
var commsPublish;
beforeEach(function() {
commsMessages = [];
});
afterEach(function() {
if (console.log.restore) {
console.log.restore();
@@ -41,13 +34,9 @@ describe("runtime", function() {
})
before(function() {
commsPublish = sinon.stub(comms,"publish", function(topic,msg,retained) {
commsMessages.push({topic:topic,msg:msg,retained:retained});
});
process.env.NODE_RED_HOME = path.resolve(path.join(__dirname,"..","..",".."))
});
after(function() {
commsPublish.restore();
delete process.env.NODE_RED_HOME;
});
@@ -55,32 +44,26 @@ describe("runtime", function() {
beforeEach(function() {
sinon.stub(log,"init",function() {});
sinon.stub(settings,"init",function() {});
sinon.stub(comms,"init",function() {});
});
afterEach(function() {
log.init.restore();
settings.init.restore();
comms.init.restore();
})
it("initialises components", function() {
var dummyServer = {};
runtime.init(dummyServer,{testSettings: true, httpAdminRoot:"/"});
runtime.init({testSettings: true, httpAdminRoot:"/"});
log.init.called.should.be.true;
settings.init.called.should.be.true;
comms.init.called.should.be.true;
});
it("returns version", function() {
var dummyServer = {};
runtime.init(dummyServer,{testSettings: true, httpAdminRoot:"/"});
runtime.init({testSettings: true, httpAdminRoot:"/"});
/^\d+\.\d+\.\d+(-git)?$/.test(runtime.version()).should.be.true;
})
});
describe("start",function() {
var commsInit;
var storageInit;
var settingsLoad;
var logMetric;
@@ -93,10 +76,8 @@ describe("runtime", function() {
var redNodesGetNodeList;
var redNodesLoadFlows;
var redNodesStartFlows;
var commsStart;
beforeEach(function() {
commsInit = sinon.stub(comms,"init",function() {});
storageInit = sinon.stub(storage,"init",function(settings) {return when.resolve();});
logMetric = sinon.stub(log,"metric",function() { return false; });
logWarn = sinon.stub(log,"warn",function() { });
@@ -107,10 +88,8 @@ describe("runtime", function() {
redNodesCleanModuleList = sinon.stub(redNodes,"cleanModuleList",function(){});
redNodesLoadFlows = sinon.stub(redNodes,"loadFlows",function() {return when.resolve()});
redNodesStartFlows = sinon.stub(redNodes,"startFlows",function() {});
commsStart = sinon.stub(comms,"start",function(){});
});
afterEach(function() {
commsInit.restore();
storageInit.restore();
logMetric.restore();
logWarn.restore();
@@ -122,7 +101,6 @@ describe("runtime", function() {
redNodesCleanModuleList.restore();
redNodesLoadFlows.restore();
redNodesStartFlows.restore();
commsStart.restore();
});
it("reports errored/missing modules",function(done) {
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
@@ -131,7 +109,7 @@ describe("runtime", function() {
{ module:"module",enabled:true,loaded:false,types:["typeA","typeB"]} // missing
].filter(cb);
});
runtime.init({},{testSettings: true, httpAdminRoot:"/", load:function() { return when.resolve();}});
runtime.init({testSettings: true, httpAdminRoot:"/", load:function() { return when.resolve();}});
sinon.stub(console,"log");
runtime.start().then(function() {
console.log.restore();
@@ -139,7 +117,6 @@ describe("runtime", function() {
storageInit.calledOnce.should.be.true;
redNodesInit.calledOnce.should.be.true;
redNodesLoad.calledOnce.should.be.true;
commsStart.calledOnce.should.be.true;
redNodesLoadFlows.calledOnce.should.be.true;
logWarn.calledWithMatch("Failed to register 1 node type");
@@ -162,7 +139,7 @@ describe("runtime", function() {
].filter(cb);
});
var serverInstallModule = sinon.stub(redNodes,"installModule",function(name) { return when.resolve();});
runtime.init({},{testSettings: true, autoInstallModules:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
runtime.init({testSettings: true, autoInstallModules:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
sinon.stub(console,"log");
runtime.start().then(function() {
console.log.restore();
@@ -188,7 +165,7 @@ describe("runtime", function() {
{ err:"errored",name:"errName" } // error
].filter(cb);
});
runtime.init({},{testSettings: true, verbose:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
runtime.init({testSettings: true, verbose:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
sinon.stub(console,"log");
runtime.start().then(function() {
console.log.restore();
@@ -203,12 +180,11 @@ describe("runtime", function() {
});
it("reports runtime metrics",function(done) {
var commsStop = sinon.stub(comms,"stop",function() {} );
var stopFlows = sinon.stub(redNodes,"stopFlows",function() {} );
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function() {return []});
logMetric.restore();
logMetric = sinon.stub(log,"metric",function() { return true; });
runtime.init({},{testSettings: true, runtimeMetricInterval:200, httpAdminRoot:"/", load:function() { return when.resolve();}});
runtime.init({testSettings: true, runtimeMetricInterval:200, httpAdminRoot:"/", load:function() { return when.resolve();}});
sinon.stub(console,"log");
runtime.start().then(function() {
console.log.restore();
@@ -226,7 +202,6 @@ describe("runtime", function() {
done(err);
} finally {
runtime.stop();
commsStop.restore();
stopFlows.restore();
}
},300);
@@ -237,15 +212,9 @@ describe("runtime", function() {
});
it("stops components", function() {
var commsStop = sinon.stub(comms,"stop",function() {} );
var stopFlows = sinon.stub(redNodes,"stopFlows",function() {} );
runtime.stop();
commsStop.called.should.be.true;
stopFlows.called.should.be.true;
commsStop.restore();
stopFlows.restore();
});
});

View File

@@ -19,7 +19,6 @@ var sinon = require('sinon');
var RedNode = require("../../../../red/runtime/nodes/Node");
var Log = require("../../../../red/runtime/log");
var flows = require("../../../../red/runtime/nodes/flows");
var comms = require("../../../../red/runtime/comms");
describe('Node', function() {
describe('#constructor',function() {
@@ -512,9 +511,6 @@ describe('Node', function() {
});
describe('#status', function() {
after(function() {
comms.publish.restore();
});
it('publishes status', function(done) {
sinon.stub(flows,"handleStatus", function(node,message,msg) {});
var n = new RedNode({id:'123',type:'abc'});
@@ -522,18 +518,9 @@ describe('Node', function() {
var topic;
var message;
var retain;
sinon.stub(comms, 'publish', function(_topic, _message, _retain) {
topic = _topic;
message = _message;
retain = _retain;
});
n.status(status);
topic.should.equal('status/123');
message.should.equal(status);
retain.should.be.true;
flows.handleStatus.called.should.be.true;
flows.handleStatus.args[0][0].should.eql(n);
flows.handleStatus.args[0][1].should.eql(status);