-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
browser.js
252 lines (198 loc) · 6.44 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var helper = require('./helper')
var events = require('./events')
var logger = require('./logger')
var Result = require('./browser_result')
// The browser is ready to execute tests.
var READY = 1
// The browser is executing the tests.
var EXECUTING = 2
// The browser is not executing, but temporarily disconnected (waiting for reconnecting).
var READY_DISCONNECTED = 3
// The browser is executing the tests, but temporarily disconnect (waiting for reconnecting).
var EXECUTING_DISCONNECTED = 4
// The browser got permanently disconnected (being removed from the collection and destroyed).
var DISCONNECTED = 5
var Browser = function (id, fullName, /* capturedBrowsers */ collection, emitter, socket, timer,
/* config.browserDisconnectTimeout */ disconnectDelay,
/* config.browserNoActivityTimeout */ noActivityTimeout) {
var name = helper.browserFullNameToShort(fullName)
var log = logger.create(name)
var activeSockets = [socket]
var activeSocketsIds = function () {
return activeSockets.map(function (s) {
return s.id
}).join(', ')
}
var self = this
var pendingDisconnect
var disconnect = function (reason) {
self.state = DISCONNECTED
self.disconnectsCount++
log.warn('Disconnected (%d times)' + (reason || ''), self.disconnectsCount)
emitter.emit('browser_error', self, 'Disconnected' + reason)
collection.remove(self)
}
var noActivityTimeoutId
var refreshNoActivityTimeout = noActivityTimeout ? function () {
clearNoActivityTimeout()
noActivityTimeoutId = timer.setTimeout(function () {
self.lastResult.totalTimeEnd()
self.lastResult.disconnected = true
disconnect(', because no message in ' + noActivityTimeout + ' ms.')
emitter.emit('browser_complete', self)
}, noActivityTimeout)
} : function () {}
var clearNoActivityTimeout = noActivityTimeout ? function () {
if (noActivityTimeoutId) {
timer.clearTimeout(noActivityTimeoutId)
noActivityTimeoutId = null
}
} : function () {}
this.id = id
this.fullName = fullName
this.name = name
this.state = READY
this.lastResult = new Result()
this.disconnectsCount = 0
this.init = function () {
collection.add(this)
events.bindAll(this, socket)
log.info('Connected on socket %s with id %s', socket.id, id)
// TODO(vojta): move to collection
emitter.emit('browsers_change', collection)
emitter.emit('browser_register', this)
}
this.isReady = function () {
return this.state === READY
}
this.toString = function () {
return this.name
}
this.onKarmaError = function (error) {
if (this.isReady()) {
return
}
this.lastResult.error = true
emitter.emit('browser_error', this, error)
refreshNoActivityTimeout()
}
this.onInfo = function (info) {
if (this.isReady()) {
return
}
// TODO(vojta): remove
if (helper.isDefined(info.dump)) {
emitter.emit('browser_log', this, info.dump, 'dump')
}
if (helper.isDefined(info.log)) {
emitter.emit('browser_log', this, info.log, info.type)
}
if (
!helper.isDefined(info.log) &&
!helper.isDefined(info.dump)
) {
emitter.emit('browser_info', this, info)
}
refreshNoActivityTimeout()
}
this.onStart = function (info) {
this.lastResult = new Result()
this.lastResult.total = info.total
if (info.total === null) {
log.warn('Adapter did not report total number of specs.')
}
emitter.emit('browser_start', this, info)
refreshNoActivityTimeout()
}
this.onComplete = function (result) {
if (this.isReady()) {
return
}
this.state = READY
this.lastResult.totalTimeEnd()
if (!this.lastResult.success) {
this.lastResult.error = true
}
emitter.emit('browsers_change', collection)
emitter.emit('browser_complete', this, result)
clearNoActivityTimeout()
}
this.onDisconnect = function (_, disconnectedSocket) {
activeSockets.splice(activeSockets.indexOf(disconnectedSocket), 1)
if (activeSockets.length) {
log.debug('Disconnected %s, still have %s', disconnectedSocket.id, activeSocketsIds())
return
}
if (this.state === READY) {
disconnect()
} else if (this.state === EXECUTING) {
log.debug('Disconnected during run, waiting %sms for reconnecting.', disconnectDelay)
this.state = EXECUTING_DISCONNECTED
pendingDisconnect = timer.setTimeout(function () {
self.lastResult.totalTimeEnd()
self.lastResult.disconnected = true
disconnect()
emitter.emit('browser_complete', self)
}, disconnectDelay)
clearNoActivityTimeout()
}
}
this.reconnect = function (newSocket) {
if (this.state === EXECUTING_DISCONNECTED) {
this.state = EXECUTING
log.debug('Reconnected on %s.', newSocket.id)
} else if (this.state === EXECUTING || this.state === READY) {
log.debug('New connection %s (already have %s)', newSocket.id, activeSocketsIds())
} else if (this.state === DISCONNECTED) {
this.state = READY
log.info('Connected on socket %s with id %s', newSocket.id, this.id)
collection.add(this)
// TODO(vojta): move to collection
emitter.emit('browsers_change', collection)
emitter.emit('browser_register', this)
}
var exists = activeSockets.some(function (s) {
return s.id === newSocket.id
})
if (!exists) {
activeSockets.push(newSocket)
events.bindAll(this, newSocket)
}
if (pendingDisconnect) {
timer.clearTimeout(pendingDisconnect)
}
refreshNoActivityTimeout()
}
this.onResult = function (result) {
if (result.length) {
return result.forEach(this.onResult, this)
}
// ignore - probably results from last run (after server disconnecting)
if (this.isReady()) {
return
}
this.lastResult.add(result)
emitter.emit('spec_complete', this, result)
refreshNoActivityTimeout()
}
this.serialize = function () {
return {
id: this.id,
name: this.name,
isReady: this.state === READY
}
}
this.execute = function (config) {
activeSockets.forEach(function (socket) {
socket.emit('execute', config)
})
this.state = EXECUTING
refreshNoActivityTimeout()
}
}
Browser.STATE_READY = READY
Browser.STATE_EXECUTING = EXECUTING
Browser.STATE_READY_DISCONNECTED = READY_DISCONNECTED
Browser.STATE_EXECUTING_DISCONNECTED = EXECUTING_DISCONNECTED
Browser.STATE_DISCONNECTED = DISCONNECTED
module.exports = Browser