UT: Clean up and faster tearDown.

This commit is contained in:
Arthur Britto
2012-10-14 01:31:46 -07:00
parent 085ff2af22
commit 5fc3d98ef5
5 changed files with 115 additions and 98 deletions

View File

@@ -165,6 +165,7 @@ Remote.prototype.connect_helper = function () {
if (self.expire) { if (self.expire) {
if (self.trace) console.log("remote: was expired"); if (self.trace) console.log("remote: was expired");
ws.onerror = undefined;
self.done(ws.readyState); self.done(ws.readyState);
} else { } else {
@@ -300,15 +301,19 @@ Remote.prototype.connect = function (done, timeout) {
}; };
// Target stated is disconnected. // Target stated is disconnected.
// Note: if exiting or other side is going away, don't need to disconnect.
Remote.prototype.disconnect = function (done) { Remote.prototype.disconnect = function (done) {
var self = this; var self = this;
var ws = this.ws; var ws = this.ws;
if (self.trace) console.log("remote: disconnect");
ws.onclose = function () { ws.onclose = function () {
if (self.trace) console.log("remote: onclose: %s", ws.readyState); if (self.trace) console.log("remote: onclose: %s", ws.readyState);
done(ws.readyState); done(ws.readyState);
}; };
// ws package has a hard coded 30 second timeout.
ws.close(); ws.close();
}; };

View File

@@ -7,11 +7,13 @@ var remote = require("../js/remote.js");
var Amount = amount.Amount; var Amount = amount.Amount;
var fastTearDown = true;
// How long to wait for server to start. // How long to wait for server to start.
var serverDelay = 1500; var serverDelay = 1500;
buster.testRunner.timeout = 5000; buster.testRunner.timeout = 5000;
buster.testCase("Remote functions", { buster.testCase("Remote functions", {
'setUp' : 'setUp' :
function (done) { function (done) {
@@ -30,14 +32,23 @@ buster.testCase("Remote functions", {
'tearDown' : 'tearDown' :
function (done) { function (done) {
alpha.disconnect(function (stat) { if (fastTearDown) {
buster.assert(3 == stat); // CLOSED // Fast tearDown
server.stop("alpha", function (e) {
server.stop("alpha", function (e) { buster.refute(e);
buster.refute(e); done();
done();
});
}); });
}
else {
alpha.disconnect(function (stat) {
buster.assert(3 == stat); // CLOSED
server.stop("alpha", function (e) {
buster.refute(e);
done();
});
});
}
}, },
'request_ledger_current' : 'request_ledger_current' :

View File

@@ -19,135 +19,134 @@ var child = require("child_process");
var servers = {}; var servers = {};
// Create a server object // Create a server object
var Server = function(name) { var Server = function (name) {
this.name = name; this.name = name;
}; };
// Return a server's rippled.cfg as string. // Return a server's rippled.cfg as string.
Server.prototype.configContent = function() { Server.prototype.configContent = function() {
var cfg = config.servers[this.name]; var cfg = config.servers[this.name];
return Object.keys(cfg).map(function(o) { return Object.keys(cfg).map(function(o) {
return util.format("[%s]\n%s\n", o, cfg[o]); return util.format("[%s]\n%s\n", o, cfg[o]);
}).join(""); }).join("");
}; };
Server.prototype.serverPath = function() { Server.prototype.serverPath = function() {
return "tmp/server/" + this.name; return "tmp/server/" + this.name;
}; };
Server.prototype.configPath = function() { Server.prototype.configPath = function() {
return path.join(this.serverPath(), "rippled.cfg"); return path.join(this.serverPath(), "rippled.cfg");
}; };
// Write a server's rippled.cfg. // Write a server's rippled.cfg.
Server.prototype.writeConfig = function(done) { Server.prototype.writeConfig = function(done) {
fs.writeFile(this.configPath(), this.configContent(), 'utf8', done); fs.writeFile(this.configPath(), this.configContent(), 'utf8', done);
}; };
// Spawn the server. // Spawn the server.
Server.prototype.serverSpawnSync = function() { Server.prototype.serverSpawnSync = function() {
// Spawn in standalone mode for now. // Spawn in standalone mode for now.
this.child = child.spawn( this.child = child.spawn(
config.rippled, config.rippled,
[ [
"-a", "-a",
"-v", "-v",
"--conf=rippled.cfg" "--conf=rippled.cfg"
], ],
{ {
cwd: this.serverPath(), cwd: this.serverPath(),
env: process.env, env: process.env,
stdio: 'inherit' stdio: 'inherit'
}); });
console.log("server: start %s: %s -a --conf=%s", this.child.pid, config.rippled, this.configPath()); console.log("server: start %s: %s -a --conf=%s", this.child.pid, config.rippled, this.configPath());
// By default, just log exits.
this.child.on('exit', function(code, signal) {
// If could not exec: code=127, signal=null
// If regular exit: code=0, signal=null
console.log("server: spawn: server exited code=%s: signal=%s", code, signal);
});
// By default, just log exits.
this.child.on('exit', function(code, signal) {
// If could not exec: code=127, signal=null
// If regular exit: code=0, signal=null
console.log("server: spawn: server exited code=%s: signal=%s", code, signal);
});
}; };
// Prepare server's working directory. // Prepare server's working directory.
Server.prototype.makeBase = function(done) { Server.prototype.makeBase = function (done) {
var path = this.serverPath(); var path = this.serverPath();
var self = this; var self = this;
// Reset the server directory, build it if needed. // Reset the server directory, build it if needed.
nodeutils.resetPath(path, '0777', function(e) { nodeutils.resetPath(path, '0777', function (e) {
if (e) { if (e) {
throw e; throw e;
} }
else { else {
self.writeConfig(done); self.writeConfig(done);
} }
}); });
}; };
// Create a standalone server. // Create a standalone server.
// Prepare the working directory and spawn the server. // Prepare the working directory and spawn the server.
Server.prototype.start = function(done) { Server.prototype.start = function (done) {
var self = this; var self = this;
this.makeBase(function(e) { this.makeBase(function (e) {
if (e) { if (e) {
throw e; throw e;
} }
else { else {
self.serverSpawnSync(); self.serverSpawnSync();
done(); done();
} }
}); });
}; };
// Stop a standalone server. // Stop a standalone server.
Server.prototype.stop = function(done) { Server.prototype.stop = function (done) {
if (this.child) { if (this.child) {
// Update the on exit to invoke done. // Update the on exit to invoke done.
this.child.on('exit', function(code, signal) { this.child.on('exit', function (code, signal) {
console.log("server: stop: server exited"); console.log("server: stop: server exited");
done(); done();
}); });
this.child.kill(); this.child.kill();
} }
else else
{ {
console.log("server: stop: no such server"); console.log("server: stop: no such server");
done('noSuchServer'); done('noSuchServer');
} }
}; };
// Start the named server. // Start the named server.
exports.start = function(name, done) { exports.start = function (name, done) {
if (servers[name]) if (servers[name])
{ {
console.log("server: start: server already started."); console.log("server: start: server already started.");
} }
else else
{ {
var server = new Server(name); var server = new Server(name);
servers[name] = server; servers[name] = server;
console.log("server: start: %s", JSON.stringify(server)); console.log("server: start: %s", JSON.stringify(server));
server.start(done); server.start(done);
} }
}; };
// Delete the named server. // Delete the named server.
exports.stop = function(name, done) { exports.stop = function (name, done) {
console.log("server: stop: %s of %s", name, Object.keys(servers).toString()); console.log("server: stop: %s of %s", name, Object.keys(servers).toString());
var server = servers[name]; var server = servers[name];
if (server) { if (server) {
server.stop(done); server.stop(done);
delete servers[name]; delete servers[name];
} }
}; };
exports.Server = Server; exports.Server = Server;

View File

@@ -30,15 +30,17 @@ buster.testCase("WebSocket connection", {
"websocket connect and disconnect" : "websocket connect and disconnect" :
function (done) { function (done) {
var alpha = remote.remoteConfig(config, "alpha"); var alpha = remote.remoteConfig(config, "alpha", 'TRACE');
alpha.connect(function (stat) { alpha.connect(function (stat) {
buster.assert(1 == stat); // OPEN buster.assert.equals(stat, 1); // OPEN
alpha.disconnect(function (stat) { alpha.disconnect(function (stat) {
buster.assert(3 == stat); // CLOSED buster.assert.equals(stat, 3); // CLOSED
done(); done();
}); });
}, serverDelay); }, serverDelay);
}, },
}); });
// vim:sw=2:sts=2:ts=8