Skip to content

Commit

Permalink
refactor(browser): log state transitions in debug (#3202)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjbarton committed Nov 6, 2018
1 parent 240209f commit ffb41f9
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/browser.js
Expand Up @@ -4,11 +4,11 @@ const BrowserResult = require('./browser_result')
const helper = require('./helper')
const logger = require('./logger')

const CONNECTED = 1 // The browser is connected but not yet been commanded to execute tests.
const CONFIGURING = 2 // The browser has been told to execute tests; it is configuring before tests execution.
const EXECUTING = 3 // The browser is executing the tests.
const EXECUTING_DISCONNECTED = 4 // The browser is executing the tests, but temporarily disconnect (waiting for reconnecting).
const DISCONNECTED = 5 // The browser got permanently disconnected (being removed from the collection and destroyed).
const CONNECTED = 'CONNECTED' // The browser is connected but not yet been commanded to execute tests.
const CONFIGURING = 'CONFIGURING' // The browser has been told to execute tests; it is configuring before tests execution.
const EXECUTING = 'EXECUTING' // The browser is executing the tests.
const EXECUTING_DISCONNECTED = 'EXECUTING_DISCONNECTED' // The browser is executing the tests, but temporarily disconnect (waiting for reconnecting).
const DISCONNECTED = 'DISCONNECTED' // The browser got permanently disconnected (being removed from the collection and destroyed).

class Browser {
constructor (id, fullName, collection, emitter, socket, timer, disconnectDelay, noActivityTimeout) {
Expand Down Expand Up @@ -40,6 +40,11 @@ class Browser {
this.emitter.emit('browser_register', this)
}

setState (toState) {
this.log.debug(`${this.state} -> ${toState}`)
this.state = toState
}

onKarmaError (error) {
if (this.isNotConnected()) {
this.lastResult.error = true
Expand Down Expand Up @@ -70,14 +75,14 @@ class Browser {
}

this.lastResult = new BrowserResult(info.total)
this.state = EXECUTING
this.setState(EXECUTING)
this.emitter.emit('browser_start', this, info)
this.refreshNoActivityTimeout()
}

onComplete (result) {
if (this.isNotConnected()) {
this.state = CONNECTED
this.setState(CONNECTED)
this.lastResult.totalTimeEnd()

if (!this.lastResult.success) {
Expand All @@ -103,7 +108,7 @@ class Browser {
this.disconnect(`Client disconnected from CONNECTED state (${reason})`)
} else if ([CONFIGURING, EXECUTING].includes(this.state)) {
this.log.debug(`Disconnected during run, waiting ${this.disconnectDelay}ms for reconnecting.`)
this.state = EXECUTING_DISCONNECTED
this.setState(EXECUTING_DISCONNECTED)

this.pendingDisconnect = this.timer.setTimeout(() => {
this.lastResult.totalTimeEnd()
Expand All @@ -119,12 +124,12 @@ class Browser {
reconnect (newSocket) {
if (this.state === EXECUTING_DISCONNECTED) {
this.log.debug(`Reconnected on ${newSocket.id}.`)
this.state = EXECUTING
this.setState(EXECUTING)
} else if ([CONNECTED, CONFIGURING, EXECUTING].includes(this.state)) {
this.log.debug(`New connection ${newSocket.id} (already have ${this.getActiveSocketsIds()})`)
} else if (this.state === DISCONNECTED) {
this.log.info(`Connected on socket ${newSocket.id} with id ${this.id}`)
this.state = CONNECTED
this.setState(CONNECTED)

this.collection.add(this)
this.emitter.emit('browser_register', this)
Expand Down Expand Up @@ -155,7 +160,7 @@ class Browser {

execute (config) {
this.activeSockets.forEach((socket) => socket.emit('execute', config))
this.state = CONFIGURING
this.setState(CONFIGURING)
this.refreshNoActivityTimeout()
}

Expand All @@ -165,7 +170,7 @@ class Browser {

disconnect (reason) {
this.log.warn(`Disconnected (${this.disconnectsCount} times)${reason || ''}`)
this.state = DISCONNECTED
this.setState(DISCONNECTED)
this.disconnectsCount++
this.emitter.emit('browser_error', this, `Disconnected${reason || ''}`)
this.collection.remove(this)
Expand Down

0 comments on commit ffb41f9

Please sign in to comment.