-
Notifications
You must be signed in to change notification settings - Fork 660
/
browser-job.js
154 lines (118 loc) · 5.54 KB
/
browser-job.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
import { pull as remove } from 'lodash';
import AsyncEventEmitter from '../utils/async-event-emitter';
import TestRunController from './test-run-controller';
import SessionController from '../test-run/session-controller';
import RESULT from './browser-job-result';
// Browser job
export default class BrowserJob extends AsyncEventEmitter {
constructor (tests, browserConnections, proxy, screenshots, warningLog, fixtureHookController, opts) {
super();
this.started = false;
this.total = 0;
this.passed = 0;
this.opts = opts;
this.proxy = proxy;
this.browserConnections = browserConnections;
this.screenshots = screenshots;
this.warningLog = warningLog;
this.fixtureHookController = fixtureHookController;
this.result = null;
this.testRunControllerQueue = tests.map((test, index) => this._createTestRunController(test, index));
this.completionQueue = [];
this.reportsPending = [];
this.connectionErrorListener = error => this._setResult(RESULT.errored, error);
this.browserConnections.map(bc => bc.once('error', this.connectionErrorListener));
}
_createTestRunController (test, index) {
const testRunController = new TestRunController(test, index + 1, this.proxy, this.screenshots, this.warningLog,
this.fixtureHookController, this.opts);
testRunController.on('test-run-create', async testRunInfo => {
await this.emit('test-run-create', testRunInfo);
});
testRunController.on('test-run-start', async () => {
await this.emit('test-run-start', testRunController.testRun);
});
testRunController.on('test-run-ready', async () => {
await this.emit('test-run-ready', testRunController);
});
testRunController.on('test-run-restart', async () => this._onTestRunRestart(testRunController));
testRunController.on('test-run-before-done', async () => {
await this.emit('test-run-before-done', testRunController);
});
testRunController.on('test-run-done', async () => this._onTestRunDone(testRunController));
testRunController.on('test-action-start', async args => {
await this.emit('test-action-start', args);
});
testRunController.on('test-action-done', async args => {
await this.emit('test-action-done', args);
});
return testRunController;
}
async _setResult (status, data) {
if (this.result)
return;
this.result = { status, data };
this.browserConnections.forEach(bc => bc.removeListener('error', this.connectionErrorListener));
await Promise.all(this.browserConnections.map(bc => bc.reportJobResult(this.result.status, this.result.data)));
}
_addToCompletionQueue (testRunInfo) {
this.completionQueue.push(testRunInfo);
}
_removeFromCompletionQueue (testRunInfo) {
remove(this.completionQueue, testRunInfo);
}
_onTestRunRestart (testRunController) {
this._removeFromCompletionQueue(testRunController);
this.testRunControllerQueue.unshift(testRunController);
}
async _onTestRunDone (testRunController) {
this.total++;
if (!testRunController.testRun.errs.length)
this.passed++;
while (this.completionQueue.length && this.completionQueue[0].done) {
testRunController = this.completionQueue.shift();
await this.emit('test-run-done', testRunController.testRun);
remove(this.reportsPending, testRunController);
}
if (!this.completionQueue.length && !this.hasQueuedTestRuns) {
if (!this.opts.live)
SessionController.closeSession(testRunController.testRun);
this
._setResult(RESULT.done, { total: this.total, passed: this.passed })
.then(() => this.emit('done'));
}
}
// API
get hasQueuedTestRuns () {
return !!this.testRunControllerQueue.length;
}
async popNextTestRunUrl (connection) {
while (this.testRunControllerQueue.length) {
// NOTE: before hook for test run fixture is currently
// executing, so test run is temporary blocked
const testRunController = this.testRunControllerQueue[0];
const isBlocked = testRunController.blocked;
const isConcurrency = this.opts.concurrency > 1;
const hasIncompleteTestRuns = this.completionQueue.some(controller => !controller.done);
const needWaitLastTestInFixture = this.reportsPending.some(controller => controller.test.fixture !== testRunController.test.fixture);
if (isBlocked || (hasIncompleteTestRuns || needWaitLastTestInFixture) && !isConcurrency)
break;
this.reportsPending.push(testRunController);
this.testRunControllerQueue.shift();
this._addToCompletionQueue(testRunController);
if (!this.started) {
this.started = true;
await this.emit('start');
}
const testRunUrl = await testRunController.start(connection);
if (testRunUrl)
return testRunUrl;
}
return null;
}
abort () {
this.clearListeners();
this._setResult(RESULT.aborted);
this.browserConnections.map(bc => bc.removeJob(this));
}
}