-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
executor.js
96 lines (85 loc) · 3 KB
/
executor.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
'use strict'
const log = require('./logger').create()
class Executor {
constructor (capturedBrowsers, config, emitter) {
this.capturedBrowsers = capturedBrowsers
this.config = config
this.emitter = emitter
this.executionScheduled = false
this.errorsScheduled = []
this.pendingCount = 0
this.runningBrowsers = null
this.emitter.on('run_complete', () => this.onRunComplete())
this.emitter.on('browser_complete', () => this.onBrowserComplete())
}
schedule () {
if (this.capturedBrowsers.length === 0) {
log.warn(`No captured browser, open ${this.config.protocol}//${this.config.hostname}:${this.config.port}${this.config.urlRoot}`)
return false
} else if (this.capturedBrowsers.areAllReady()) {
log.debug('All browsers are ready, executing')
log.debug(`Captured ${this.capturedBrowsers.length} browsers`)
this.executionScheduled = false
this.capturedBrowsers.clearResults()
this.pendingCount = this.capturedBrowsers.length
this.runningBrowsers = this.capturedBrowsers.clone()
this.emitter.emit('run_start', this.runningBrowsers)
this.socketIoSockets.emit('execute', this.config.client)
return true
} else {
log.info('Delaying execution, these browsers are not ready: ' + this.capturedBrowsers.getNonReady().join(', '))
this.executionScheduled = true
return false
}
}
/**
* Schedule an error to be reported
* @param {string} errorMessage
* @returns {boolean} a boolean indicating whether or not the error was handled synchronously
*/
scheduleError (errorMessage) {
// We don't want to interfere with any running test.
// Verify that no test is running before reporting the error.
if (this.capturedBrowsers.areAllReady()) {
log.warn(errorMessage)
const errorResult = {
success: 0,
failed: 0,
skipped: 0,
error: errorMessage,
exitCode: 1
}
const noBrowsersStartedTests = []
this.emitter.emit('run_start', noBrowsersStartedTests) // A run cannot complete without being started
this.emitter.emit('run_complete', noBrowsersStartedTests, errorResult)
return true
} else {
this.errorsScheduled.push(errorMessage)
return false
}
}
onRunComplete () {
if (this.executionScheduled) {
this.schedule()
}
if (this.errorsScheduled.length) {
const errorsToReport = this.errorsScheduled
this.errorsScheduled = []
errorsToReport.forEach((error) => this.scheduleError(error))
}
}
onBrowserComplete () {
this.pendingCount--
if (!this.pendingCount) {
// Ensure run_complete is emitted in the next tick
// so it is never emitted before browser_complete
setTimeout(() => {
this.emitter.emit('run_complete', this.runningBrowsers, this.runningBrowsers.getResults())
})
}
}
}
Executor.factory = function (capturedBrowsers, config, emitter) {
return new Executor(capturedBrowsers, config, emitter)
}
module.exports = Executor