Skip to content

Commit

Permalink
add in process strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
pofider committed Feb 17, 2016
1 parent 0f36f5b commit 975bb02
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var scriptManager = require("script-manager")({
inputRequestLimit: 200e6,
/* switch to use dedicated process for script evalution, this can help with
some issues caused by corporate proxies */
strategy: "http-server | dedicated-process"
strategy: "http-server | dedicated-process | in-process"
});
```

Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module.exports = function(options) {
return new (require("./lib/manager-processes.js"))(options);
}

if (options.strategy === "in-process") {
return new (require("./lib/in-process.js"))(options);
}

throw new Error("Unsupported scripts manager strategy: " + options.strategy);
};

Expand Down
20 changes: 20 additions & 0 deletions lib/in-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var ScriptsManager = module.exports = function (options) {
};


ScriptsManager.prototype.start = function (cb) {
cb();
};

ScriptsManager.prototype.ensureStarted = function (cb) {
cb();
};

ScriptsManager.prototype.execute = function (inputs, options, cb) {
require(options.execModulePath)(inputs, options.callback, cb);
};

ScriptsManager.prototype.kill = function () {
};


87 changes: 53 additions & 34 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var should = require("should"),
path = require("path"),
ScriptsManager = require("../lib/manager-servers.js");
ScriptsManagerWithProcesses = require("../lib/manager-processes.js");
ScriptManagerInProcess = require("../lib/in-process.js");


describe("scripts manager", function () {
Expand All @@ -18,6 +19,7 @@ describe("scripts manager", function () {
});

common(scriptsManager);
commonForSafeExecution(scriptsManager);

it("should be able to set up on custom port", function (done) {
var scriptsManager2 = new ScriptsManager({numberOfWorkers: 1, portLeftBoundary: 10000, portRightBoundary: 11000});
Expand Down Expand Up @@ -102,40 +104,34 @@ describe("scripts manager", function () {
});

common(scriptsManager);
commonForSafeExecution(scriptsManager);
});

function common(scriptsManager) {
describe("in process", function () {

it("should be able to execute simple script", function (done) {
scriptsManager.execute({foo: "foo"}, {execModulePath: path.join(__dirname, "scripts", "script.js")}, function (err, res) {
if (err)
return done(err);

res.foo.should.be.eql("foo");
done();
});
var scriptsManager = new ScriptManagerInProcess();
beforeEach(function (done) {
scriptsManager.ensureStarted(done);
});

it("should handle script error", function (done) {
scriptsManager.execute({foo: "foo"}, {execModulePath: path.join(__dirname, "scripts", "error.js")}, function (err, res) {
if (!err)
return done(new Error("It should have failed."));

err.stack.should.containEql("error.js");
done();
});
afterEach(function () {
scriptsManager.kill();
});

common(scriptsManager);
});

function commonForSafeExecution(scriptsManager) {
it("should handle timeouts", function (done) {
var timeouted = false;
scriptsManager.execute({foo: "foo"},
{
execModulePath: path.join(__dirname, "scripts", "timeout.js"),
timeout: 10
}, function (err, res) {
timeouted = true;
done();
});
{
execModulePath: path.join(__dirname, "scripts", "timeout.js"),
timeout: 10
}, function (err, res) {
timeouted = true;
done();
});

setTimeout(function () {
if (!timeouted)
Expand All @@ -153,6 +149,39 @@ describe("scripts manager", function () {
});
});

it("should expose gc", function (done) {
scriptsManager.execute({foo: "foo"}, {execModulePath: path.join(__dirname, "scripts", "gc.js")}, function (err, res) {
if (err)
return done(err);

res.foo.should.be.eql("foo");
done();
});
});
}

function common(scriptsManager) {

it("should be able to execute simple script", function (done) {
scriptsManager.execute({foo: "foo"}, {execModulePath: path.join(__dirname, "scripts", "script.js")}, function (err, res) {
if (err)
return done(err);

res.foo.should.be.eql("foo");
done();
});
});

it("should handle script error", function (done) {
scriptsManager.execute({foo: "foo"}, {execModulePath: path.join(__dirname, "scripts", "error.js")}, function (err, res) {
if (!err)
return done(new Error("It should have failed."));

err.stack.should.containEql("error.js");
done();
});
});

it("should be able to callback to the caller", function (done) {
function callback(str, cb) {
cb(null, str + "aaa");
Expand Down Expand Up @@ -213,16 +242,6 @@ describe("scripts manager", function () {
}
});

it("should expose gc", function (done) {
scriptsManager.execute({foo: "foo"}, {execModulePath: path.join(__dirname, "scripts", "gc.js")}, function (err, res) {
if (err)
return done(err);

res.foo.should.be.eql("foo");
done();
});
});

it("should be able to execute script with giant input data", function (done) {
this.timeout(10000);
var foo = "xxx";
Expand Down

0 comments on commit 975bb02

Please sign in to comment.