UT: Rework server module to provide an object.

This commit is contained in:
Arthur Britto
2012-09-21 14:37:16 -07:00
parent 73c5b6addb
commit 169f0f487a
3 changed files with 98 additions and 54 deletions

View File

@@ -1,15 +1,22 @@
// YYY Should probably have two versions: node vs browser
var fs = require("fs");
var path = require("path");
Function.prototype.method = function(name,func) {
this.prototype[name] = func;
return this;
};
var filterErr = function(code, done) {
return function (e) {
return function(e) {
done(e.code !== code ? e : undefined);
};
};
var throwErr = function(done) {
return function (e) {
return function(e) {
if (e)
throw e;
@@ -20,7 +27,7 @@ var throwErr = function(done) {
// apply function to elements of array. Return first true value to done or undefined.
var mapOr = function(func, array, done) {
if (array.length) {
func(array[array.length-1], function (v) {
func(array[array.length-1], function(v) {
if (v) {
done(v);
}
@@ -37,7 +44,7 @@ var mapOr = function(func, array, done) {
// Make a directory and sub-directories.
var mkPath = function(dirPath, mode, done) {
fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function (e) {
fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function(e) {
if (!e || e.code === "EEXIST") {
// Created or already exists, done.
done();
@@ -62,7 +69,7 @@ var mkPath = function(dirPath, mode, done) {
// Empty a directory.
var emptyPath = function(dirPath, done) {
fs.readdir(dirPath, function (err, files) {
fs.readdir(dirPath, function(err, files) {
if (err) {
done(err);
}
@@ -76,7 +83,7 @@ var emptyPath = function(dirPath, done) {
var rmPath = function(dirPath, done) {
// console.log("rmPath: %s", dirPath);
fs.lstat(dirPath, function (err, stats) {
fs.lstat(dirPath, function(err, stats) {
if (err && err.code == "ENOENT") {
done();
}
@@ -84,7 +91,7 @@ var rmPath = function(dirPath, done) {
done(err);
}
else if (stats.isDirectory()) {
emptyPath(dirPath, function (e) {
emptyPath(dirPath, function(e) {
if (e) {
done(e);
}
@@ -103,7 +110,7 @@ var rmPath = function(dirPath, done) {
// Create directory if needed and empty if needed.
var resetPath = function(dirPath, mode, done) {
mkPath(dirPath, mode, function (e) {
mkPath(dirPath, mode, function(e) {
if (e) {
done(e);
}

View File

@@ -6,7 +6,7 @@
//
var config = require("./config.js");
var utils = require("./utils.js");
var utils = require("../js/utils.js");
var fs = require("fs");
var path = require("path");
@@ -15,99 +15,137 @@ var child = require("child_process");
var servers = {};
var serverPath = function(name) {
return "tmp/server/" + name;
// Create a server object
var Server = function(name) {
this.name = name;
};
// Return a server's newcoind.cfg as string.
var configContent = function(name) {
var cfg = config.servers[name];
Server.method('configContent', function() {
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]);
}).join("");
};
});
var configPath = function(name) {
return path.join(serverPath(name), "newcoind.cfg");
};
Server.method('serverPath', function() {
return "tmp/server/" + this.name;
});
Server.method('configPath', function() {
return path.join(this.serverPath(), "newcoind.cfg");
});
// Write a server's newcoind.cfg.
var writeConfig = function(name, done) {
fs.writeFile(configPath(name), configContent(name), 'utf8', done);
};
Server.method('writeConfig', function(done) {
fs.writeFile(this.configPath(), this.configContent(), 'utf8', done);
});
var serverSpawnSync = function(name) {
// Spawn the server.
Server.method('serverSpawnSync', function() {
// Spawn in standalone mode for now.
var server = child.spawn(
this.child = child.spawn(
config.newcoind,
[
"-a",
"--conf=newcoind.cfg"
],
{
cwd: serverPath(name),
cwd: this.serverPath(),
env: process.env,
stdio: 'inherit'
});
servers[name] = server;
console.log("server: %s: %s -a --conf=%s", server.pid, config.newcoind, configPath(name));
console.log("sever: start: servers = %s", Object.keys(servers).toString());
console.log("server: start %s: %s -a --conf=%s", this.child.pid, config.newcoind, this.configPath());
server.on('exit', function (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("sever: spawn: server exited code=%s: signal=%s", code, signal);
delete servers[name];
console.log("server: spawn: server exited code=%s: signal=%s", code, signal);
});
};
});
var makeBase = function(name, done) {
var path = serverPath(name);
// Prepare server's working directory.
Server.method('makeBase', function(done) {
var path = this.serverPath();
var self = this;
// Reset the server directory, build it if needed.
utils.resetPath(path, '0777', function (e) {
utils.resetPath(path, '0777', function(e) {
if (e) {
throw e;
}
else {
writeConfig(name, done);
self.writeConfig(done);
}
});
};
});
// Create a standalone server.
// Prepare the working directory and spawn the server.
exports.start = function(name, done) {
makeBase(name, function (e) {
Server.method('start', function(done) {
var self = this;
this.makeBase(function(e) {
if (e) {
throw e;
}
else {
serverSpawnSync(name);
self.serverSpawnSync();
done();
}
});
};
});
exports.stop = function(name, done) {
console.log("sever: stop: servers = %s", Object.keys(servers).toString());
var server = servers[name];
if (server) {
server.on('exit', function (code, signal) {
console.log("sever: stop: server exited");
delete servers[name];
// Stop a standalone server.
Server.method('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();
});
server.kill();
this.child.kill();
}
else
{
console.log("sever: stop: no such server");
console.log("server: stop: no such server");
done();
}
});
// 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);
servers[name] = server;
console.log("server: start: %s", JSON.stringify(server));
server.start(done);
}
};
// Delete the named server.
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];
}
};
exports.Server = Server;
// vim:ts=4

View File

@@ -6,11 +6,11 @@ var buster = require("buster");
var server = require("./server.js");
buster.testCase("Check standalone server startup", {
"server start and stop": function (done) {
"server start and stop": function(done) {
server.start("alpha",
function (e) {
function(e) {
buster.refute(e);
server.stop("alpha", function (e) {
server.stop("alpha", function(e) {
buster.refute(e);
done();
});
@@ -18,5 +18,4 @@ buster.testCase("Check standalone server startup", {
}
});
// console.log("standalone-test.js<");
// vim:ts=4