From 5fc3d98ef5d0d27e2c8ac7f2042fb391cd098854 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sun, 14 Oct 2012 01:31:46 -0700 Subject: [PATCH] UT: Clean up and faster tearDown. --- js/remote.js | 7 +- test/remote-test.js | 27 +++- test/{standalone-test.js => server-test.js} | 0 test/server.js | 171 ++++++++++---------- test/websocket-test.js | 8 +- 5 files changed, 115 insertions(+), 98 deletions(-) rename test/{standalone-test.js => server-test.js} (100%) diff --git a/js/remote.js b/js/remote.js index 064dc81f6..9825797fa 100644 --- a/js/remote.js +++ b/js/remote.js @@ -165,6 +165,7 @@ Remote.prototype.connect_helper = function () { if (self.expire) { if (self.trace) console.log("remote: was expired"); + ws.onerror = undefined; self.done(ws.readyState); } else { @@ -300,15 +301,19 @@ Remote.prototype.connect = function (done, timeout) { }; // Target stated is disconnected. +// Note: if exiting or other side is going away, don't need to disconnect. Remote.prototype.disconnect = function (done) { var self = this; var ws = this.ws; + + if (self.trace) console.log("remote: disconnect"); ws.onclose = function () { if (self.trace) console.log("remote: onclose: %s", ws.readyState); done(ws.readyState); }; - + + // ws package has a hard coded 30 second timeout. ws.close(); }; diff --git a/test/remote-test.js b/test/remote-test.js index ea8570ec8..4292575e2 100644 --- a/test/remote-test.js +++ b/test/remote-test.js @@ -7,11 +7,13 @@ var remote = require("../js/remote.js"); var Amount = amount.Amount; +var fastTearDown = true; + // How long to wait for server to start. var serverDelay = 1500; buster.testRunner.timeout = 5000; - + buster.testCase("Remote functions", { 'setUp' : function (done) { @@ -30,14 +32,23 @@ buster.testCase("Remote functions", { 'tearDown' : function (done) { - alpha.disconnect(function (stat) { - buster.assert(3 == stat); // CLOSED - - server.stop("alpha", function (e) { - buster.refute(e); - done(); - }); + if (fastTearDown) { + // Fast tearDown + server.stop("alpha", function (e) { + buster.refute(e); + done(); }); + } + else { + alpha.disconnect(function (stat) { + buster.assert(3 == stat); // CLOSED + + server.stop("alpha", function (e) { + buster.refute(e); + done(); + }); + }); + } }, 'request_ledger_current' : diff --git a/test/standalone-test.js b/test/server-test.js similarity index 100% rename from test/standalone-test.js rename to test/server-test.js diff --git a/test/server.js b/test/server.js index 680e9d4e3..be6d8e7be 100644 --- a/test/server.js +++ b/test/server.js @@ -19,135 +19,134 @@ var child = require("child_process"); var servers = {}; // Create a server object -var Server = function(name) { - this.name = name; +var Server = function (name) { + this.name = name; }; // Return a server's rippled.cfg as string. Server.prototype.configContent = function() { - var cfg = config.servers[this.name]; + var cfg = config.servers[this.name]; - return Object.keys(cfg).map(function(o) { - return util.format("[%s]\n%s\n", o, cfg[o]); - }).join(""); + return Object.keys(cfg).map(function(o) { + return util.format("[%s]\n%s\n", o, cfg[o]); + }).join(""); }; Server.prototype.serverPath = function() { - return "tmp/server/" + this.name; + return "tmp/server/" + this.name; }; Server.prototype.configPath = function() { - return path.join(this.serverPath(), "rippled.cfg"); + return path.join(this.serverPath(), "rippled.cfg"); }; // Write a server's rippled.cfg. Server.prototype.writeConfig = function(done) { - fs.writeFile(this.configPath(), this.configContent(), 'utf8', done); + fs.writeFile(this.configPath(), this.configContent(), 'utf8', done); }; // Spawn the server. Server.prototype.serverSpawnSync = function() { - // Spawn in standalone mode for now. - this.child = child.spawn( - config.rippled, - [ - "-a", - "-v", - "--conf=rippled.cfg" - ], - { - cwd: this.serverPath(), - env: process.env, - stdio: 'inherit' - }); + // Spawn in standalone mode for now. + this.child = child.spawn( + config.rippled, + [ + "-a", + "-v", + "--conf=rippled.cfg" + ], + { + cwd: this.serverPath(), + env: process.env, + stdio: 'inherit' + }); - 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); - }); + 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); + }); }; // Prepare server's working directory. -Server.prototype.makeBase = function(done) { - var path = this.serverPath(); - var self = this; +Server.prototype.makeBase = function (done) { + var path = this.serverPath(); + var self = this; - // Reset the server directory, build it if needed. - nodeutils.resetPath(path, '0777', function(e) { - if (e) { - throw e; - } - else { - self.writeConfig(done); - } - }); + // Reset the server directory, build it if needed. + nodeutils.resetPath(path, '0777', function (e) { + if (e) { + throw e; + } + else { + self.writeConfig(done); + } + }); }; // Create a standalone server. // Prepare the working directory and spawn the server. -Server.prototype.start = function(done) { - var self = this; +Server.prototype.start = function (done) { + var self = this; - this.makeBase(function(e) { - if (e) { - throw e; - } - else { - self.serverSpawnSync(); - done(); - } - }); + this.makeBase(function (e) { + if (e) { + throw e; + } + else { + self.serverSpawnSync(); + done(); + } + }); }; // Stop a standalone server. -Server.prototype.stop = function(done) { - if (this.child) { - // Update the on exit to invoke done. - this.child.on('exit', function(code, signal) { - console.log("server: stop: server exited"); - done(); - }); - this.child.kill(); - } - else - { - console.log("server: stop: no such server"); - done('noSuchServer'); - } +Server.prototype.stop = function (done) { + if (this.child) { + // Update the on exit to invoke done. + this.child.on('exit', function (code, signal) { + console.log("server: stop: server exited"); + done(); + }); + this.child.kill(); + } + else + { + console.log("server: stop: no such server"); + done('noSuchServer'); + } }; // Start the named server. -exports.start = function(name, done) { - if (servers[name]) - { - console.log("server: start: server already started."); - } - else - { - var server = new Server(name); +exports.start = function (name, done) { + if (servers[name]) + { + console.log("server: start: server already started."); + } + else + { + 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. -exports.stop = function(name, done) { - console.log("server: stop: %s of %s", name, Object.keys(servers).toString()); +exports.stop = function (name, done) { + console.log("server: stop: %s of %s", name, Object.keys(servers).toString()); - var server = servers[name]; - if (server) { - server.stop(done); - delete servers[name]; - } + var server = servers[name]; + if (server) { + server.stop(done); + delete servers[name]; + } }; exports.Server = Server; diff --git a/test/websocket-test.js b/test/websocket-test.js index 82507101a..9e6e87261 100644 --- a/test/websocket-test.js +++ b/test/websocket-test.js @@ -30,15 +30,17 @@ buster.testCase("WebSocket connection", { "websocket connect and disconnect" : function (done) { - var alpha = remote.remoteConfig(config, "alpha"); + var alpha = remote.remoteConfig(config, "alpha", 'TRACE'); alpha.connect(function (stat) { - buster.assert(1 == stat); // OPEN + buster.assert.equals(stat, 1); // OPEN alpha.disconnect(function (stat) { - buster.assert(3 == stat); // CLOSED + buster.assert.equals(stat, 3); // CLOSED done(); }); }, serverDelay); }, }); + +// vim:sw=2:sts=2:ts=8