Skip to content

Commit 7bf042c

Browse files
committed
Merge pull request #93 from jzaefferer/reuse-worker
Reuse worker
2 parents 19d51dd + 8f936a4 commit 7bf042c

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

bin/cli.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function cleanUpAndExit(signal, status) {
104104

105105
function getTestBrowserInfo(browserString, path) {
106106
var info = browserString;
107-
if(config.multipleTest) {
107+
if (config.multipleTest) {
108108
info += ", " + path;
109109
}
110110
return info;
@@ -145,7 +145,7 @@ function launchBrowser(browser, path) {
145145
}
146146

147147
timeout = parseInt(config.timeout);
148-
if(! isNaN(timeout)) {
148+
if (!isNaN(timeout)) {
149149
browser.timeout = timeout;
150150
} else {
151151
timeout = 300;
@@ -166,6 +166,7 @@ function launchBrowser(browser, path) {
166166
worker.config = browser;
167167
worker.string = browserString;
168168
worker.test_path = path;
169+
worker.path_index = 0;
169170
workers[key] = worker;
170171
workerKeys[worker.id] = {key: key, marked: false};
171172
});
@@ -174,11 +175,9 @@ function launchBrowser(browser, path) {
174175

175176
function launchBrowsers(config, browser) {
176177
setTimeout(function () {
177-
if(Object.prototype.toString.call(config.test_path) === '[object Array]'){
178+
if (Object.prototype.toString.call(config.test_path) === '[object Array]'){
178179
config.multipleTest = config.test_path.length > 1? true : false;
179-
config.test_path.forEach(function(path){
180-
launchBrowser(browser, path);
181-
});
180+
launchBrowser(browser, config.test_path[0]);
182181
} else {
183182
config.multipleTest = false;
184183
launchBrowser(browser, config.test_path);
@@ -277,8 +276,8 @@ function runTests() {
277276
launchServer();
278277
tunnel = new Tunnel(config.key, serverPort, config.tunnelIdentifier, function () {
279278
statusPoller.start();
280-
var total_workers = config.browsers.length * (Object.prototype.toString.call(config.test_path) === '[object Array]' ? config.test_path.length : 1);
281-
logger.info("Launching " + total_workers + " workers");
279+
var total_runs = config.browsers.length * (Object.prototype.toString.call(config.test_path) === '[object Array]' ? config.test_path.length : 1);
280+
logger.info("Launching " + config.browsers.length + " worker(s) for " + total_runs + " run(s).");
282281
browsers.forEach(function(browser) {
283282
if (browser.browser_version === "latest") {
284283
logger.debug("[%s] Finding version.", utils.browserString(browser));

lib/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ var commit_id = process.env.TRAVIS_COMMIT;
4949

5050
if (commit_id) {
5151
config.build = "Commit-" + commit_id.slice(0, commit_id.length / 2);
52+
} {
53+
config.build = "Local run, " + new Date().toISOString();
5254
}
5355

5456
['username', 'key', 'test_path', 'browsers'].forEach(function(param) {

lib/server.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,27 @@ exports.Server = function Server(bsClient, workers) {
157157
return details;
158158
}
159159

160+
function checkAndTerminateWorker(worker, callback) {
161+
var next_path = getNextTestPath(worker);
162+
if (next_path) {
163+
var new_url = 'http://localhost:' + 8888 + '/' + next_path
164+
+ "?_worker_key=" + worker._worker_key + "&_browser_string=" + getTestBrowserInfo(worker) ;
165+
worker.test_path = next_path;
166+
bsClient.changeUrl(worker.id, {url: new_url}, function() {
167+
callback(true);
168+
});
169+
} else {
170+
bsClient.terminateWorker(worker.id, callback);
171+
}
172+
};
173+
174+
function getNextTestPath(worker) {
175+
if (!config.multipleTest) {
176+
return null;
177+
}
178+
return config.test_path[ ++worker.path_index ];
179+
}
180+
160181
handlers = {
161182
"_progress": function progressHandler(uri, body, request, response) {
162183
var uuid = request.headers['x-worker-uuid'];
@@ -184,6 +205,7 @@ exports.Server = function Server(bsClient, workers) {
184205
} catch (e) {}
185206
var uuid = request.headers['x-worker-uuid'];
186207
var worker = workers[uuid] || {};
208+
worker._worker_key = uuid;
187209

188210
if (query === null) {
189211
logger.info("[%s] Null response from remote Browser", request.headers['x-browser-string']);
@@ -205,11 +227,16 @@ exports.Server = function Server(bsClient, workers) {
205227
logger.info('[%s] ' + chalk['yellow']('Screenshot') + ': %s', getTestBrowserInfo(worker), screenshot.url);
206228
}
207229

208-
bsClient.terminateWorker(worker.id, function() {
230+
checkAndTerminateWorker(worker, function(reusedWorker) {
209231
if (!workers[uuid]) {
210232
return;
211233
}
212234

235+
if (reusedWorker) {
236+
logger.debug('[%s] Reused', getTestBrowserInfo(worker));
237+
return;
238+
}
239+
213240
logger.debug('[%s] Terminated', getTestBrowserInfo(worker));
214241

215242
clearTimeout(workers[uuid].activityTimeout);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"url": "https://github.com/browserstack/browserstack-runner.git"
99
},
1010
"dependencies": {
11-
"browserstack": "1.0.1",
11+
"browserstack": "1.1.0",
1212
"chalk": "0.4.0",
1313
"tunnel": "0.0.3"
1414
},

0 commit comments

Comments
 (0)