From ffb41f93f9f29e1f534514e4f1774123fd249726 Mon Sep 17 00:00:00 2001 From: johnjbarton Date: Tue, 6 Nov 2018 07:50:19 -0800 Subject: [PATCH] refactor(browser): log state transitions in debug (#3202) --- lib/browser.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/browser.js b/lib/browser.js index 1301c93a5..8f0bdf610 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -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) { @@ -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 @@ -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) { @@ -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() @@ -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) @@ -155,7 +160,7 @@ class Browser { execute (config) { this.activeSockets.forEach((socket) => socket.emit('execute', config)) - this.state = CONFIGURING + this.setState(CONFIGURING) this.refreshNoActivityTimeout() } @@ -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)