mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Merge pull request #313 from hindessm/more-comms-tests
More comms tests
This commit is contained in:
		@@ -35,6 +35,8 @@ function init(_server,_settings) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function start() {
 | 
			
		||||
 | 
			
		||||
    var webSocketKeepAliveTime = settings.webSocketKeepAliveTime || 15000;
 | 
			
		||||
    var path = settings.httpAdminRoot || "/";
 | 
			
		||||
    path = path + (path.slice(-1) == "/" ? "":"/") + "comms";
 | 
			
		||||
    wsServer = new ws.Server({server:server,path:path});
 | 
			
		||||
@@ -74,11 +76,10 @@ function start() {
 | 
			
		||||
    
 | 
			
		||||
    heartbeatTimer = setInterval(function() {
 | 
			
		||||
        var now = Date.now();
 | 
			
		||||
        if (now-lastSentTime > 15000) {
 | 
			
		||||
            lastSentTime = now;
 | 
			
		||||
        if (now-lastSentTime > webSocketKeepAliveTime) {
 | 
			
		||||
            publish("hb",lastSentTime);
 | 
			
		||||
        }
 | 
			
		||||
    }, 15000);
 | 
			
		||||
    }, webSocketKeepAliveTime);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function publish(topic,data,retain) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user