diff --git a/README.md b/README.md index 46dab00..8a3c11e 100644 --- a/README.md +++ b/README.md @@ -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" }); ``` diff --git a/index.js b/index.js index be9e0e0..4c86367 100644 --- a/index.js +++ b/index.js @@ -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); }; diff --git a/lib/in-process.js b/lib/in-process.js new file mode 100644 index 0000000..54f5cdd --- /dev/null +++ b/lib/in-process.js @@ -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 () { +}; + + diff --git a/test/test.js b/test/test.js index f87cf61..bfbca6c 100644 --- a/test/test.js +++ b/test/test.js @@ -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 () { @@ -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}); @@ -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) @@ -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"); @@ -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";