buster-ci-agent
Advanced tools
Comparing version 0.1.2 to 0.1.3
178
lib/agent.js
@@ -8,3 +8,4 @@ "use strict"; | ||
logger = require("evented-logger"), | ||
closeWindows = require("./close-windows"); | ||
closeWindows = require("./close-windows"), | ||
async = require("async"); | ||
@@ -25,72 +26,2 @@ | ||
function browserClosedLog(browser) { | ||
/*jshint validthis: true */ | ||
this._logger.info("browser " + browser + " closed"); | ||
} | ||
function startBrowser(browserName, browser, url, id) { | ||
/*jshint validthis: true */ | ||
var logger = this._logger; | ||
if (browser.prepareStart) { | ||
logger.info("prepare start"); | ||
var prepareStartCommand = browser.prepareStart; | ||
logger.debug(prepareStartCommand); | ||
cp.exec( | ||
prepareStartCommand, | ||
processLog.bind(this) | ||
); | ||
} | ||
var startCommand = browser.start; | ||
var startArgs = browser.startArgs; | ||
startArgs = startArgs ? startArgs.slice(0) : []; | ||
if (url) { | ||
url = id ? url + "?id=" + id : url; | ||
startArgs.push(url); | ||
} | ||
logger.info("start browser " + browserName); | ||
var process = cp.spawn(startCommand, startArgs); | ||
process.stdout.on('data', function(buffer){ | ||
logger.debug(buffer.toString('utf8')) | ||
}); | ||
process.stderr.on('data', function(buffer){ | ||
logger.error(buffer.toString('utf8')) | ||
}); | ||
process.on('close', browserClosedLog.bind(this, browserName)); | ||
browser.process = process; | ||
} | ||
function stopBrowser(browserName, browser) { | ||
/*jshint validthis: true */ | ||
if (browser.stop) { | ||
if (browser.stop.command) { | ||
this._logger.info("stop browser " + browserName + " by command"); | ||
var command = browser.stop.command.replace(/\$\{PID\}/g, | ||
browser.process.pid | ||
); | ||
this._logger.debug(command); | ||
cp.exec(command, processLog.bind(this)); | ||
} | ||
if (browser.stop.windowTitle) { | ||
this._logger.info("stop browser " + browserName | ||
+ " by closing window"); | ||
try { | ||
closeWindows(browser.stop.windowTitle); | ||
} catch (err) { | ||
this._logger.error(err.message); | ||
} | ||
} | ||
} else { | ||
this._logger.info("stop browser " + browserName); | ||
browser.process.kill(); | ||
} | ||
delete browser.process; | ||
} | ||
function Agent(config) { | ||
@@ -119,2 +50,5 @@ this._config = config; | ||
this._bayeux.attach(this._server); | ||
process.on("SIGINT", function () { | ||
this.close(function () { process.exit(0); }); | ||
}.bind(this)); | ||
} | ||
@@ -134,3 +68,3 @@ | ||
} | ||
var response = this.handleRequest(request); | ||
var response = this._handleRequest(request); | ||
if (response) { | ||
@@ -145,4 +79,3 @@ this._client.publish("/messages", response); | ||
handleRequest: function (request) { | ||
_handleRequest: function (request) { | ||
var browser; | ||
@@ -158,3 +91,3 @@ this._logger.debug("received command: " + formatio.ascii(request)); | ||
if (request.browsers.hasOwnProperty(browser)) { | ||
startBrowser.call(this, | ||
this._startBrowser.call(this, | ||
browser, | ||
@@ -171,3 +104,3 @@ this._config.browsers[browser], | ||
if (request.browsers.hasOwnProperty(browser)) { | ||
stopBrowser.call(this, | ||
this._stopBrowser.call(this, | ||
browser, this._config.browsers[browser]); | ||
@@ -183,9 +116,92 @@ } | ||
close: function (cb) { | ||
if (this._bayeux) { | ||
this._bayeux.close(); | ||
_startBrowser: function (browserName, browser, url, id) { | ||
var logger = this._logger; | ||
if (browser.prepareStart) { | ||
logger.info("prepare start"); | ||
var prepareStartCommand = browser.prepareStart; | ||
logger.debug(prepareStartCommand); | ||
cp.exec( | ||
prepareStartCommand, | ||
processLog.bind(this) | ||
); | ||
} | ||
if (this._server) { | ||
this._server.close(cb); | ||
var startCommand = browser.start; | ||
var startArgs = browser.startArgs; | ||
startArgs = startArgs ? startArgs.slice(0) : []; | ||
if (url) { | ||
url = id ? url + "?id=" + id : url; | ||
startArgs.push(url); | ||
} | ||
logger.info("start browser " + browserName); | ||
var process = cp.spawn(startCommand, startArgs); | ||
process.stdout.on('data', function (buffer) { | ||
logger.debug(buffer.toString('utf8')) | ||
}); | ||
process.stderr.on('data', function (buffer) { | ||
logger.error(buffer.toString('utf8')) | ||
}); | ||
browser.process = process; | ||
}, | ||
_stopBrowser: function (browserName, browser, cb) { | ||
cb = cb || function () {}; | ||
if (!browser.process) { | ||
return cb(); | ||
} | ||
if (browser.stop) { | ||
if (browser.stop.command) { | ||
this._logger.info("stop browser " + | ||
browserName + " by command"); | ||
var command = browser.stop.command.replace(/\$\{PID\}/g, | ||
browser.process.pid | ||
); | ||
this._logger.debug(command); | ||
var process = cp.exec(command, processLog.bind(this)); | ||
process.on("exit", cb); | ||
} | ||
if (browser.stop.windowTitle) { | ||
this._logger.info("stop browser " + browserName | ||
+ " by closing window"); | ||
try { | ||
closeWindows(browser.stop.windowTitle); | ||
cb(); | ||
} catch (err) { | ||
this._logger.error(err.message); | ||
} | ||
} | ||
} else { | ||
this._logger.info("stop browser " + browserName); | ||
browser.process.kill(); | ||
cb(); | ||
} | ||
delete browser.process; | ||
}, | ||
_stopAllBrowsers: function (cb) { | ||
var browserName; | ||
var browser; | ||
var tasks = []; | ||
for (browserName in this._config.browsers) { | ||
if (this._config.browsers.hasOwnProperty(browserName)) { | ||
browser = this._config.browsers[browserName]; | ||
tasks.push(this._stopBrowser.bind(this, browserName, browser)); | ||
} | ||
} | ||
async.parallel(tasks, cb); | ||
}, | ||
close: function (cb) { | ||
this._stopAllBrowsers(function () { | ||
if (this._bayeux) { | ||
this._bayeux.close(); | ||
} | ||
if (this._server) { | ||
this._server.close(cb); | ||
} | ||
}.bind(this)); | ||
} | ||
@@ -192,0 +208,0 @@ }; |
@@ -46,3 +46,3 @@ "use strict"; | ||
var windows = []; | ||
var titleBuffer = ref.alloc('String', strOfLength(MAX_TITLE_SIZE)); | ||
var titleBuffer = ref.alloc('String', strOfLength(MAX_TITLE_SIZE * 2)); | ||
var lpEnumFunc = ffi.Callback('bool', ['int', 'int'], | ||
@@ -61,3 +61,3 @@ function (hwnd, lParam) { | ||
user32.EnumWindows(lpEnumFunc, 0); | ||
return windows; | ||
@@ -69,3 +69,3 @@ } | ||
var windows = getWindowList(windowTitle); | ||
var i; | ||
@@ -72,0 +72,0 @@ for (i = 0; i < windows.length; i++) { |
{ | ||
"name": "buster-ci-agent", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Starts, captures and stops browsers for buster-ci.", | ||
@@ -30,3 +30,4 @@ "bugs": { | ||
"formatio": "~1.1", | ||
"evented-logger": "~1.0" | ||
"evented-logger": "~1.0", | ||
"async": "~0.9" | ||
}, | ||
@@ -33,0 +34,0 @@ "optionalDependencies": { |
@@ -12,2 +12,6 @@ # buster-ci-agent | ||
**0.1.3** (04.03.2015) | ||
* Close all browsers on exit, part of issue [#447](https://github.com/busterjs/buster/issues/447) | ||
**0.1.2** (30.01.2015) | ||
@@ -14,0 +18,0 @@ |
"use strict"; | ||
var buster = require("buster"), | ||
events = require("events"), | ||
@@ -31,19 +32,25 @@ proxyquire = require("proxyquire"), | ||
this.processStub = { | ||
on: this.stub(), | ||
stdout: { | ||
on: this.stub() | ||
}, | ||
stderr: { | ||
on: this.stub() | ||
}, | ||
kill: this.stub() | ||
create: function () { | ||
var stub = new events.EventEmitter(); | ||
stub.stdout = { | ||
on: this.stub() | ||
}; | ||
stub.stderr = { | ||
on: this.stub() | ||
}; | ||
stub.kill = this.stub(); | ||
return stub; | ||
}.bind(this) | ||
} | ||
this.stub(childProcessStub, "spawn").returns( | ||
Object.create(this.processStub) | ||
this.processStub.create() | ||
); | ||
this.stub(childProcessStub, "exec"); | ||
process.setMaxListeners(0); | ||
}, | ||
tearDown: function () { | ||
this.agent.close(); | ||
this.agent.close(function () {}); | ||
}, | ||
@@ -78,3 +85,3 @@ | ||
this.agent = new Agent(config); | ||
var response = this.agent.handleRequest({ command: "Welcome" }); | ||
var response = this.agent._handleRequest({ command: "Welcome" }); | ||
@@ -105,3 +112,3 @@ assert.equals(response.browsers, config.browsers); | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "start", | ||
@@ -143,3 +150,3 @@ browsers: { | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "start", | ||
@@ -176,3 +183,3 @@ browsers: { "Chrome": {} }, | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "start", | ||
@@ -217,3 +224,3 @@ browsers: { | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "start", | ||
@@ -249,4 +256,4 @@ browsers: { | ||
}; | ||
var processChrome = Object.create(this.processStub); | ||
var processIE = Object.create(this.processStub); | ||
var processChrome = this.processStub.create(); | ||
var processIE = this.processStub.create(); | ||
childProcessStub.spawn.withArgs(config.browsers.Chrome.start) | ||
@@ -258,3 +265,3 @@ .returns(processChrome); | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "start", | ||
@@ -266,3 +273,3 @@ browsers: { | ||
}); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "stop", | ||
@@ -293,8 +300,10 @@ browsers: { | ||
}; | ||
var processIE = Object.create(this.processStub); | ||
var processIE = this.processStub.create(); | ||
childProcessStub.spawn.withArgs(config.browsers.IE.start) | ||
.returns(processIE); | ||
childProcessStub.exec.withArgs(config.browsers.IE.stop.command) | ||
.returns(processIE); | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "start", | ||
@@ -305,3 +314,3 @@ browsers: { | ||
}); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "stop", | ||
@@ -331,9 +340,11 @@ browsers: { | ||
}; | ||
var processIE = Object.create(this.processStub); | ||
var processIE = this.processStub.create(); | ||
processIE.pid = 1234; | ||
childProcessStub.spawn.withArgs(config.browsers.IE.start) | ||
.returns(processIE); | ||
childProcessStub.exec.withArgs("commandToStop 1234") | ||
.returns(processIE); | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "start", | ||
@@ -344,3 +355,3 @@ browsers: { | ||
}); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "stop", | ||
@@ -369,3 +380,3 @@ browsers: { | ||
}; | ||
var processIE = Object.create(this.processStub); | ||
var processIE = this.processStub.create(); | ||
processIE.pid = 1234; | ||
@@ -376,3 +387,3 @@ childProcessStub.spawn.withArgs(config.browsers.IE.start) | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "start", | ||
@@ -383,3 +394,3 @@ browsers: { | ||
}); | ||
this.agent.handleRequest({ | ||
this.agent._handleRequest({ | ||
command: "stop", | ||
@@ -396,5 +407,87 @@ browsers: { | ||
); | ||
}, | ||
"kills all running browsers on close": function () { | ||
var config = { | ||
port: 8888, | ||
browsers: { | ||
IE: { | ||
start: "iexplore" | ||
}, | ||
Chrome: { | ||
start: "chromium-browser", | ||
startArgs: ["--new-window"] | ||
}, | ||
FF: { | ||
start: "firefox" | ||
} | ||
}, | ||
logLevel: 0 | ||
}; | ||
var processIE = this.processStub.create(); | ||
processIE.pid = 1234; | ||
childProcessStub.spawn.withArgs(config.browsers.IE.start) | ||
.returns(processIE); | ||
childProcessStub.spawn.withArgs(config.browsers.IE.stop) | ||
.returns(processIE); | ||
var processChrome = this.processStub.create(); | ||
processIE.pid = 5678; | ||
childProcessStub.spawn.withArgs(config.browsers.Chrome.start) | ||
.returns(processChrome); | ||
var processFF = this.processStub.create(); | ||
processFF.pid = 1111; | ||
childProcessStub.spawn.withArgs(config.browsers.FF.start) | ||
.returns(processFF); | ||
this.agent = new Agent(config); | ||
this.agent._handleRequest({ | ||
command: "start", | ||
browsers: { | ||
IE: {}, | ||
Chrome: {} | ||
} | ||
}); | ||
this.agent.close(); | ||
assert.calledOnce(processIE.kill); | ||
assert.calledOnce(processChrome.kill); | ||
refute.called(processFF.kill); | ||
}, | ||
"calls callback of close after browsers are closed": function () { | ||
var config = { | ||
port: 8888, | ||
browsers: { | ||
IE: { | ||
start: "iexplore" | ||
}, | ||
Chrome: { | ||
start: "chromium-browser", | ||
startArgs: ["--new-window"] | ||
}, | ||
FF: { | ||
start: "firefox" | ||
} | ||
}, | ||
logLevel: 0 | ||
}; | ||
this.server.close.callsArg(0); | ||
this.agent = new Agent(config); | ||
this.stub(this.agent, "_stopBrowser"); | ||
var callback = this.stub(); | ||
this.agent.close(callback); | ||
var cb1 = this.agent._stopBrowser.getCall(0).args[2]; | ||
var cb2 = this.agent._stopBrowser.getCall(1).args[2]; | ||
var cb3 = this.agent._stopBrowser.getCall(2).args[2]; | ||
refute.called(callback); | ||
cb1(); | ||
refute.called(callback); | ||
cb2(); | ||
refute.called(callback); | ||
cb3(); | ||
assert.calledOnce(callback); | ||
} | ||
} | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28039
728
24
6
+ Addedasync@~0.9
+ Addedasync@0.9.2(transitive)