mirror of
https://github.com/Xahau/xahaud.git
synced 2025-11-20 18:45:55 +00:00
UT: Add timeout to WS connecting.
This commit is contained in:
115
js/remote.js
115
js/remote.js
@@ -4,65 +4,132 @@
|
|||||||
// http://www.w3.org/TR/websockets/#the-websocket-interface
|
// http://www.w3.org/TR/websockets/#the-websocket-interface
|
||||||
//
|
//
|
||||||
// YYY Will later provide a network access which use multiple instances of this.
|
// YYY Will later provide a network access which use multiple instances of this.
|
||||||
|
// YYY A better model might be to allow requesting a target state: keep connected or not.
|
||||||
//
|
//
|
||||||
|
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
|
|
||||||
var WebSocket = require('ws');
|
var WebSocket = require('ws');
|
||||||
|
|
||||||
// YYY This is wrong should not use anything in test directory.
|
|
||||||
var config = require("../test/config.js");
|
|
||||||
|
|
||||||
// --> trusted: truthy, if remote is trusted
|
// --> trusted: truthy, if remote is trusted
|
||||||
var Remote = function(trusted, websocket_ip, websocket_port) {
|
var Remote = function(trusted, websocket_ip, websocket_port, trace) {
|
||||||
this.trusted = trusted;
|
this.trusted = trusted;
|
||||||
this.websocket_ip = websocket_ip;
|
this.websocket_ip = websocket_ip;
|
||||||
this.websocket_port = websocket_port;
|
this.websocket_port = websocket_port;
|
||||||
this.id = 0;
|
this.id = 0;
|
||||||
|
this.trace = trace;
|
||||||
};
|
};
|
||||||
|
|
||||||
var remoteConfig = function(server) {
|
var remoteConfig = function(config, server) {
|
||||||
var serverConfig = config.servers[server];
|
var serverConfig = config.servers[server];
|
||||||
|
|
||||||
return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port);
|
return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Target state is connectted.
|
Remote.method('connect_helper', function() {
|
||||||
// done(readyState):
|
var self = this;
|
||||||
// --> readyState: OPEN, CLOSED
|
|
||||||
Remote.method('connect', function(done, onmessage) {
|
|
||||||
var url = util.format("ws://%s:%s", this.websocket_ip, this.websocket_port);
|
|
||||||
|
|
||||||
console.log("remote: connect: %s", url);
|
if (this.trace)
|
||||||
|
console.log("remote: connect: %s", this.url);
|
||||||
|
|
||||||
this.ws = new WebSocket(url);
|
this.ws = new WebSocket(this.url);
|
||||||
|
|
||||||
var ws = this.ws;
|
var ws = this.ws;
|
||||||
|
|
||||||
ws.onopen = function() {
|
ws.onopen = function() {
|
||||||
console.log("remote: onopen: %s", ws.readyState);
|
if (this.trace)
|
||||||
ws.onclose = undefined;
|
console.log("remote: onopen: %s", ws.readyState);
|
||||||
done(ws.readyState);
|
|
||||||
|
ws.onclose = undefined;
|
||||||
|
ws.onerror = undefined;
|
||||||
|
|
||||||
|
self.done(ws.readyState);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Also covers failure to open.
|
ws.onerror = function() {
|
||||||
ws.onclose = function() {
|
if (this.trace)
|
||||||
console.log("remote: onclose: %s", ws.readyState);
|
console.log("remote: onerror: %s", ws.readyState);
|
||||||
done(ws.readyState);
|
|
||||||
|
ws.onclose = undefined;
|
||||||
|
|
||||||
|
if (self.expire) {
|
||||||
|
if (this.trace)
|
||||||
|
console.log("remote: was expired");
|
||||||
|
|
||||||
|
self.done(ws.readyState);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Delay and retry.
|
||||||
|
setTimeout(function() {
|
||||||
|
if (this.trace)
|
||||||
|
console.log("remote: retry");
|
||||||
|
|
||||||
|
self.connect_helper();
|
||||||
|
}, 50); // Retry rate 50ms.
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (onmessage) {
|
// Covers failure to open.
|
||||||
ws.onmessage = onmessage;
|
ws.onclose = function() {
|
||||||
|
if (this.trace)
|
||||||
|
console.log("remote: onclose: %s", ws.readyState);
|
||||||
|
|
||||||
|
ws.onerror = undefined;
|
||||||
|
|
||||||
|
self.done(ws.readyState);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.onmessage) {
|
||||||
|
ws.onmessage = this.onmessage;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Target state is connectted.
|
||||||
|
// done(readyState):
|
||||||
|
// --> readyState: OPEN, CLOSED
|
||||||
|
Remote.method('connect', function(done, onmessage, timeout) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
this.url = util.format("ws://%s:%s", this.websocket_ip, this.websocket_port);
|
||||||
|
this.done = done;
|
||||||
|
|
||||||
|
if (onmessage)
|
||||||
|
this.onmessage = onmessage;
|
||||||
|
|
||||||
|
if (timeout) {
|
||||||
|
if (this.trace)
|
||||||
|
console.log("remote: expire: false");
|
||||||
|
|
||||||
|
this.expire = false;
|
||||||
|
|
||||||
|
setTimeout(function () {
|
||||||
|
if (this.trace)
|
||||||
|
console.log("remote: expire: timeout");
|
||||||
|
|
||||||
|
self.expire = true;
|
||||||
|
}, timeout);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (this.trace)
|
||||||
|
console.log("remote: expire: false");
|
||||||
|
|
||||||
|
this.expire = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.connect_helper();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
// Target stated is disconnected.
|
// Target stated is disconnected.
|
||||||
Remote.method('disconnect', function(done) {
|
Remote.method('disconnect', function(done) {
|
||||||
var ws = this.ws;
|
var ws = this.ws;
|
||||||
|
|
||||||
ws.onclose = function() {
|
ws.onclose = function() {
|
||||||
console.log("remote: onclose: %s", ws.readyState);
|
if (this.trace)
|
||||||
done(ws.readyState);
|
console.log("remote: onclose: %s", ws.readyState);
|
||||||
|
|
||||||
|
done(ws.readyState);
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
// Manage test servers
|
// Manage test servers
|
||||||
|
//
|
||||||
|
// YYY Would be nice to be able to hide server output.
|
||||||
|
//
|
||||||
|
|
||||||
// Provide servers
|
// Provide servers
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -5,8 +5,15 @@ var buster = require("buster");
|
|||||||
|
|
||||||
var server = require("./server.js");
|
var server = require("./server.js");
|
||||||
var remote = require("../js/remote.js");
|
var remote = require("../js/remote.js");
|
||||||
|
var config = require("./config.js");
|
||||||
|
|
||||||
buster.testCase("Check standalone server startup", {
|
// How long to wait for server to start.
|
||||||
|
var serverDelay = 1500;
|
||||||
|
|
||||||
|
buster.testRunner.timeout = 5000;
|
||||||
|
|
||||||
|
|
||||||
|
buster.testCase("Standalone server startup", {
|
||||||
"server start and stop": function(done) {
|
"server start and stop": function(done) {
|
||||||
server.start("alpha",
|
server.start("alpha",
|
||||||
function(e) {
|
function(e) {
|
||||||
@@ -19,14 +26,15 @@ buster.testCase("Check standalone server startup", {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
buster.testCase("Check websocket connection", {
|
buster.testCase("WebSocket connection", {
|
||||||
'setUp' :
|
'setUp' :
|
||||||
function(done) {
|
function(done) {
|
||||||
server.start("alpha",
|
server.start("alpha",
|
||||||
function(e) {
|
function(e) {
|
||||||
buster.refute(e);
|
buster.refute(e);
|
||||||
done();
|
done();
|
||||||
});
|
}
|
||||||
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
'tearDown' :
|
'tearDown' :
|
||||||
@@ -39,24 +47,53 @@ buster.testCase("Check websocket connection", {
|
|||||||
|
|
||||||
"websocket connect and disconnect" :
|
"websocket connect and disconnect" :
|
||||||
function(done) {
|
function(done) {
|
||||||
var alpha = remote.remoteConfig("alpha");
|
var alpha = remote.remoteConfig(config, "alpha");
|
||||||
|
|
||||||
alpha.connect(function(stat) {
|
alpha.connect(function(stat) {
|
||||||
buster.assert(1 == stat); // OPEN
|
buster.assert(1 == stat); // OPEN
|
||||||
|
|
||||||
alpha.disconnect(function(stat) {
|
alpha.disconnect(function(stat) {
|
||||||
buster.assert(3 == stat); // CLOSED
|
buster.assert(3 == stat); // CLOSED
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
}, undefined, serverDelay);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
buster.testCase("Check assert", {
|
// var alpha = remote.remoteConfig("alpha");
|
||||||
"assert" :
|
//
|
||||||
function() {
|
// buster.testCase("Websocket commands", {
|
||||||
buster.assert(true);
|
// 'setUp' :
|
||||||
}
|
// function(done) {
|
||||||
});
|
// server.start("alpha",
|
||||||
|
// function(e) {
|
||||||
|
// buster.refute(e);
|
||||||
|
//
|
||||||
|
// alpha.connect(function(stat) {
|
||||||
|
// buster.assert(1 == stat); // OPEN
|
||||||
|
//
|
||||||
|
// done();
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// 'tearDown' :
|
||||||
|
// function(done) {
|
||||||
|
// alpha.disconnect(function(stat) {
|
||||||
|
// buster.assert(3 == stat); // CLOSED
|
||||||
|
//
|
||||||
|
// server.stop("alpha", function(e) {
|
||||||
|
// buster.refute(e);
|
||||||
|
//
|
||||||
|
// done();
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// "assert" :
|
||||||
|
// function() {
|
||||||
|
// buster.assert(true);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
// vim:ts=4
|
// vim:ts=4
|
||||||
|
|||||||
Reference in New Issue
Block a user