From a8c491bf2b1df751f759466d01bb65b3d9c8928d Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Wed, 30 Jul 2014 13:58:11 +0100 Subject: [PATCH] Add comms keep alive tests. --- test/red/comms_spec.js | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test/red/comms_spec.js b/test/red/comms_spec.js index f83a7839a..65aa25ad0 100644 --- a/test/red/comms_spec.js +++ b/test/red/comms_spec.js @@ -123,4 +123,55 @@ describe("comms", function() { }); }); } + + describe("keep alives", function() { + before(function(done) { + server = http.createServer(function(req,res){app(req,res)}); + comms.init(server, {webSocketKeepAliveTime: 100}); + server.listen(listenPort, address); + server.on('listening', function() { + port = server.address().port; + url = 'http://' + address + ':' + port + '/comms'; + comms.start(); + done(); + }); + }); + it('are sent', function(done) { + var ws = new WebSocket(url); + var count = 0; + ws.on('message', function(data) { + var msg = JSON.parse(data); + msg.should.have.property('topic','hb'); + msg.should.have.property('data').be.a.Number; + count++; + if (count == 3) { + ws.close(); + done(); + } + }); + }); + it('are not sent if other messages are sent', function(done) { + var ws = new WebSocket(url); + var count = 0; + var interval; + ws.on('open', function() { + ws.send('{"subscribe":"foo"}'); + interval = setInterval(function() { + comms.publish('foo', 'bar'); + }, 50); + }); + ws.on('message', function(data) { + var msg = JSON.parse(data); + msg.should.have.property('topic', 'foo'); + msg.should.have.property('data', 'bar'); + count++; + if (count == 5) { + clearInterval(interval); + ws.close(); + done(); + } + }); + }); + }); + });