Skip to content

Commit 2b7d703

Browse files
fix(launcher): Allow dynamic browser launches
1 parent 8ef475f commit 2b7d703

4 files changed

Lines changed: 131 additions & 92 deletions

File tree

lib/launcher.js

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var Promise = require('bluebird')
2-
var Batch = require('batch')
2+
var Jobs = require('qjobs')
33

44
var helper = require('./helper')
55
var log = require('./logger').create('launcher')
@@ -25,27 +25,25 @@ var baseBrowserDecoratorFactory = function (
2525
}
2626

2727
var Launcher = function (server, emitter, injector) {
28-
var browsers = []
28+
this._browsers = []
2929
var lastStartTime
30+
var self = this
3031

3132
var getBrowserById = function (id) {
32-
for (var i = 0; i < browsers.length; i++) {
33-
if (browsers[i].id === id) {
34-
return browsers[i]
33+
for (var i = 0; i < self._browsers.length; i++) {
34+
if (self._browsers[i].id === id) {
35+
return self._browsers[i]
3536
}
3637
}
3738

3839
return null
3940
}
4041

41-
this.launch = function (names, protocol, hostname, port, urlRoot, concurrency) {
42-
var url = protocol + '//' + hostname + ':' + port + urlRoot
43-
var batch = new Batch()
44-
batch.concurrency(concurrency)
42+
this.launchSingle = function (protocol, hostname, port, urlRoot) {
43+
var self = this
44+
return function (name) {
45+
var url = protocol + '//' + hostname + ':' + port + urlRoot
4546

46-
lastStartTime = Date.now()
47-
48-
names.forEach(function (name) {
4947
var locals = {
5048
id: ['value', Launcher.generateId()],
5149
name: ['value', name],
@@ -75,29 +73,26 @@ var Launcher = function (server, emitter, injector) {
7573
return
7674
}
7775

78-
if (server.loadErrors.length > 0) return []
7976
// TODO(vojta): remove in v1.0 (BC for old launchers)
8077
if (!browser.forceKill) {
8178
browser.forceKill = function () {
82-
var self = this
83-
79+
var me = this
8480
return new Promise(function (resolve) {
85-
self.kill(resolve)
81+
me.kill(resolve)
8682
})
8783
}
8884

8985
browser.restart = function () {
90-
var self = this
86+
var me = this
9187
this.kill(function () {
92-
self.start(url)
88+
me.start(url)
9389
})
9490
}
9591
}
9692

97-
batch.push(function (done) {
93+
self.jobs.add(function (args, done) {
9894
log.info('Starting browser %s', helper.isDefined(browser.displayName) ? browser.displayName : browser.name)
9995

100-
browser.start(url)
10196
browser.on('browser_process_failure', function () {
10297
done(browser.error)
10398
})
@@ -109,29 +104,56 @@ var Launcher = function (server, emitter, injector) {
109104

110105
done(null, browser)
111106
})
112-
})
113107

114-
browsers.push(browser)
115-
})
108+
browser.start(url)
109+
}, [])
110+
111+
self.jobs.run()
112+
self._browsers.push(browser)
113+
}
114+
}
115+
116+
this.launch = function (names, concurrency) {
117+
log.info('Launching', names, concurrency)
118+
this.jobs = new Jobs({maxConcurrency: concurrency})
116119

117-
batch.end(function (err) {
120+
var self = this
121+
lastStartTime = Date.now()
122+
123+
if (server.loadErrors.length === 0) {
124+
names.forEach(function (name) {
125+
injector.invoke(self.launchSingle, self)(name)
126+
})
127+
} else {
128+
// Empty task to ensure `end` is emitted
129+
this.jobs.add(function (args, done) {
130+
done()
131+
}, [])
132+
}
133+
134+
this.jobs.on('end', function (err) {
118135
log.debug('Finished all browsers')
119136

120137
if (err) {
121138
log.error(err)
122139
}
123140
})
124141

125-
return browsers
142+
this.jobs.run()
143+
144+
return self._browsers
126145
}
127146

128147
this.launch.$inject = [
129148
'config.browsers',
149+
'config.concurrency'
150+
]
151+
152+
this.launchSingle.$inject = [
130153
'config.protocol',
131154
'config.hostname',
132155
'config.port',
133-
'config.urlRoot',
134-
'config.concurrency'
156+
'config.urlRoot'
135157
]
136158

137159
this.kill = function (id, callback) {
@@ -169,24 +191,24 @@ var Launcher = function (server, emitter, injector) {
169191
}
170192
}
171193

172-
if (!browsers.length) {
194+
if (!self._browsers.length) {
173195
return process.nextTick(callback)
174196
}
175197

176-
browsers.forEach(function (browser) {
198+
self._browsers.forEach(function (browser) {
177199
remaining++
178200
browser.forceKill().then(finish)
179201
})
180202
}
181203

182204
this.areAllCaptured = function () {
183-
return !browsers.some(function (browser) {
205+
return !self._browsers.some(function (browser) {
184206
return !browser.isCaptured()
185207
})
186208
}
187209

188210
this.markCaptured = function (id) {
189-
browsers.forEach(function (browser) {
211+
self._browsers.forEach(function (browser) {
190212
if (browser.id === id) {
191213
browser.markCaptured()
192214
log.debug('%s (id %s) captured in %d secs', browser.name, browser.id,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@
261261
"Karolis Narkevicius <[email protected]>"
262262
],
263263
"dependencies": {
264-
"batch": "^0.5.3",
265264
"bluebird": "^3.3.0",
266265
"body-parser": "^1.12.4",
267266
"chokidar": "^1.4.1",
@@ -281,6 +280,7 @@
281280
"mime": "^1.3.4",
282281
"minimatch": "^3.0.0",
283282
"optimist": "^0.6.1",
283+
"qjobs": "^1.1.4",
284284
"rimraf": "^2.3.3",
285285
"socket.io": "^1.4.5",
286286
"source-map": "^0.5.3",

test/e2e/load.feature

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,5 @@ Feature: Basic Testrunner
9898
"""
9999
And it fails with like:
100100
"""
101-
Cannot load browser "NonExistingBrowser": it is not registered! Perhaps you are missing some plugin\?
102-
"""
103-
And it fails with like:
104-
"""
105-
Found 3 load errors
101+
Found 2 load errors
106102
"""

0 commit comments

Comments
 (0)