Skip to content

Commit

Permalink
Merge 361e541 into b8b2ed8
Browse files Browse the repository at this point in the history
  • Loading branch information
devoto13 authored Mar 21, 2020
2 parents b8b2ed8 + 361e541 commit a064a32
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
92 changes: 54 additions & 38 deletions lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,37 @@ const baseBrowserDecoratorFactory = function (
}
}

function Launcher (server, emitter, injector) {
this._browsers = []
let lastStartTime
class Launcher {
constructor (server, emitter, injector) {
this._server = server
this._emitter = emitter
this._injector = injector
this._browsers = []
this._lastStartTime = null

// Attach list of dependency injection parameters to methods.
this.launch.$inject = [
'config.browsers',
'config.concurrency'
]

this.launchSingle.$inject = [
'config.protocol',
'config.hostname',
'config.port',
'config.urlRoot',
'config.upstreamProxy',
'config.processKillTimeout'
]

this._emitter.on('exit', (callback) => this.killAll(callback))
}

const getBrowserById = (id) => this._browsers.find((browser) => browser.id === id)
getBrowserById (id) {
return this._browsers.find((browser) => browser.id === id)
}

this.launchSingle = (protocol, hostname, port, urlRoot, upstreamProxy, processKillTimeout) => {
launchSingle (protocol, hostname, port, urlRoot, upstreamProxy, processKillTimeout) {
if (upstreamProxy) {
protocol = upstreamProxy.protocol
hostname = upstreamProxy.hostname
Expand All @@ -58,15 +82,15 @@ function Launcher (server, emitter, injector) {
}

try {
browser = injector.createChild([locals], ['launcher:' + name]).get('launcher:' + name)
browser = this._injector.createChild([locals], ['launcher:' + name]).get('launcher:' + name)
} catch (e) {
if (e.message.includes(`No provider for "launcher:${name}"`)) {
log.error(`Cannot load browser "${name}": it is not registered! Perhaps you are missing some plugin?`)
} else {
log.error(`Cannot load browser "${name}"!\n ` + e.stack)
}

emitter.emit('load_error', 'launcher', name)
this._emitter.emit('load_error', 'launcher', name)
return
}

Expand All @@ -89,16 +113,16 @@ function Launcher (server, emitter, injector) {
}
}

this.launch = (names, concurrency) => {
launch (names, concurrency) {
log.info(`Launching browsers ${names.join(', ')} with concurrency ${concurrency === Infinity ? 'unlimited' : concurrency}`)
this.jobs = new Jobs({ maxConcurrency: concurrency })

lastStartTime = Date.now()
this._lastStartTime = Date.now()

if (server.loadErrors.length) {
if (this._server.loadErrors.length) {
this.jobs.add((args, done) => done(), [])
} else {
names.forEach((name) => injector.invoke(this.launchSingle, this)(name))
names.forEach((name) => this._injector.invoke(this.launchSingle, this)(name))
}

this.jobs.on('end', (err) => {
Expand All @@ -114,24 +138,9 @@ function Launcher (server, emitter, injector) {
return this._browsers
}

this.launch.$inject = [
'config.browsers',
'config.concurrency',
'config.processKillTimeout'
]

this.launchSingle.$inject = [
'config.protocol',
'config.hostname',
'config.port',
'config.urlRoot',
'config.upstreamProxy',
'config.processKillTimeout'
]

this.kill = (id, callback) => {
kill (id, callback) {
callback = callback || function () {}
const browser = getBrowserById(id)
const browser = this.getBrowserById(id)

if (browser) {
browser.forceKill().then(callback)
Expand All @@ -141,16 +150,16 @@ function Launcher (server, emitter, injector) {
return false
}

this.restart = (id) => {
const browser = getBrowserById(id)
restart (id) {
const browser = this.getBrowserById(id)
if (browser) {
browser.restart()
return true
}
return false
}

this.killAll = (callback) => {
killAll (callback) {
callback = callback || function () {}
log.debug('Disconnecting all browsers')

Expand All @@ -164,20 +173,27 @@ function Launcher (server, emitter, injector) {
).then(callback)
}

this.areAllCaptured = () => this._browsers.every((browser) => browser.isCaptured())
areAllCaptured () {
return this._browsers.every((browser) => browser.isCaptured())
}

this.markCaptured = (id) => {
const browser = getBrowserById(id)
markCaptured (id) {
const browser = this.getBrowserById(id)
if (browser) {
browser.markCaptured()
log.debug(`${browser.name} (id ${browser.id}) captured in ${(Date.now() - lastStartTime) / 1000} secs`)
log.debug(`${browser.name} (id ${browser.id}) captured in ${(Date.now() - this._lastStartTime) / 1000} secs`)
}
}

emitter.on('exit', this.killAll)
static generateId () {
return Math.floor(Math.random() * 100000000).toString()
}
}

Launcher.factory = function (server, emitter, injector) {
return new Launcher(server, emitter, injector)
}

Launcher.$inject = ['server', 'emitter', 'injector']
Launcher.generateId = () => Math.floor(Math.random() * 100000000).toString()
Launcher.factory.$inject = ['server', 'emitter', 'injector']

exports.Launcher = Launcher
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Server extends KarmaEventEmitter {
emitter: ['value', this],
server: ['value', this],
watcher: ['value', watcher],
launcher: ['type', Launcher],
launcher: ['factory', Launcher.factory],
config: ['value', config],
preprocess: ['factory', preprocessor.createPriorityPreprocessor],
fileList: ['factory', FileList.factory],
Expand Down

0 comments on commit a064a32

Please sign in to comment.