Skip to content

Commit a7cad0b

Browse files
committed
Merge pull request #58 from browserstack/bug_fixes
Bug fixes
2 parents c73bd61 + 1c335a3 commit a7cad0b

File tree

9 files changed

+115
-64
lines changed

9 files changed

+115
-64
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ tmp
22
node_modules
33
browserstack.json
44
browserstack-runner.pid
5+
lib/BrowserStackLocal

bin/runner.js

Lines changed: 98 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
var BrowserStack = require('browserstack'),
44
fs = require('fs'),
5+
chalk = require('chalk'),
56
utils = require('../lib/utils'),
67
Server = require('../lib/server').Server,
78
config = require('../lib/config'),
89
Tunnel = require('../lib/local').Tunnel,
910
ConfigParser = require('../lib/configParser').ConfigParser,
1011
serverPort = 8888,
12+
timeout = null,
13+
activityTimeout = null,
1114
tunnel;
1215

1316
var client = BrowserStack.createClient({
@@ -19,7 +22,8 @@ var pid_file = process.cwd() + '/browserstack-run.pid';
1922
fs.writeFileSync(pid_file, process.pid, 'utf-8')
2023

2124
var workers = {};
22-
var cleanUp = function cleanUp () {
25+
var workerKeys = {};
26+
var cleanUp = function(signal) {
2327
try {
2428
server.close();
2529
} catch (e) {
@@ -29,33 +33,39 @@ var cleanUp = function cleanUp () {
2933
console.log("Exiting");
3034

3135
for (var key in workers) {
36+
var worker = workers[key];
3237
if (workers.hasOwnProperty(key)) {
33-
client.terminateWorker(workers[key].id, function () {
38+
client.terminateWorker(worker.id, function () {
3439
if (!workers[key]) {
3540
return;
3641
}
3742

38-
console.log('[%s] Terminated', workers[key].string);
39-
clearTimeout(workers[key].activityTimeout);
40-
delete workers[key]
43+
console.log('[%s] Terminated', worker.string);
44+
clearTimeout(worker.activityTimeout);
45+
delete workers[key];
46+
delete workerKeys[worker.id];
4147
});
4248
}
4349
}
50+
if (statusPoller) statusPoller.stop();
4451

4552
try {
4653
process.kill(tunnel.process.pid, 'SIGKILL');
4754
} catch (e) {
4855
console.log("Non existent tunnel");
4956
}
5057
try {
51-
fs.unlink(pid_file);
58+
fs.unlinkSync(pid_file);
5259
} catch (e) {
5360
console.log("Non existent pid file.");
5461
}
62+
if (signal) {
63+
process.kill(process.pid, 'SIGTERM');
64+
}
5565
};
5666

57-
process.on('exit', cleanUp);
58-
process.on('SIGINT', cleanUp);
67+
process.on('exit', function() {cleanUp(false)});
68+
process.on('SIGINT', function() {cleanUp(true)});
5969

6070
console.log("Launching server on port:", serverPort);
6171

@@ -88,13 +98,13 @@ function launchBrowser(browser, url) {
8898
browser["tunnel_identifier"] = config.tunnelIdentifier;
8999
}
90100

91-
var timeout = parseInt(config.timeout);
101+
timeout = parseInt(config.timeout);
92102
if(! isNaN(timeout)) {
93103
browser.timeout = timeout;
94104
} else {
95105
timeout = 300;
96106
}
97-
var activityTimeout = timeout - 10;
107+
activityTimeout = timeout - 10;
98108

99109
client.createWorker(browser, function (err, worker) {
100110
if (err || typeof worker !== 'object') {
@@ -110,47 +120,9 @@ function launchBrowser(browser, url) {
110120
worker.config = browser;
111121
worker.string = browserString;
112122
workers[key] = worker;
113-
114-
var statusPoller = setInterval(function () {
115-
client.getWorker(worker.id, function (err, _worker) {
116-
if (worker.launched) {
117-
return;
118-
}
119-
120-
if (_worker.status === 'running') {
121-
clearInterval(statusPoller);
122-
console.log('[%s] Launched', worker.string);
123-
worker.launched = true;
124-
125-
worker.activityTimeout = setTimeout(function () {
126-
if (!worker.acknowledged) {
127-
var subject = "Worker inactive for too long: " + worker.string;
128-
var content = "Worker details:\n" + JSON.stringify(worker.config, null, 4);
129-
client.takeScreenshot(worker.id, function(error, screenshot) {
130-
if (!error && screenshot.url) {
131-
console.log('[%s] Screenshot: %s', worker.string, screenshot.url);
132-
}
133-
utils.alertBrowserStack(subject, content);
134-
});
135-
}
136-
}, activityTimeout * 1000);
137-
138-
setTimeout(function () {
139-
if (workers[key]) {
140-
var subject = "Tests timed out on: " + worker.string;
141-
var content = "Worker details:\n" + JSON.stringify(worker.config, null, 4);
142-
client.takeScreenshot(worker.id, function(error, screenshot) {
143-
if (!error && screenshot.url) {
144-
console.log('[%s] Screenshot: %s', worker.string, screenshot.url);
145-
}
146-
utils.alertBrowserStack(subject, content);
147-
});
148-
}
149-
}, (activityTimeout * 1000));
150-
}
151-
});
152-
}, 2000);
123+
workerKeys[worker.id] = {key: key, marked: false};
153124
});
125+
154126
}
155127

156128
var launchBrowsers = function(config, browser) {
@@ -167,6 +139,81 @@ var launchBrowsers = function(config, browser) {
167139
}, 100);
168140
}
169141

142+
var statusPoller = {
143+
poller: null,
144+
145+
start: function() {
146+
statusPoller.poller = setInterval(function () {
147+
client.getWorkers(function (err, _workers) {
148+
_workers = _workers.filter(function(currentValue, index, array) {
149+
return currentValue.status == 'running' && workerKeys[currentValue.id] && !workerKeys[currentValue.id].marked;
150+
});
151+
for (var i in _workers) {
152+
var _worker = _workers[i];
153+
var workerData = workerKeys[_worker.id];
154+
var worker = workers[workerData.key];
155+
if (worker.launched) {
156+
return;
157+
}
158+
159+
if (_worker.status === 'running') {
160+
//clearInterval(statusPoller);
161+
console.log('[%s] Launched', worker.string);
162+
worker.launched = true;
163+
workerData.marked = true;
164+
165+
worker.activityTimeout = setTimeout(function () {
166+
if (!worker.acknowledged) {
167+
var subject = "Worker inactive for too long: " + worker.string;
168+
var content = "Worker details:\n" + JSON.stringify(worker.config, null, 4);
169+
utils.alertBrowserStack(subject, content, null, function(){});
170+
delete workers[workerData.key];
171+
delete workerKeys[worker.id];
172+
config.status += 1;
173+
if (utils.objectSize(workers) === 0) {
174+
var color = config.status > 0 ? "red" : "green";
175+
console.log(chalk[color]("All tests done, failures: %d."), config.status);
176+
177+
if (config.status > 0) {
178+
config.status = 1;
179+
}
180+
181+
process.exit(config.status);
182+
}
183+
}
184+
}, activityTimeout * 1000);
185+
186+
setTimeout(function () {
187+
if (worker.acknowledged) {
188+
var subject = "Tests timed out on: " + worker.string;
189+
var content = "Worker details:\n" + JSON.stringify(worker.config, null, 4);
190+
utils.alertBrowserStack(subject, content, null, function(){});
191+
delete workers[workerData.key];
192+
delete workerKeys[worker.id];
193+
config.status += 1;
194+
if (utils.objectSize(workers) === 0) {
195+
var color = config.status > 0 ? "red" : "green";
196+
console.log(chalk[color]("All tests done, failures: %d."), config.status);
197+
198+
if (config.status > 0) {
199+
config.status = 1;
200+
}
201+
202+
process.exit(config.status);
203+
}
204+
}
205+
}, (activityTimeout * 1000));
206+
}
207+
}
208+
});
209+
}, 2000);
210+
},
211+
212+
stop: function() {
213+
clearInterval(statusPoller.poller);
214+
}
215+
};
216+
170217
if (config.browsers && config.browsers.length > 0) {
171218
ConfigParser.parse(client, config.browsers, function(browsers){
172219
tunnel = new Tunnel(config.key, serverPort, config.tunnelIdentifier, function () {
@@ -187,5 +234,6 @@ if (config.browsers && config.browsers.length > 0) {
187234
}
188235
});
189236
});
237+
statusPoller.start();
190238
});
191239
}

lib/_patch/browserstack.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
req.setRequestHeader('X-Browser-String', BrowserStack.browser_string);
3030
req.setRequestHeader('X-Worker-UUID', BrowserStack.worker_uuid);
3131
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
32-
req.setRequestHeader('Content-length', data.length);
33-
req.setRequestHeader('Connection', 'close');
3432
req.send(data);
3533
}
3634

lib/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ if (Object.prototype.toString.call(config.test_path) === '[object Array]') {
7878
config.test_path = formatPath(config.test_path);
7979
}
8080

81+
config.status = 0;
82+
8183
for (var key in config) {
8284
if (config.hasOwnProperty(key)) {
8385
exports[key] = config[key];

lib/configParser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var ConfigParser = {
3232
}).sort(function(a, b) {
3333
return parseFloat(a) - parseFloat(b);
3434
});
35-
if (verStr == 'current') {
35+
if (verStr == 'current' || verStr == 'latest') {
3636
return filteredBrowsers[filteredBrowsers.length - 1];
3737
}
3838
else if (verStr == 'previous') {
@@ -82,7 +82,7 @@ var ConfigParser = {
8282
browserObject.browser = browserData[0];
8383
}
8484
if (browserData[lindex] && browserData[lindex].indexOf("+") == -1) {
85-
if (["current", "previous"].indexOf(browserData[lindex]) != -1) {
85+
if (["current", "previous", "latest"].indexOf(browserData[lindex]) != -1) {
8686
version = ConfigParser.setBrowserVersion(browserObject, browserData[lindex]);
8787
}
8888
else {

lib/local.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback, err) {
2626
var subProcess = exec(tunnelCommand, function(error, stdout, stderr) {
2727
console.log(stderr);
2828
console.log(error);
29-
if (stdout.indexOf('Error') >= 0) {
29+
if (stdout.indexOf('Error') >= 0 || error) {
3030
console.log("[%s] Tunnel launching failed", new Date());
3131
console.log(stdout);
3232
process.exit(1);
@@ -78,6 +78,9 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback, err) {
7878
setTimeout(function() {
7979
tunnelLauncher();
8080
}, 100);
81+
}).on('error', function(e) {
82+
console.log("Got error while downloading binary: " + e.message);
83+
process.exit(1);
8184
});
8285
});
8386
});

lib/server.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ var mimeTypes = {
2020

2121

2222
exports.Server = function Server(bsClient, workers) {
23-
var status = 0;
2423

2524
function handleFile(filename, request, response) {
2625
var url_parts = url.parse(request.url, true);
@@ -171,7 +170,7 @@ exports.Server = function Server(bsClient, workers) {
171170
}
172171
var color = query.failed ? "red" : "green";
173172
console.log(chalk[color]("[%s] Completed in %d milliseconds. %d of %d passed, %d failed."), request.headers['x-browser-string'], query.runtime, query.passed, query.total, query.failed);
174-
status += query.failed;
173+
config.status += query.failed;
175174
}
176175

177176
if (worker) {
@@ -191,14 +190,14 @@ exports.Server = function Server(bsClient, workers) {
191190
delete workers[uuid];
192191

193192
if (utils.objectSize(workers) === 0) {
194-
var color = status > 0 ? "red" : "green";
195-
console.log(chalk[color]("All tests done, failures: %d."), status);
193+
var color = config.status > 0 ? "red" : "green";
194+
console.log(chalk[color]("All tests done, failures: %d."), config.status);
196195

197-
if (status > 0) {
198-
status = 1;
196+
if (config.status > 0) {
197+
config.status = 1;
199198
}
200199

201-
process.exit(status);
200+
process.exit(config.status);
202201
}
203202
});
204203
});

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var alertBrowserStack = function alertBrowserStack(subject, content, params, fn)
5252
}
5353
}
5454

55-
if (typeof params !== 'object') {
55+
if (!params || typeof(params) !== 'object') {
5656
params = {};
5757
}
5858

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"homepage": "https://github.com/browserstack/browserstack-runner",
66
"repository": {
77
"type": "git",
8-
"url": "git://github.com/browserstack/browserstack-runner.git"
8+
"url": "https://github.com/browserstack/browserstack-runner.git"
99
},
1010
"dependencies": {
1111
"browserstack": "1.0.1",

0 commit comments

Comments
 (0)