-
Notifications
You must be signed in to change notification settings - Fork 659
/
test-run-controller.js
238 lines (179 loc) · 7.36 KB
/
test-run-controller.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
import AsyncEventEmitter from '../utils/async-event-emitter';
import { TestRun as LegacyTestRun } from 'testcafe-legacy-api';
import TestRun from '../test-run';
import SessionController from '../test-run/session-controller';
const QUARANTINE_THRESHOLD = 3;
const DISCONNECT_THRESHOLD = 3;
class Quarantine {
constructor () {
this.attempts = [];
}
getFailedAttempts () {
return this.attempts.filter(errors => !!errors.length);
}
getPassedAttempts () {
return this.attempts.filter(errors => errors.length === 0);
}
getNextAttemptNumber () {
return this.attempts.length + 1;
}
isThresholdReached (extraErrors) {
const { failedTimes, passedTimes } = this._getAttemptsResult(extraErrors);
const failedThresholdReached = failedTimes >= QUARANTINE_THRESHOLD;
const passedThresholdReached = passedTimes >= QUARANTINE_THRESHOLD;
return failedThresholdReached || passedThresholdReached;
}
isFirstAttemptSuccessful (extraErrors) {
const { failedTimes, passedTimes } = this._getAttemptsResult(extraErrors);
return failedTimes === 0 && passedTimes > 0;
}
_getAttemptsResult (extraErrors) {
let failedTimes = this.getFailedAttempts().length;
let passedTimes = this.getPassedAttempts().length;
if (extraErrors) {
if (extraErrors.length)
failedTimes += extraErrors.length;
else
passedTimes += 1;
}
return { failedTimes, passedTimes };
}
}
export default class TestRunController extends AsyncEventEmitter {
constructor (test, index, proxy, screenshots, warningLog, fixtureHookController, opts) {
super();
this.test = test;
this.index = index;
this.opts = opts;
this.proxy = proxy;
this.screenshots = screenshots;
this.warningLog = warningLog;
this.fixtureHookController = fixtureHookController;
this.TestRunCtor = TestRunController._getTestRunCtor(test, opts);
this.testRun = null;
this.done = false;
this.quarantine = null;
this.disconnectionCount = 0;
if (this.opts.quarantineMode)
this.quarantine = new Quarantine();
}
static _getTestRunCtor (test, opts) {
if (opts.TestRunCtor)
return opts.TestRunCtor;
return test.isLegacy ? LegacyTestRun : TestRun;
}
async _createTestRun (connection) {
const screenshotCapturer = this.screenshots.createCapturerFor(this.test, this.index, this.quarantine, connection, this.warningLog);
const TestRunCtor = this.TestRunCtor;
this.testRun = new TestRunCtor(this.test, connection, screenshotCapturer, this.warningLog, this.opts);
this.screenshots.addTestRun(this.test, this.testRun);
if (this.testRun.addQuarantineInfo)
this.testRun.addQuarantineInfo(this.quarantine);
if (!this.quarantine || this._isFirstQuarantineAttempt()) {
await this.emit('test-run-create', {
testRun: this.testRun,
legacy: TestRunCtor === LegacyTestRun,
test: this.test,
index: this.index,
quarantine: this.quarantine,
});
}
return this.testRun;
}
async _endQuarantine () {
if (this.quarantine.attempts.length > 1)
this.testRun.unstable = this.quarantine.getPassedAttempts().length > 0;
await this._emitTestRunDone();
}
_shouldKeepInQuarantine () {
const errors = this.testRun.errs;
const hasErrors = !!errors.length;
const attempts = this.quarantine.attempts;
const isFirstAttempt = this._isFirstQuarantineAttempt();
attempts.push(errors);
return isFirstAttempt ? hasErrors : !this.quarantine.isThresholdReached();
}
_isFirstQuarantineAttempt () {
return this.quarantine && !this.quarantine.attempts.length;
}
async _keepInQuarantine () {
await this._restartTest();
}
async _restartTest () {
await this.emit('test-run-restart');
}
async _testRunDoneInQuarantineMode () {
if (this._shouldKeepInQuarantine())
await this._keepInQuarantine();
else
await this._endQuarantine();
}
async _testRunDone () {
if (this.quarantine)
await this._testRunDoneInQuarantineMode();
else
await this._emitTestRunDone();
}
async _emitActionStart (args) {
await this.emit('test-action-start', args);
}
async _emitActionDone (args) {
await this.emit('test-action-done', args);
}
async _emitTestRunDone () {
// NOTE: we should report test run completion in order they were completed in browser.
// To keep a sequence after fixture hook execution we use completion queue.
await this.fixtureHookController.runFixtureAfterHookIfNecessary(this.testRun);
this.done = true;
await this.emit('test-run-done');
}
async _emitTestRunStart () {
await this.emit('test-run-start');
}
async _testRunBeforeDone () {
let raiseEvent = !this.quarantine;
if (!raiseEvent) {
const isSuccessfulQuarantineFirstAttempt = this._isFirstQuarantineAttempt() && !this.testRun.errs.length;
const isAttemptsThresholdReached = this.quarantine.isThresholdReached(this.testRun.errs);
raiseEvent = isSuccessfulQuarantineFirstAttempt || isAttemptsThresholdReached;
}
if (raiseEvent)
await this.emit('test-run-before-done');
}
_testRunDisconnected (connection) {
this.disconnectionCount++;
const disconnectionThresholdExceedeed = this.disconnectionCount >= DISCONNECT_THRESHOLD;
return connection
.processDisconnection(disconnectionThresholdExceedeed)
.then(() => {
return this._restartTest();
});
}
_assignTestRunEvents (testRun, connection) {
testRun.on('action-start', async args => this._emitActionStart(Object.assign(args, { testRun })));
testRun.on('action-done', async args => this._emitActionDone(Object.assign(args, { testRun })));
testRun.once('start', async () => this._emitTestRunStart());
testRun.once('ready', async () => {
if (!this.quarantine || this._isFirstQuarantineAttempt())
await this.emit('test-run-ready');
});
testRun.once('before-done', () => this._testRunBeforeDone());
testRun.once('done', () => this._testRunDone());
testRun.once('disconnected', () => this._testRunDisconnected(connection));
}
get blocked () {
return this.fixtureHookController.isTestBlocked(this.test);
}
async start (connection) {
const testRun = await this._createTestRun(connection);
const hookOk = await this.fixtureHookController.runFixtureBeforeHookIfNecessary(testRun);
if (this.test.skip || !hookOk) {
await this.emit('test-run-start');
await this._emitTestRunDone();
return null;
}
this._assignTestRunEvents(testRun, connection);
testRun.start();
return SessionController.getSessionUrl(testRun, this.proxy);
}
}