Skip to content

Commit af6b40b

Browse files
committed
Reuse worker initial commit
1 parent 19d51dd commit af6b40b

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

bin/cli.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var Log = require('../lib/logger'),
3232
timeout,
3333
activityTimeout,
3434
workers = {},
35+
browserToWorker = {},
3536
workerKeys = {},
3637
logLevel,
3738
tunnelingAgent,
@@ -176,9 +177,13 @@ function launchBrowsers(config, browser) {
176177
setTimeout(function () {
177178
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+
if(config.reuseWorker) {
181+
launchBrowser(browser, config.test_path[0]);
182+
} else {
183+
config.test_path.forEach(function(path){
184+
launchBrowser(browser, path);
185+
});
186+
}
182187
} else {
183188
config.multipleTest = false;
184189
launchBrowser(browser, config.test_path);

lib/server.js

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

160+
function checkAndTerminateWorker(worker, callback) {
161+
if(config.reuseWorker && config.multipleTest) {
162+
var next_path = getNextTestPath(worker.test_path);
163+
var new_url = 'http://localhost:' + 8888 + '/' + next_path
164+
+ "_worker_key=" + worker._worker_key + "&_browser_string=" + getTestBrowserInfo(worker) ;
165+
bsClient.postNewUrl(worker.id, {url: new_url}, function() {
166+
callback(true);
167+
});
168+
} else {
169+
bsClient.terminateWorker(worker.id, callback);
170+
}
171+
};
172+
173+
function getNextTestPath(test_path) {
174+
// Someday I'll die for codes like these
175+
for(var i = 0; i < config.test_path.length; i++) {
176+
if(config.test_path[i] == test_path) {
177+
return config.test_path[ i + 1 ];
178+
}
179+
}
180+
181+
}
182+
160183
handlers = {
161184
"_progress": function progressHandler(uri, body, request, response) {
162185
var uuid = request.headers['x-worker-uuid'];
@@ -184,6 +207,7 @@ exports.Server = function Server(bsClient, workers) {
184207
} catch (e) {}
185208
var uuid = request.headers['x-worker-uuid'];
186209
var worker = workers[uuid] || {};
210+
worker._worker_key = uuid;
187211

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

208-
bsClient.terminateWorker(worker.id, function() {
232+
checkAndTerminateWorker(worker, function(reusedWorker) {
209233
if (!workers[uuid]) {
210234
return;
211235
}
212236

237+
if(reusedWorker) {
238+
logger.debug('[%s] Reused', getTestBrowserInfo(worker));
239+
return;
240+
}
241+
213242
logger.debug('[%s] Terminated', getTestBrowserInfo(worker));
214243

215244
clearTimeout(workers[uuid].activityTimeout);

0 commit comments

Comments
 (0)