ScuttleBot

scuttlebot / tests / e2e / node_modules / playwright / lib / runner / dispatcher.js
Blame History Raw 531 lines
1
"use strict";
2
var __defProp = Object.defineProperty;
3
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
var __getOwnPropNames = Object.getOwnPropertyNames;
5
var __hasOwnProp = Object.prototype.hasOwnProperty;
6
var __export = (target, all) => {
7
for (var name in all)
8
__defProp(target, name, { get: all[name], enumerable: true });
9
};
10
var __copyProps = (to, from, except, desc) => {
11
if (from && typeof from === "object" || typeof from === "function") {
12
for (let key of __getOwnPropNames(from))
13
if (!__hasOwnProp.call(to, key) && key !== except)
14
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
}
16
return to;
17
};
18
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
var dispatcher_exports = {};
20
__export(dispatcher_exports, {
21
Dispatcher: () => Dispatcher
22
});
23
module.exports = __toCommonJS(dispatcher_exports);
24
var import_utils = require("playwright-core/lib/utils");
25
var import_utils2 = require("playwright-core/lib/utils");
26
var import_rebase = require("./rebase");
27
var import_workerHost = require("./workerHost");
28
var import_ipc = require("../common/ipc");
29
var import_internalReporter = require("../reporters/internalReporter");
30
var import_util = require("../util");
31
var import_storage = require("./storage");
32
class Dispatcher {
33
constructor(config, reporter, failureTracker) {
34
this._workerSlots = [];
35
this._queue = [];
36
this._workerLimitPerProjectId = /* @__PURE__ */ new Map();
37
this._queuedOrRunningHashCount = /* @__PURE__ */ new Map();
38
this._finished = new import_utils.ManualPromise();
39
this._isStopped = true;
40
this._extraEnvByProjectId = /* @__PURE__ */ new Map();
41
this._producedEnvByProjectId = /* @__PURE__ */ new Map();
42
this._config = config;
43
this._reporter = reporter;
44
this._failureTracker = failureTracker;
45
for (const project of config.projects) {
46
if (project.workers)
47
this._workerLimitPerProjectId.set(project.id, project.workers);
48
}
49
}
50
_findFirstJobToRun() {
51
for (let index = 0; index < this._queue.length; index++) {
52
const job = this._queue[index];
53
const projectIdWorkerLimit = this._workerLimitPerProjectId.get(job.projectId);
54
if (!projectIdWorkerLimit)
55
return index;
56
const runningWorkersWithSameProjectId = this._workerSlots.filter((w) => w.busy && w.worker && w.worker.projectId() === job.projectId).length;
57
if (runningWorkersWithSameProjectId < projectIdWorkerLimit)
58
return index;
59
}
60
return -1;
61
}
62
_scheduleJob() {
63
if (this._isStopped)
64
return;
65
const jobIndex = this._findFirstJobToRun();
66
if (jobIndex === -1)
67
return;
68
const job = this._queue[jobIndex];
69
let workerIndex = this._workerSlots.findIndex((w) => !w.busy && w.worker && w.worker.hash() === job.workerHash && !w.worker.didSendStop());
70
if (workerIndex === -1)
71
workerIndex = this._workerSlots.findIndex((w) => !w.busy);
72
if (workerIndex === -1) {
73
return;
74
}
75
this._queue.splice(jobIndex, 1);
76
const jobDispatcher = new JobDispatcher(job, this._config, this._reporter, this._failureTracker, () => this.stop().catch(() => {
77
}));
78
this._workerSlots[workerIndex].busy = true;
79
this._workerSlots[workerIndex].jobDispatcher = jobDispatcher;
80
void this._runJobInWorker(workerIndex, jobDispatcher).then(() => {
81
this._workerSlots[workerIndex].jobDispatcher = void 0;
82
this._workerSlots[workerIndex].busy = false;
83
this._checkFinished();
84
this._scheduleJob();
85
});
86
}
87
async _runJobInWorker(index, jobDispatcher) {
88
const job = jobDispatcher.job;
89
if (jobDispatcher.skipWholeJob())
90
return;
91
let worker = this._workerSlots[index].worker;
92
if (worker && (worker.hash() !== job.workerHash || worker.didSendStop())) {
93
await worker.stop();
94
worker = void 0;
95
if (this._isStopped)
96
return;
97
}
98
let startError;
99
if (!worker) {
100
worker = this._createWorker(job, index, (0, import_ipc.serializeConfig)(this._config, true));
101
this._workerSlots[index].worker = worker;
102
worker.on("exit", () => this._workerSlots[index].worker = void 0);
103
startError = await worker.start();
104
if (this._isStopped)
105
return;
106
}
107
if (startError)
108
jobDispatcher.onExit(startError);
109
else
110
jobDispatcher.runInWorker(worker);
111
const result = await jobDispatcher.jobResult;
112
this._updateCounterForWorkerHash(job.workerHash, -1);
113
if (result.didFail)
114
void worker.stop(
115
true
116
/* didFail */
117
);
118
else if (this._isWorkerRedundant(worker))
119
void worker.stop();
120
if (!this._isStopped && result.newJob) {
121
this._queue.unshift(result.newJob);
122
this._updateCounterForWorkerHash(result.newJob.workerHash, 1);
123
}
124
}
125
_checkFinished() {
126
if (this._finished.isDone())
127
return;
128
if (this._queue.length && !this._isStopped)
129
return;
130
if (this._workerSlots.some((w) => w.busy))
131
return;
132
this._finished.resolve();
133
}
134
_isWorkerRedundant(worker) {
135
let workersWithSameHash = 0;
136
for (const slot of this._workerSlots) {
137
if (slot.worker && !slot.worker.didSendStop() && slot.worker.hash() === worker.hash())
138
workersWithSameHash++;
139
}
140
return workersWithSameHash > this._queuedOrRunningHashCount.get(worker.hash());
141
}
142
_updateCounterForWorkerHash(hash, delta) {
143
this._queuedOrRunningHashCount.set(hash, delta + (this._queuedOrRunningHashCount.get(hash) || 0));
144
}
145
async run(testGroups, extraEnvByProjectId) {
146
this._extraEnvByProjectId = extraEnvByProjectId;
147
this._queue = testGroups;
148
for (const group of testGroups)
149
this._updateCounterForWorkerHash(group.workerHash, 1);
150
this._isStopped = false;
151
this._workerSlots = [];
152
if (this._failureTracker.hasReachedMaxFailures())
153
void this.stop();
154
for (let i = 0; i < this._config.config.workers; i++)
155
this._workerSlots.push({ busy: false });
156
for (let i = 0; i < this._workerSlots.length; i++)
157
this._scheduleJob();
158
this._checkFinished();
159
await this._finished;
160
}
161
_createWorker(testGroup, parallelIndex, loaderData) {
162
const projectConfig = this._config.projects.find((p) => p.id === testGroup.projectId);
163
const outputDir = projectConfig.project.outputDir;
164
const worker = new import_workerHost.WorkerHost(testGroup, {
165
parallelIndex,
166
config: loaderData,
167
extraEnv: this._extraEnvByProjectId.get(testGroup.projectId) || {},
168
outputDir,
169
pauseOnError: this._failureTracker.pauseOnError(),
170
pauseAtEnd: this._failureTracker.pauseAtEnd(projectConfig)
171
});
172
const handleOutput = (params) => {
173
const chunk = chunkFromParams(params);
174
if (worker.didFail()) {
175
return { chunk };
176
}
177
const currentlyRunning = this._workerSlots[parallelIndex].jobDispatcher?.currentlyRunning();
178
if (!currentlyRunning)
179
return { chunk };
180
return { chunk, test: currentlyRunning.test, result: currentlyRunning.result };
181
};
182
worker.on("stdOut", (params) => {
183
const { chunk, test, result } = handleOutput(params);
184
result?.stdout.push(chunk);
185
this._reporter.onStdOut?.(chunk, test, result);
186
});
187
worker.on("stdErr", (params) => {
188
const { chunk, test, result } = handleOutput(params);
189
result?.stderr.push(chunk);
190
this._reporter.onStdErr?.(chunk, test, result);
191
});
192
worker.on("teardownErrors", (params) => {
193
this._failureTracker.onWorkerError();
194
for (const error of params.fatalErrors)
195
this._reporter.onError?.(error);
196
});
197
worker.on("exit", () => {
198
const producedEnv = this._producedEnvByProjectId.get(testGroup.projectId) || {};
199
this._producedEnvByProjectId.set(testGroup.projectId, { ...producedEnv, ...worker.producedEnv() });
200
});
201
worker.onRequest("cloneStorage", async (params) => {
202
return await import_storage.Storage.clone(params.storageFile, outputDir);
203
});
204
worker.onRequest("upstreamStorage", async (params) => {
205
await import_storage.Storage.upstream(params.storageFile, params.storageOutFile);
206
});
207
return worker;
208
}
209
producedEnvByProjectId() {
210
return this._producedEnvByProjectId;
211
}
212
async stop() {
213
if (this._isStopped)
214
return;
215
this._isStopped = true;
216
await Promise.all(this._workerSlots.map(({ worker }) => worker?.stop()));
217
this._checkFinished();
218
}
219
}
220
class JobDispatcher {
221
constructor(job, config, reporter, failureTracker, stopCallback) {
222
this.jobResult = new import_utils.ManualPromise();
223
this._listeners = [];
224
this._failedTests = /* @__PURE__ */ new Set();
225
this._failedWithNonRetriableError = /* @__PURE__ */ new Set();
226
this._remainingByTestId = /* @__PURE__ */ new Map();
227
this._dataByTestId = /* @__PURE__ */ new Map();
228
this._parallelIndex = 0;
229
this._workerIndex = 0;
230
this.job = job;
231
this._config = config;
232
this._reporter = reporter;
233
this._failureTracker = failureTracker;
234
this._stopCallback = stopCallback;
235
this._remainingByTestId = new Map(this.job.tests.map((e) => [e.id, e]));
236
}
237
_onTestBegin(params) {
238
const test = this._remainingByTestId.get(params.testId);
239
if (!test) {
240
return;
241
}
242
const result = test._appendTestResult();
243
this._dataByTestId.set(test.id, { test, result, steps: /* @__PURE__ */ new Map() });
244
result.parallelIndex = this._parallelIndex;
245
result.workerIndex = this._workerIndex;
246
result.startTime = new Date(params.startWallTime);
247
this._reporter.onTestBegin?.(test, result);
248
this._currentlyRunning = { test, result };
249
}
250
_onTestEnd(params) {
251
if (this._failureTracker.hasReachedMaxFailures()) {
252
params.status = "interrupted";
253
params.errors = [];
254
}
255
const data = this._dataByTestId.get(params.testId);
256
if (!data) {
257
return;
258
}
259
this._dataByTestId.delete(params.testId);
260
this._remainingByTestId.delete(params.testId);
261
const { result, test } = data;
262
result.duration = params.duration;
263
result.errors = params.errors;
264
result.error = result.errors[0];
265
result.status = params.status;
266
result.annotations = params.annotations;
267
test.annotations = [...params.annotations];
268
test.expectedStatus = params.expectedStatus;
269
test.timeout = params.timeout;
270
const isFailure = result.status !== "skipped" && result.status !== test.expectedStatus;
271
if (isFailure)
272
this._failedTests.add(test);
273
if (params.hasNonRetriableError)
274
this._addNonretriableTestAndSerialModeParents(test);
275
this._reportTestEnd(test, result);
276
this._currentlyRunning = void 0;
277
}
278
_addNonretriableTestAndSerialModeParents(test) {
279
this._failedWithNonRetriableError.add(test);
280
for (let parent = test.parent; parent; parent = parent.parent) {
281
if (parent._parallelMode === "serial")
282
this._failedWithNonRetriableError.add(parent);
283
}
284
}
285
_onStepBegin(params) {
286
const data = this._dataByTestId.get(params.testId);
287
if (!data) {
288
return;
289
}
290
const { result, steps, test } = data;
291
const parentStep = params.parentStepId ? steps.get(params.parentStepId) : void 0;
292
const step = {
293
title: params.title,
294
titlePath: () => {
295
const parentPath = parentStep?.titlePath() || [];
296
return [...parentPath, params.title];
297
},
298
parent: parentStep,
299
category: params.category,
300
startTime: new Date(params.wallTime),
301
duration: -1,
302
steps: [],
303
attachments: [],
304
annotations: [],
305
location: params.location
306
};
307
steps.set(params.stepId, step);
308
(parentStep || result).steps.push(step);
309
this._reporter.onStepBegin?.(test, result, step);
310
}
311
_onStepEnd(params) {
312
const data = this._dataByTestId.get(params.testId);
313
if (!data) {
314
return;
315
}
316
const { result, steps, test } = data;
317
const step = steps.get(params.stepId);
318
if (!step) {
319
this._reporter.onStdErr?.("Internal error: step end without step begin: " + params.stepId, test, result);
320
return;
321
}
322
step.duration = params.wallTime - step.startTime.getTime();
323
if (params.error)
324
step.error = params.error;
325
if (params.suggestedRebaseline)
326
(0, import_rebase.addSuggestedRebaseline)(step.location, params.suggestedRebaseline);
327
step.annotations = params.annotations;
328
steps.delete(params.stepId);
329
this._reporter.onStepEnd?.(test, result, step);
330
}
331
_onAttach(params) {
332
const data = this._dataByTestId.get(params.testId);
333
if (!data) {
334
return;
335
}
336
const attachment = {
337
name: params.name,
338
path: params.path,
339
contentType: params.contentType,
340
body: params.body !== void 0 ? Buffer.from(params.body, "base64") : void 0
341
};
342
data.result.attachments.push(attachment);
343
if (params.stepId) {
344
const step = data.steps.get(params.stepId);
345
if (step)
346
step.attachments.push(attachment);
347
else
348
this._reporter.onStdErr?.("Internal error: step id not found: " + params.stepId);
349
}
350
}
351
_failTestWithErrors(test, errors) {
352
const runData = this._dataByTestId.get(test.id);
353
let result;
354
if (runData) {
355
result = runData.result;
356
} else {
357
result = test._appendTestResult();
358
this._reporter.onTestBegin?.(test, result);
359
}
360
result.errors = [...errors];
361
result.error = result.errors[0];
362
result.status = errors.length ? "failed" : "skipped";
363
this._reportTestEnd(test, result);
364
this._failedTests.add(test);
365
}
366
_massSkipTestsFromRemaining(testIds, errors) {
367
for (const test of this._remainingByTestId.values()) {
368
if (!testIds.has(test.id))
369
continue;
370
if (!this._failureTracker.hasReachedMaxFailures()) {
371
this._failTestWithErrors(test, errors);
372
errors = [];
373
}
374
this._remainingByTestId.delete(test.id);
375
}
376
if (errors.length) {
377
this._failureTracker.onWorkerError();
378
for (const error of errors)
379
this._reporter.onError?.(error);
380
}
381
}
382
_onDone(params) {
383
if (!this._remainingByTestId.size && !this._failedTests.size && !params.fatalErrors.length && !params.skipTestsDueToSetupFailure.length && !params.fatalUnknownTestIds && !params.unexpectedExitError && !params.stoppedDueToUnhandledErrorInTestFail) {
384
this._finished({ didFail: false });
385
return;
386
}
387
for (const testId of params.fatalUnknownTestIds || []) {
388
const test = this._remainingByTestId.get(testId);
389
if (test) {
390
this._remainingByTestId.delete(testId);
391
this._failTestWithErrors(test, [{ message: `Test not found in the worker process. Make sure test title does not change.` }]);
392
}
393
}
394
if (params.fatalErrors.length) {
395
this._massSkipTestsFromRemaining(new Set(this._remainingByTestId.keys()), params.fatalErrors);
396
}
397
this._massSkipTestsFromRemaining(new Set(params.skipTestsDueToSetupFailure), []);
398
if (params.unexpectedExitError) {
399
if (this._currentlyRunning)
400
this._massSkipTestsFromRemaining(/* @__PURE__ */ new Set([this._currentlyRunning.test.id]), [params.unexpectedExitError]);
401
else
402
this._massSkipTestsFromRemaining(new Set(this._remainingByTestId.keys()), [params.unexpectedExitError]);
403
}
404
const retryCandidates = /* @__PURE__ */ new Set();
405
const serialSuitesWithFailures = /* @__PURE__ */ new Set();
406
for (const failedTest of this._failedTests) {
407
if (this._failedWithNonRetriableError.has(failedTest))
408
continue;
409
retryCandidates.add(failedTest);
410
let outermostSerialSuite;
411
for (let parent = failedTest.parent; parent; parent = parent.parent) {
412
if (parent._parallelMode === "serial")
413
outermostSerialSuite = parent;
414
}
415
if (outermostSerialSuite && !this._failedWithNonRetriableError.has(outermostSerialSuite))
416
serialSuitesWithFailures.add(outermostSerialSuite);
417
}
418
const testsBelongingToSomeSerialSuiteWithFailures = [...this._remainingByTestId.values()].filter((test) => {
419
let parent = test.parent;
420
while (parent && !serialSuitesWithFailures.has(parent))
421
parent = parent.parent;
422
return !!parent;
423
});
424
this._massSkipTestsFromRemaining(new Set(testsBelongingToSomeSerialSuiteWithFailures.map((test) => test.id)), []);
425
for (const serialSuite of serialSuitesWithFailures) {
426
serialSuite.allTests().forEach((test) => retryCandidates.add(test));
427
}
428
const remaining = [...this._remainingByTestId.values()];
429
for (const test of retryCandidates) {
430
if (test.results.length < test.retries + 1)
431
remaining.push(test);
432
}
433
const newJob = remaining.length ? { ...this.job, tests: remaining } : void 0;
434
this._finished({ didFail: true, newJob });
435
}
436
onExit(data) {
437
const unexpectedExitError = data.unexpectedly ? {
438
message: `Error: worker process exited unexpectedly (code=${data.code}, signal=${data.signal})`
439
} : void 0;
440
this._onDone({ skipTestsDueToSetupFailure: [], fatalErrors: [], unexpectedExitError });
441
}
442
_finished(result) {
443
import_utils.eventsHelper.removeEventListeners(this._listeners);
444
this.jobResult.resolve(result);
445
}
446
runInWorker(worker) {
447
this._parallelIndex = worker.parallelIndex;
448
this._workerIndex = worker.workerIndex;
449
const runPayload = {
450
file: this.job.requireFile,
451
entries: this.job.tests.map((test) => {
452
return { testId: test.id, retry: test.results.length };
453
})
454
};
455
worker.runTestGroup(runPayload);
456
this._listeners = [
457
import_utils.eventsHelper.addEventListener(worker, "testBegin", this._onTestBegin.bind(this)),
458
import_utils.eventsHelper.addEventListener(worker, "testEnd", this._onTestEnd.bind(this)),
459
import_utils.eventsHelper.addEventListener(worker, "stepBegin", this._onStepBegin.bind(this)),
460
import_utils.eventsHelper.addEventListener(worker, "stepEnd", this._onStepEnd.bind(this)),
461
import_utils.eventsHelper.addEventListener(worker, "attach", this._onAttach.bind(this)),
462
import_utils.eventsHelper.addEventListener(worker, "testPaused", this._onTestPaused.bind(this, worker)),
463
import_utils.eventsHelper.addEventListener(worker, "done", this._onDone.bind(this)),
464
import_utils.eventsHelper.addEventListener(worker, "exit", this.onExit.bind(this))
465
];
466
}
467
_onTestPaused(worker, params) {
468
const data = this._dataByTestId.get(params.testId);
469
if (!data)
470
return;
471
const { result, test } = data;
472
const sendMessage = async (message) => {
473
try {
474
if (this.jobResult.isDone())
475
throw new Error("Test has already stopped");
476
const response = await worker.sendCustomMessage({ testId: test.id, request: message.request });
477
if (response.error)
478
(0, import_internalReporter.addLocationAndSnippetToError)(this._config.config, response.error);
479
return response;
480
} catch (e) {
481
const error = (0, import_util.serializeError)(e);
482
(0, import_internalReporter.addLocationAndSnippetToError)(this._config.config, error);
483
return { response: void 0, error };
484
}
485
};
486
result.status = params.status;
487
result.errors = params.errors;
488
result.error = result.errors[0];
489
void this._reporter.onTestPaused?.(test, result).then(() => {
490
worker.sendResume({});
491
});
492
this._failureTracker.onTestPaused?.({ ...params, sendMessage });
493
}
494
skipWholeJob() {
495
const allTestsSkipped = this.job.tests.every((test) => test.expectedStatus === "skipped");
496
if (allTestsSkipped && !this._failureTracker.hasReachedMaxFailures()) {
497
for (const test of this.job.tests) {
498
const result = test._appendTestResult();
499
this._reporter.onTestBegin?.(test, result);
500
result.status = "skipped";
501
result.annotations = [...test.annotations];
502
this._reportTestEnd(test, result);
503
}
504
return true;
505
}
506
return false;
507
}
508
currentlyRunning() {
509
return this._currentlyRunning;
510
}
511
_reportTestEnd(test, result) {
512
this._reporter.onTestEnd?.(test, result);
513
const hadMaxFailures = this._failureTracker.hasReachedMaxFailures();
514
this._failureTracker.onTestEnd(test, result);
515
if (this._failureTracker.hasReachedMaxFailures()) {
516
this._stopCallback();
517
if (!hadMaxFailures)
518
this._reporter.onError?.({ message: import_utils2.colors.red(`Testing stopped early after ${this._failureTracker.maxFailures()} maximum allowed failures.`) });
519
}
520
}
521
}
522
function chunkFromParams(params) {
523
if (typeof params.text === "string")
524
return params.text;
525
return Buffer.from(params.buffer, "base64");
526
}
527
// Annotate the CommonJS export names for ESM import in node:
528
0 && (module.exports = {
529
Dispatcher
530
});
531

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button