1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add comms.stop to tidy up keepalive timer

This commit is contained in:
Nick O'Leary 2014-08-01 21:55:05 +01:00
parent 6c464ed3a4
commit 8e8e13a3a2
4 changed files with 112 additions and 90 deletions

View File

@ -82,6 +82,11 @@ function start() {
}, webSocketKeepAliveTime); }, webSocketKeepAliveTime);
} }
function stop() {
clearInterval(heartbeatTimer);
wsServer.close();
}
function publish(topic,data,retain) { function publish(topic,data,retain) {
if (retain) { if (retain) {
retained[topic] = data; retained[topic] = data;
@ -116,5 +121,6 @@ function handleRemoteSubscription(ws,topic) {
module.exports = { module.exports = {
init:init, init:init,
start:start, start:start,
stop:stop,
publish:publish, publish:publish,
} }

View File

@ -98,6 +98,7 @@ function start() {
function stop() { function stop() {
redNodes.stopFlows(); redNodes.stopFlows();
comms.stop();
} }
module.exports = { module.exports = {

View File

@ -25,106 +25,115 @@ var address = '127.0.0.1';
var listenPort = 0; // use ephemeral port var listenPort = 0; // use ephemeral port
describe("comms", function() { describe("comms", function() {
var server; describe("with default keepalive", function() {
var url; var server;
var port; var url;
before(function(done) { var port;
server = http.createServer(function(req,res){app(req,res)}); before(function(done) {
comms.init(server, {}); server = http.createServer(function(req,res){app(req,res)});
server.listen(listenPort, address); comms.init(server, {});
server.on('listening', function() { server.listen(listenPort, address);
port = server.address().port; server.on('listening', function() {
url = 'http://' + address + ':' + port + '/comms'; port = server.address().port;
comms.start(); url = 'http://' + address + ':' + port + '/comms';
done(); comms.start();
done();
});
}); });
});
after(function() {
it('accepts connection', function(done) { comms.stop();
var ws = new WebSocket(url);
ws.on('open', function() {
ws.close();
done();
}); });
});
it('accepts connection', function(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); var ws = new WebSocket(url);
ws.on('open', function() { 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(); ws.close();
done(); 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() { describe("keep alives", function() {
var server;
var url;
var port;
before(function(done) { before(function(done) {
server = http.createServer(function(req,res){app(req,res)}); server = http.createServer(function(req,res){app(req,res)});
comms.init(server, {webSocketKeepAliveTime: 100}); comms.init(server, {webSocketKeepAliveTime: 100});
@ -136,6 +145,9 @@ describe("comms", function() {
done(); done();
}); });
}); });
after(function() {
comms.stop();
});
it('are sent', function(done) { it('are sent', function(done) {
var ws = new WebSocket(url); var ws = new WebSocket(url);
var count = 0; var count = 0;

View File

@ -277,6 +277,9 @@ describe('Node', function() {
}); });
describe('#status', function() { describe('#status', function() {
after(function() {
comms.publish.restore();
});
it('publishes status', function(done) { it('publishes status', function(done) {
var n = new RedNode({id:'123',type:'abc'}); var n = new RedNode({id:'123',type:'abc'});
var status = {fill:"green",shape:"dot",text:"connected"}; var status = {fill:"green",shape:"dot",text:"connected"};