1 var helper = require('./helper')
2 var events = require('./events')
3 var logger = require('./logger')
5 var Result = require('./browser_result')
7 // The browser is ready to execute tests.
10 // The browser is executing the tests/
13 // The browser is not executing, but temporarily disconnected (waiting for reconnecting).
14 var READY_DISCONNECTED = 3
16 // The browser is executing the tests, but temporarily disconnect (waiting for reconnecting).
17 var EXECUTING_DISCONNECTED = 4
19 // The browser got permanently disconnected (being removed from the collection and destroyed).
22 var Browser = function (id, fullName, /* capturedBrowsers */ collection, emitter, socket, timer,
23 /* config.browserDisconnectTimeout */ disconnectDelay,
24 /* config.browserNoActivityTimeout */ noActivityTimeout) {
25 var name = helper.browserFullNameToShort(fullName)
26 var log = logger.create(name)
27 var activeSockets = [socket]
28 var activeSocketsIds = function () {
29 return activeSockets.map(function (s) {
36 var disconnect = function (reason) {
37 self.state = DISCONNECTED
38 self.disconnectsCount++
39 log.warn('Disconnected (%d times)' + (reason || ''), self.disconnectsCount)
40 collection.remove(self)
43 var noActivityTimeoutId
44 var refreshNoActivityTimeout = noActivityTimeout ? function () {
45 clearNoActivityTimeout()
46 noActivityTimeoutId = timer.setTimeout(function () {
47 self.lastResult.totalTimeEnd()
48 self.lastResult.disconnected = true
49 disconnect(', because no message in ' + noActivityTimeout + ' ms.')
50 emitter.emit('browser_complete', self)
54 var clearNoActivityTimeout = noActivityTimeout ? function () {
55 if (noActivityTimeoutId) {
56 timer.clearTimeout(noActivityTimeoutId)
57 noActivityTimeoutId = null
62 this.fullName = fullName
65 this.lastResult = new Result()
66 this.disconnectsCount = 0
68 this.init = function () {
71 events.bindAll(this, socket)
73 log.info('Connected on socket %s with id %s', socket.id, id)
75 // TODO(vojta): move to collection
76 emitter.emit('browsers_change', collection)
78 emitter.emit('browser_register', this)
81 this.isReady = function () {
82 return this.state === READY
85 this.toString = function () {
89 this.onError = function (error) {
94 this.lastResult.error = true
95 emitter.emit('browser_error', this, error)
97 refreshNoActivityTimeout()
100 this.onInfo = function (info) {
101 if (this.isReady()) {
105 // TODO(vojta): remove
106 if (helper.isDefined(info.dump)) {
107 emitter.emit('browser_log', this, info.dump, 'dump')
110 if (helper.isDefined(info.log)) {
111 emitter.emit('browser_log', this, info.log, info.type)
114 refreshNoActivityTimeout()
117 this.onStart = function (info) {
118 this.lastResult = new Result()
119 this.lastResult.total = info.total
121 if (info.total === null) {
122 log.warn('Adapter did not report total number of specs.')
125 emitter.emit('browser_start', this, info)
126 refreshNoActivityTimeout()
129 this.onComplete = function (result) {
130 if (this.isReady()) {
135 this.lastResult.totalTimeEnd()
137 if (!this.lastResult.success) {
138 this.lastResult.error = true
141 emitter.emit('browsers_change', collection)
142 emitter.emit('browser_complete', this, result)
144 clearNoActivityTimeout()
147 this.onDisconnect = function (_, disconnectedSocket) {
148 activeSockets.splice(activeSockets.indexOf(disconnectedSocket), 1)
150 if (activeSockets.length) {
151 log.debug('Disconnected %s, still have %s', disconnectedSocket.id, activeSocketsIds())
155 if (this.state === READY) {
157 } else if (this.state === EXECUTING) {
158 log.debug('Disconnected during run, waiting %sms for reconnecting.', disconnectDelay)
159 this.state = EXECUTING_DISCONNECTED
161 pendingDisconnect = timer.setTimeout(function () {
162 self.lastResult.totalTimeEnd()
163 self.lastResult.disconnected = true
165 emitter.emit('browser_complete', self)
168 clearNoActivityTimeout()
172 this.reconnect = function (newSocket) {
173 if (this.state === EXECUTING_DISCONNECTED) {
174 this.state = EXECUTING
175 log.debug('Reconnected on %s.', newSocket.id)
176 } else if (this.state === EXECUTING || this.state === READY) {
177 log.debug('New connection %s (already have %s)', newSocket.id, activeSocketsIds())
178 } else if (this.state === DISCONNECTED) {
180 log.info('Connected on socket %s with id %s', newSocket.id, this.id)
183 // TODO(vojta): move to collection
184 emitter.emit('browsers_change', collection)
186 emitter.emit('browser_register', this)
189 var exists = activeSockets.some(function (s) {
190 return s.id === newSocket.id
193 activeSockets.push(newSocket)
194 events.bindAll(this, newSocket)
197 if (pendingDisconnect) {
198 timer.clearTimeout(pendingDisconnect)
201 refreshNoActivityTimeout()
204 this.onResult = function (result) {
206 return result.forEach(this.onResult, this)
209 // ignore - probably results from last run (after server disconnecting)
210 if (this.isReady()) {
214 this.lastResult.add(result)
216 emitter.emit('spec_complete', this, result)
217 refreshNoActivityTimeout()
220 this.serialize = function () {
224 isReady: this.state === READY
228 this.execute = function (config) {
229 activeSockets.forEach(function (socket) {
230 socket.emit('execute', config)
233 this.state = EXECUTING
234 refreshNoActivityTimeout()
238 Browser.STATE_READY = READY
239 Browser.STATE_EXECUTING = EXECUTING
240 Browser.STATE_READY_DISCONNECTED = READY_DISCONNECTED
241 Browser.STATE_EXECUTING_DISCONNECTED = EXECUTING_DISCONNECTED
242 Browser.STATE_DISCONNECTED = DISCONNECTED
244 module.exports = Browser