From 8e8e13a3a2f928339c629234e3fbad30a18bb4e0 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 1 Aug 2014 21:55:05 +0100 Subject: [PATCH] Add comms.stop to tidy up keepalive timer --- red/comms.js | 6 ++ red/server.js | 1 + test/red/comms_spec.js | 192 +++++++++++++++++++----------------- test/red/nodes/Node_spec.js | 3 + 4 files changed, 112 insertions(+), 90 deletions(-) diff --git a/red/comms.js b/red/comms.js index 06b21ce37..519c35d9c 100644 --- a/red/comms.js +++ b/red/comms.js @@ -82,6 +82,11 @@ function start() { }, webSocketKeepAliveTime); } +function stop() { + clearInterval(heartbeatTimer); + wsServer.close(); +} + function publish(topic,data,retain) { if (retain) { retained[topic] = data; @@ -116,5 +121,6 @@ function handleRemoteSubscription(ws,topic) { module.exports = { init:init, start:start, + stop:stop, publish:publish, } diff --git a/red/server.js b/red/server.js index 9e538a334..79f3e39f7 100644 --- a/red/server.js +++ b/red/server.js @@ -98,6 +98,7 @@ function start() { function stop() { redNodes.stopFlows(); + comms.stop(); } module.exports = { diff --git a/test/red/comms_spec.js b/test/red/comms_spec.js index 65aa25ad0..dce4d83af 100644 --- a/test/red/comms_spec.js +++ b/test/red/comms_spec.js @@ -25,106 +25,115 @@ var address = '127.0.0.1'; var listenPort = 0; // use ephemeral port describe("comms", function() { - var server; - var url; - var port; - before(function(done) { - server = http.createServer(function(req,res){app(req,res)}); - comms.init(server, {}); - server.listen(listenPort, address); - server.on('listening', function() { - port = server.address().port; - url = 'http://' + address + ':' + port + '/comms'; - comms.start(); - done(); + describe("with default keepalive", function() { + var server; + var url; + var port; + before(function(done) { + server = http.createServer(function(req,res){app(req,res)}); + comms.init(server, {}); + server.listen(listenPort, address); + server.on('listening', function() { + port = server.address().port; + url = 'http://' + address + ':' + port + '/comms'; + comms.start(); + done(); + }); }); - }); - - it('accepts connection', function(done) { - var ws = new WebSocket(url); - ws.on('open', function() { - ws.close(); - done(); + + after(function() { + comms.stop(); }); - }); - - it('publishes message after subscription', function(done) { - var ws = new WebSocket(url); - ws.on('open', function() { - ws.send('{"subscribe":"topic1"}'); - comms.publish('topic1', 'foo'); - }); - ws.on('message', function(msg) { - msg.should.equal('{"topic":"topic1","data":"foo"}'); - ws.close(); - done(); - }); - }); - - it('publishes retained message for subscription', function(done) { - comms.publish('topic2', 'bar', true); - var ws = new WebSocket(url); - ws.on('open', function() { - ws.send('{"subscribe":"topic2"}'); - }); - ws.on('message', function(msg) { - msg.should.equal('{"topic":"topic2","data":"bar"}'); - ws.close(); - done(); - }); - }); - - it('retained message is deleted by non-retained message', function(done) { - comms.publish('topic3', 'retained', true); - comms.publish('topic3', 'non-retained'); - var ws = new WebSocket(url); - ws.on('open', function() { - ws.send('{"subscribe":"topic3"}'); - comms.publish('topic3', 'new'); - }); - ws.on('message', function(msg) { - msg.should.equal('{"topic":"topic3","data":"new"}'); - ws.close(); - done(); - }); - }); - - it('malformed messages are ignored',function(done) { - var ws = new WebSocket(url); - ws.on('open', function() { - ws.send('not json'); - ws.send('[]'); - ws.send('{"subscribe":"topic3"}'); - comms.publish('topic3', 'correct'); - }); - ws.on('message', function(msg) { - msg.should.equal('{"topic":"topic3","data":"correct"}'); - ws.close(); - done(); - }); - }); - - // The following test currently fails due to minimum viable - // 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) { + + it('accepts connection', 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('publishes message after subscription', function(done) { + var ws = new WebSocket(url); + ws.on('open', function() { + ws.send('{"subscribe":"topic1"}'); + comms.publish('topic1', 'foo'); + }); + ws.on('message', function(msg) { + msg.should.equal('{"topic":"topic1","data":"foo"}'); + ws.close(); + done(); + }); + }); + + it('publishes retained message for subscription', function(done) { + comms.publish('topic2', 'bar', true); + var ws = new WebSocket(url); + ws.on('open', function() { + ws.send('{"subscribe":"topic2"}'); + }); + ws.on('message', function(msg) { + msg.should.equal('{"topic":"topic2","data":"bar"}'); + ws.close(); + done(); + }); + }); + + it('retained message is deleted by non-retained message', function(done) { + comms.publish('topic3', 'retained', true); + comms.publish('topic3', 'non-retained'); + var ws = new WebSocket(url); + ws.on('open', function() { + ws.send('{"subscribe":"topic3"}'); + comms.publish('topic3', 'new'); + }); + ws.on('message', function(msg) { + msg.should.equal('{"topic":"topic3","data":"new"}'); + ws.close(); + done(); + }); + }); + + it('malformed messages are ignored',function(done) { + var ws = new WebSocket(url); + ws.on('open', function() { + ws.send('not json'); + ws.send('[]'); + ws.send('{"subscribe":"topic3"}'); + comms.publish('topic3', 'correct'); + }); + ws.on('message', function(msg) { + msg.should.equal('{"topic":"topic3","data":"correct"}'); + ws.close(); + done(); + }); + }); + + // The following test currently fails due to minimum viable + // 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(); + }); + }); + } + }); describe("keep alives", function() { + var server; + var url; + var port; before(function(done) { server = http.createServer(function(req,res){app(req,res)}); comms.init(server, {webSocketKeepAliveTime: 100}); @@ -136,6 +145,9 @@ describe("comms", function() { done(); }); }); + after(function() { + comms.stop(); + }); it('are sent', function(done) { var ws = new WebSocket(url); var count = 0; diff --git a/test/red/nodes/Node_spec.js b/test/red/nodes/Node_spec.js index 0445a9d3b..6ac54bd2d 100644 --- a/test/red/nodes/Node_spec.js +++ b/test/red/nodes/Node_spec.js @@ -277,6 +277,9 @@ describe('Node', function() { }); describe('#status', function() { + after(function() { + comms.publish.restore(); + }); it('publishes status', function(done) { var n = new RedNode({id:'123',type:'abc'}); var status = {fill:"green",shape:"dot",text:"connected"};