Skip to content

Commit ffb41f9

Browse files
authored
refactor(browser): log state transitions in debug (karma-runner#3202)
1 parent 240209f commit ffb41f9

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

lib/browser.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ const BrowserResult = require('./browser_result')
44
const helper = require('./helper')
55
const logger = require('./logger')
66

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

1313
class Browser {
1414
constructor (id, fullName, collection, emitter, socket, timer, disconnectDelay, noActivityTimeout) {
@@ -40,6 +40,11 @@ class Browser {
4040
this.emitter.emit('browser_register', this)
4141
}
4242

43+
setState (toState) {
44+
this.log.debug(`${this.state} -> ${toState}`)
45+
this.state = toState
46+
}
47+
4348
onKarmaError (error) {
4449
if (this.isNotConnected()) {
4550
this.lastResult.error = true
@@ -70,14 +75,14 @@ class Browser {
7075
}
7176

7277
this.lastResult = new BrowserResult(info.total)
73-
this.state = EXECUTING
78+
this.setState(EXECUTING)
7479
this.emitter.emit('browser_start', this, info)
7580
this.refreshNoActivityTimeout()
7681
}
7782

7883
onComplete (result) {
7984
if (this.isNotConnected()) {
80-
this.state = CONNECTED
85+
this.setState(CONNECTED)
8186
this.lastResult.totalTimeEnd()
8287

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

108113
this.pendingDisconnect = this.timer.setTimeout(() => {
109114
this.lastResult.totalTimeEnd()
@@ -119,12 +124,12 @@ class Browser {
119124
reconnect (newSocket) {
120125
if (this.state === EXECUTING_DISCONNECTED) {
121126
this.log.debug(`Reconnected on ${newSocket.id}.`)
122-
this.state = EXECUTING
127+
this.setState(EXECUTING)
123128
} else if ([CONNECTED, CONFIGURING, EXECUTING].includes(this.state)) {
124129
this.log.debug(`New connection ${newSocket.id} (already have ${this.getActiveSocketsIds()})`)
125130
} else if (this.state === DISCONNECTED) {
126131
this.log.info(`Connected on socket ${newSocket.id} with id ${this.id}`)
127-
this.state = CONNECTED
132+
this.setState(CONNECTED)
128133

129134
this.collection.add(this)
130135
this.emitter.emit('browser_register', this)
@@ -155,7 +160,7 @@ class Browser {
155160

156161
execute (config) {
157162
this.activeSockets.forEach((socket) => socket.emit('execute', config))
158-
this.state = CONFIGURING
163+
this.setState(CONFIGURING)
159164
this.refreshNoActivityTimeout()
160165
}
161166

@@ -165,7 +170,7 @@ class Browser {
165170

166171
disconnect (reason) {
167172
this.log.warn(`Disconnected (${this.disconnectsCount} times)${reason || ''}`)
168-
this.state = DISCONNECTED
173+
this.setState(DISCONNECTED)
169174
this.disconnectsCount++
170175
this.emitter.emit('browser_error', this, `Disconnected${reason || ''}`)
171176
this.collection.remove(this)

0 commit comments

Comments
 (0)