Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(browser): log state transitions in debug #3202

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions lib/browser.js
Original file line number Diff line number Diff line change
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