2021-06-06 17:09:53 -07:00
|
|
|
/**
|
|
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import child_process from 'child_process';
|
|
|
|
import path from 'path';
|
|
|
|
import { EventEmitter } from 'events';
|
2021-08-02 17:17:20 -07:00
|
|
|
import { RunPayload, TestBeginPayload, TestEndPayload, DonePayload, TestOutputPayload, WorkerInitParams, StepBeginPayload, StepEndPayload } from './ipc';
|
2021-10-11 10:52:17 -04:00
|
|
|
import type { TestResult, Reporter, TestStep } from 'playwright-core/types/testReporter';
|
2021-08-10 21:26:45 -07:00
|
|
|
import { Suite, TestCase } from './test';
|
2021-06-06 17:09:53 -07:00
|
|
|
import { Loader } from './loader';
|
|
|
|
|
2021-07-27 11:04:38 -07:00
|
|
|
export type TestGroup = {
|
|
|
|
workerHash: string;
|
|
|
|
requireFile: string;
|
|
|
|
repeatEachIndex: number;
|
|
|
|
projectIndex: number;
|
|
|
|
tests: TestCase[];
|
|
|
|
};
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
export class Dispatcher {
|
|
|
|
private _workers = new Set<Worker>();
|
|
|
|
private _freeWorkers: Worker[] = [];
|
|
|
|
private _workerClaimers: (() => void)[] = [];
|
|
|
|
|
2021-08-17 13:57:26 -07:00
|
|
|
private _testById = new Map<string, { test: TestCase, result: TestResult, steps: Map<string, TestStep>, stepStack: Set<TestStep> }>();
|
2021-07-29 21:41:06 -07:00
|
|
|
private _queue: TestGroup[] = [];
|
2021-06-06 17:09:53 -07:00
|
|
|
private _stopCallback = () => {};
|
|
|
|
readonly _loader: Loader;
|
|
|
|
private _reporter: Reporter;
|
|
|
|
private _hasWorkerErrors = false;
|
|
|
|
private _isStopped = false;
|
|
|
|
private _failureCount = 0;
|
|
|
|
|
2021-07-27 11:04:38 -07:00
|
|
|
constructor(loader: Loader, testGroups: TestGroup[], reporter: Reporter) {
|
2021-06-06 17:09:53 -07:00
|
|
|
this._loader = loader;
|
|
|
|
this._reporter = reporter;
|
2021-07-29 21:41:06 -07:00
|
|
|
this._queue = testGroups;
|
2021-07-27 11:04:38 -07:00
|
|
|
for (const group of testGroups) {
|
|
|
|
for (const test of group.tests) {
|
|
|
|
const result = test._appendTestResult();
|
2021-08-02 17:17:20 -07:00
|
|
|
// When changing this line, change the one in retry too.
|
2021-08-17 13:57:26 -07:00
|
|
|
this._testById.set(test._id, { test, result, steps: new Map(), stepStack: new Set() });
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async run() {
|
|
|
|
// Loop in case job schedules more jobs
|
|
|
|
while (this._queue.length && !this._isStopped)
|
|
|
|
await this._dispatchQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _dispatchQueue() {
|
|
|
|
const jobs = [];
|
|
|
|
while (this._queue.length) {
|
|
|
|
if (this._isStopped)
|
|
|
|
break;
|
2021-07-29 21:41:06 -07:00
|
|
|
const testGroup = this._queue.shift()!;
|
|
|
|
const requiredHash = testGroup.workerHash;
|
|
|
|
let worker = await this._obtainWorker(testGroup);
|
2021-08-05 15:00:00 -07:00
|
|
|
while (worker && worker.hash && worker.hash !== requiredHash) {
|
2021-06-06 17:09:53 -07:00
|
|
|
worker.stop();
|
2021-07-29 21:41:06 -07:00
|
|
|
worker = await this._obtainWorker(testGroup);
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
2021-08-05 15:00:00 -07:00
|
|
|
if (this._isStopped || !worker)
|
2021-06-06 17:09:53 -07:00
|
|
|
break;
|
2021-07-29 21:41:06 -07:00
|
|
|
jobs.push(this._runJob(worker, testGroup));
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
await Promise.all(jobs);
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:41:06 -07:00
|
|
|
async _runJob(worker: Worker, testGroup: TestGroup) {
|
|
|
|
worker.run(testGroup);
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
let doneCallback = () => {};
|
|
|
|
const result = new Promise<void>(f => doneCallback = f);
|
2021-07-07 12:04:43 -07:00
|
|
|
const doneWithJob = () => {
|
|
|
|
worker.removeListener('testBegin', onTestBegin);
|
|
|
|
worker.removeListener('testEnd', onTestEnd);
|
2021-09-27 21:38:19 -07:00
|
|
|
worker.removeListener('stepBegin', onStepBegin);
|
|
|
|
worker.removeListener('stepEnd', onStepEnd);
|
2021-07-07 12:04:43 -07:00
|
|
|
worker.removeListener('done', onDone);
|
|
|
|
worker.removeListener('exit', onExit);
|
|
|
|
doneCallback();
|
|
|
|
};
|
|
|
|
|
2021-07-29 21:41:06 -07:00
|
|
|
const remainingByTestId = new Map(testGroup.tests.map(e => [ e._id, e ]));
|
2021-07-07 12:04:43 -07:00
|
|
|
let lastStartedTestId: string | undefined;
|
2021-09-27 21:38:19 -07:00
|
|
|
const failedTestIds = new Set<string>();
|
2021-07-07 12:04:43 -07:00
|
|
|
|
|
|
|
const onTestBegin = (params: TestBeginPayload) => {
|
|
|
|
lastStartedTestId = params.testId;
|
2021-09-27 21:38:19 -07:00
|
|
|
if (this._hasReachedMaxFailures())
|
|
|
|
return;
|
|
|
|
const { test, result: testRun } = this._testById.get(params.testId)!;
|
|
|
|
testRun.workerIndex = params.workerIndex;
|
|
|
|
testRun.startTime = new Date(params.startWallTime);
|
|
|
|
this._reporter.onTestBegin?.(test, testRun);
|
2021-07-07 12:04:43 -07:00
|
|
|
};
|
|
|
|
worker.addListener('testBegin', onTestBegin);
|
|
|
|
|
|
|
|
const onTestEnd = (params: TestEndPayload) => {
|
|
|
|
remainingByTestId.delete(params.testId);
|
2021-09-27 21:38:19 -07:00
|
|
|
if (this._hasReachedMaxFailures())
|
|
|
|
return;
|
|
|
|
const { test, result } = this._testById.get(params.testId)!;
|
|
|
|
result.duration = params.duration;
|
|
|
|
result.error = params.error;
|
|
|
|
result.attachments = params.attachments.map(a => ({
|
|
|
|
name: a.name,
|
|
|
|
path: a.path,
|
|
|
|
contentType: a.contentType,
|
|
|
|
body: a.body ? Buffer.from(a.body, 'base64') : undefined
|
|
|
|
}));
|
|
|
|
result.status = params.status;
|
|
|
|
test.expectedStatus = params.expectedStatus;
|
|
|
|
test.annotations = params.annotations;
|
|
|
|
test.timeout = params.timeout;
|
|
|
|
const isFailure = result.status !== 'skipped' && result.status !== test.expectedStatus;
|
|
|
|
if (isFailure)
|
|
|
|
failedTestIds.add(params.testId);
|
|
|
|
this._reportTestEnd(test, result);
|
2021-07-07 12:04:43 -07:00
|
|
|
};
|
|
|
|
worker.addListener('testEnd', onTestEnd);
|
|
|
|
|
2021-09-27 21:38:19 -07:00
|
|
|
const onStepBegin = (params: StepBeginPayload) => {
|
|
|
|
const { test, result, steps, stepStack } = this._testById.get(params.testId)!;
|
|
|
|
const parentStep = params.forceNoParent ? undefined : [...stepStack].pop();
|
|
|
|
const step: TestStep = {
|
|
|
|
title: params.title,
|
|
|
|
titlePath: () => {
|
|
|
|
const parentPath = parentStep?.titlePath() || [];
|
|
|
|
return [...parentPath, params.title];
|
|
|
|
},
|
|
|
|
parent: parentStep,
|
|
|
|
category: params.category,
|
|
|
|
startTime: new Date(params.wallTime),
|
|
|
|
duration: 0,
|
|
|
|
steps: [],
|
|
|
|
data: {},
|
|
|
|
};
|
|
|
|
steps.set(params.stepId, step);
|
|
|
|
(parentStep || result).steps.push(step);
|
|
|
|
if (params.canHaveChildren)
|
|
|
|
stepStack.add(step);
|
|
|
|
this._reporter.onStepBegin?.(test, result, step);
|
|
|
|
};
|
|
|
|
worker.on('stepBegin', onStepBegin);
|
|
|
|
|
|
|
|
const onStepEnd = (params: StepEndPayload) => {
|
|
|
|
const { test, result, steps, stepStack } = this._testById.get(params.testId)!;
|
|
|
|
const step = steps.get(params.stepId);
|
|
|
|
if (!step) {
|
|
|
|
this._reporter.onStdErr?.('Internal error: step end without step begin: ' + params.stepId, test, result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
step.duration = params.wallTime - step.startTime.getTime();
|
|
|
|
if (params.error)
|
|
|
|
step.error = params.error;
|
|
|
|
stepStack.delete(step);
|
|
|
|
steps.delete(params.stepId);
|
|
|
|
this._reporter.onStepEnd?.(test, result, step);
|
|
|
|
};
|
|
|
|
worker.on('stepEnd', onStepEnd);
|
|
|
|
|
2021-07-07 12:04:43 -07:00
|
|
|
const onDone = (params: DonePayload) => {
|
|
|
|
let remaining = [...remainingByTestId.values()];
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
// We won't file remaining if:
|
|
|
|
// - there are no remaining
|
|
|
|
// - we are here not because something failed
|
|
|
|
// - no unrecoverable worker error
|
2021-09-27 21:38:19 -07:00
|
|
|
if (!remaining.length && !failedTestIds.size && !params.fatalError) {
|
2021-06-06 17:09:53 -07:00
|
|
|
this._freeWorkers.push(worker);
|
|
|
|
this._notifyWorkerClaimer();
|
2021-07-07 12:04:43 -07:00
|
|
|
doneWithJob();
|
2021-06-06 17:09:53 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When worker encounters error, we will stop it and create a new one.
|
|
|
|
worker.stop();
|
2021-08-19 17:04:09 -07:00
|
|
|
worker.didFail = true;
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2021-07-28 15:43:37 -07:00
|
|
|
// In case of fatal error, report first remaining test as failing with this error,
|
|
|
|
// and all others as skipped.
|
2021-06-06 17:09:53 -07:00
|
|
|
if (params.fatalError) {
|
2021-07-28 15:43:37 -07:00
|
|
|
let first = true;
|
2021-07-29 21:41:06 -07:00
|
|
|
for (const test of remaining) {
|
|
|
|
const { result } = this._testById.get(test._id)!;
|
2021-07-28 15:43:37 -07:00
|
|
|
if (this._hasReachedMaxFailures())
|
|
|
|
break;
|
2021-07-07 12:04:43 -07:00
|
|
|
// There might be a single test that has started but has not finished yet.
|
2021-07-29 21:41:06 -07:00
|
|
|
if (test._id !== lastStartedTestId)
|
2021-08-02 17:17:20 -07:00
|
|
|
this._reporter.onTestBegin?.(test, result);
|
2021-06-06 17:09:53 -07:00
|
|
|
result.error = params.fatalError;
|
2021-07-28 15:43:37 -07:00
|
|
|
result.status = first ? 'failed' : 'skipped';
|
|
|
|
this._reportTestEnd(test, result);
|
2021-09-27 15:58:26 -07:00
|
|
|
failedTestIds.add(test._id);
|
2021-07-28 15:43:37 -07:00
|
|
|
first = false;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
2021-08-09 14:21:53 -07:00
|
|
|
if (first) {
|
|
|
|
// We had a fatal error after all tests have passed - most likely in the afterAll hook.
|
|
|
|
// Let's just fail the test run.
|
|
|
|
this._hasWorkerErrors = true;
|
|
|
|
this._reporter.onError?.(params.fatalError);
|
|
|
|
}
|
2021-07-07 12:04:43 -07:00
|
|
|
// Since we pretend that all remaining tests failed, there is nothing else to run,
|
2021-06-06 17:09:53 -07:00
|
|
|
// except for possible retries.
|
|
|
|
remaining = [];
|
|
|
|
}
|
2021-08-10 21:26:45 -07:00
|
|
|
|
2021-09-27 15:58:26 -07:00
|
|
|
const retryCandidates = new Set<string>();
|
|
|
|
const serialSuitesWithFailures = new Set<Suite>();
|
|
|
|
|
|
|
|
for (const failedTestId of failedTestIds) {
|
|
|
|
retryCandidates.add(failedTestId);
|
2021-08-10 21:26:45 -07:00
|
|
|
|
|
|
|
let outermostSerialSuite: Suite | undefined;
|
2021-09-27 15:58:26 -07:00
|
|
|
for (let parent = this._testById.get(failedTestId)!.test.parent; parent; parent = parent.parent) {
|
2021-09-02 15:42:07 -07:00
|
|
|
if (parent._parallelMode === 'serial')
|
2021-08-10 21:26:45 -07:00
|
|
|
outermostSerialSuite = parent;
|
|
|
|
}
|
2021-09-27 15:58:26 -07:00
|
|
|
if (outermostSerialSuite)
|
|
|
|
serialSuitesWithFailures.add(outermostSerialSuite);
|
|
|
|
}
|
2021-08-10 21:26:45 -07:00
|
|
|
|
2021-09-27 15:58:26 -07:00
|
|
|
// We have failed tests that belong to a serial suite.
|
|
|
|
// We should skip all future tests from the same serial suite.
|
|
|
|
remaining = remaining.filter(test => {
|
|
|
|
let parent = test.parent;
|
|
|
|
while (parent && !serialSuitesWithFailures.has(parent))
|
|
|
|
parent = parent.parent;
|
|
|
|
|
|
|
|
// Does not belong to the failed serial suite, keep it.
|
|
|
|
if (!parent)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Emulate a "skipped" run, and drop this test from remaining.
|
|
|
|
const { result } = this._testById.get(test._id)!;
|
|
|
|
this._reporter.onTestBegin?.(test, result);
|
|
|
|
result.status = 'skipped';
|
|
|
|
this._reportTestEnd(test, result);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const serialSuite of serialSuitesWithFailures) {
|
|
|
|
// Add all tests from faiiled serial suites for possible retry.
|
|
|
|
// These will only be retried together, because they have the same
|
|
|
|
// "retries" setting and the same number of previous runs.
|
|
|
|
serialSuite.allTests().forEach(test => retryCandidates.add(test._id));
|
2021-08-10 21:26:45 -07:00
|
|
|
}
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2021-08-10 21:26:45 -07:00
|
|
|
for (const testId of retryCandidates) {
|
2021-06-06 17:09:53 -07:00
|
|
|
const pair = this._testById.get(testId)!;
|
2021-08-25 12:19:50 -07:00
|
|
|
if (!this._isStopped && pair.test.results.length < pair.test.retries + 1) {
|
2021-06-06 17:09:53 -07:00
|
|
|
pair.result = pair.test._appendTestResult();
|
2021-08-02 17:17:20 -07:00
|
|
|
pair.steps = new Map();
|
2021-08-17 13:57:26 -07:00
|
|
|
pair.stepStack = new Set();
|
2021-08-10 21:26:45 -07:00
|
|
|
remaining.push(pair.test);
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remaining.length)
|
2021-07-29 21:41:06 -07:00
|
|
|
this._queue.unshift({ ...testGroup, tests: remaining });
|
2021-06-06 17:09:53 -07:00
|
|
|
|
|
|
|
// This job is over, we just scheduled another one.
|
2021-07-07 12:04:43 -07:00
|
|
|
doneWithJob();
|
|
|
|
};
|
|
|
|
worker.on('done', onDone);
|
|
|
|
|
|
|
|
const onExit = () => {
|
2021-07-28 15:43:37 -07:00
|
|
|
if (worker.didSendStop)
|
|
|
|
onDone({});
|
|
|
|
else
|
|
|
|
onDone({ fatalError: { value: 'Worker process exited unexpectedly' } });
|
2021-07-07 12:04:43 -07:00
|
|
|
};
|
|
|
|
worker.on('exit', onExit);
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:41:06 -07:00
|
|
|
async _obtainWorker(testGroup: TestGroup) {
|
2021-06-06 17:09:53 -07:00
|
|
|
const claimWorker = (): Promise<Worker> | null => {
|
2021-08-05 15:00:00 -07:00
|
|
|
if (this._isStopped)
|
|
|
|
return null;
|
2021-06-06 17:09:53 -07:00
|
|
|
// Use available worker.
|
|
|
|
if (this._freeWorkers.length)
|
|
|
|
return Promise.resolve(this._freeWorkers.pop()!);
|
|
|
|
// Create a new worker.
|
|
|
|
if (this._workers.size < this._loader.fullConfig().workers)
|
2021-07-29 21:41:06 -07:00
|
|
|
return this._createWorker(testGroup);
|
2021-06-06 17:09:53 -07:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note: it is important to claim the worker synchronously,
|
|
|
|
// so that we won't miss a _notifyWorkerClaimer call while awaiting.
|
|
|
|
let worker = claimWorker();
|
|
|
|
if (!worker) {
|
|
|
|
// Wait for available or stopped worker.
|
|
|
|
await new Promise<void>(f => this._workerClaimers.push(f));
|
|
|
|
worker = claimWorker();
|
|
|
|
}
|
2021-08-05 15:00:00 -07:00
|
|
|
return worker;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async _notifyWorkerClaimer() {
|
|
|
|
if (this._isStopped || !this._workerClaimers.length)
|
|
|
|
return;
|
|
|
|
const callback = this._workerClaimers.shift()!;
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:41:06 -07:00
|
|
|
_createWorker(testGroup: TestGroup) {
|
2021-06-06 17:09:53 -07:00
|
|
|
const worker = new Worker(this);
|
|
|
|
worker.on('stdOut', (params: TestOutputPayload) => {
|
|
|
|
const chunk = chunkFromParams(params);
|
2021-08-19 17:04:09 -07:00
|
|
|
if (worker.didFail) {
|
|
|
|
// Note: we keep reading stdout from workers that are currently stopping after failure,
|
|
|
|
// to debug teardown issues. However, we avoid spoiling the test result from
|
|
|
|
// the next retry.
|
|
|
|
this._reporter.onStdOut?.(chunk);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-06 17:09:53 -07:00
|
|
|
const pair = params.testId ? this._testById.get(params.testId) : undefined;
|
|
|
|
if (pair)
|
|
|
|
pair.result.stdout.push(chunk);
|
2021-08-02 17:17:20 -07:00
|
|
|
this._reporter.onStdOut?.(chunk, pair?.test, pair?.result);
|
2021-06-06 17:09:53 -07:00
|
|
|
});
|
|
|
|
worker.on('stdErr', (params: TestOutputPayload) => {
|
|
|
|
const chunk = chunkFromParams(params);
|
2021-08-19 17:04:09 -07:00
|
|
|
if (worker.didFail) {
|
|
|
|
// Note: we keep reading stderr from workers that are currently stopping after failure,
|
|
|
|
// to debug teardown issues. However, we avoid spoiling the test result from
|
|
|
|
// the next retry.
|
|
|
|
this._reporter.onStdErr?.(chunk);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-06 17:09:53 -07:00
|
|
|
const pair = params.testId ? this._testById.get(params.testId) : undefined;
|
|
|
|
if (pair)
|
|
|
|
pair.result.stderr.push(chunk);
|
2021-08-02 17:17:20 -07:00
|
|
|
this._reporter.onStdErr?.(chunk, pair?.test, pair?.result);
|
2021-06-06 17:09:53 -07:00
|
|
|
});
|
2021-09-27 18:58:08 +02:00
|
|
|
worker.on('teardownError', ({ error }) => {
|
2021-06-06 17:09:53 -07:00
|
|
|
this._hasWorkerErrors = true;
|
2021-07-16 12:40:33 -07:00
|
|
|
this._reporter.onError?.(error);
|
2021-06-06 17:09:53 -07:00
|
|
|
});
|
|
|
|
worker.on('exit', () => {
|
|
|
|
this._workers.delete(worker);
|
|
|
|
this._notifyWorkerClaimer();
|
|
|
|
if (this._stopCallback && !this._workers.size)
|
|
|
|
this._stopCallback();
|
|
|
|
});
|
|
|
|
this._workers.add(worker);
|
2021-07-29 21:41:06 -07:00
|
|
|
return worker.init(testGroup).then(() => worker);
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async stop() {
|
|
|
|
this._isStopped = true;
|
|
|
|
if (this._workers.size) {
|
|
|
|
const result = new Promise<void>(f => this._stopCallback = f);
|
|
|
|
for (const worker of this._workers)
|
|
|
|
worker.stop();
|
|
|
|
await result;
|
|
|
|
}
|
2021-08-05 15:00:00 -07:00
|
|
|
while (this._workerClaimers.length)
|
|
|
|
this._workerClaimers.shift()!();
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:43:37 -07:00
|
|
|
private _hasReachedMaxFailures() {
|
2021-07-07 12:04:43 -07:00
|
|
|
const maxFailures = this._loader.fullConfig().maxFailures;
|
2021-07-28 15:43:37 -07:00
|
|
|
return maxFailures > 0 && this._failureCount >= maxFailures;
|
2021-07-07 12:04:43 -07:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:43:37 -07:00
|
|
|
private _reportTestEnd(test: TestCase, result: TestResult) {
|
2021-06-06 17:09:53 -07:00
|
|
|
if (result.status !== 'skipped' && result.status !== test.expectedStatus)
|
|
|
|
++this._failureCount;
|
2021-07-28 15:43:37 -07:00
|
|
|
this._reporter.onTestEnd?.(test, result);
|
2021-06-06 17:09:53 -07:00
|
|
|
const maxFailures = this._loader.fullConfig().maxFailures;
|
|
|
|
if (maxFailures && this._failureCount === maxFailures)
|
2021-07-07 12:04:43 -07:00
|
|
|
this.stop().catch(e => {});
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
hasWorkerErrors(): boolean {
|
|
|
|
return this._hasWorkerErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let lastWorkerIndex = 0;
|
|
|
|
|
|
|
|
class Worker extends EventEmitter {
|
|
|
|
process: child_process.ChildProcess;
|
|
|
|
runner: Dispatcher;
|
|
|
|
hash = '';
|
|
|
|
index: number;
|
2021-07-28 15:43:37 -07:00
|
|
|
didSendStop = false;
|
2021-08-19 17:04:09 -07:00
|
|
|
didFail = false;
|
2021-06-06 17:09:53 -07:00
|
|
|
|
|
|
|
constructor(runner: Dispatcher) {
|
|
|
|
super();
|
|
|
|
this.runner = runner;
|
|
|
|
this.index = lastWorkerIndex++;
|
|
|
|
|
|
|
|
this.process = child_process.fork(path.join(__dirname, 'worker.js'), {
|
|
|
|
detached: false,
|
|
|
|
env: {
|
|
|
|
FORCE_COLOR: process.stdout.isTTY ? '1' : '0',
|
|
|
|
DEBUG_COLORS: process.stdout.isTTY ? '1' : '0',
|
|
|
|
TEST_WORKER_INDEX: String(this.index),
|
|
|
|
...process.env
|
|
|
|
},
|
|
|
|
// Can't pipe since piping slows down termination for some reason.
|
|
|
|
stdio: ['ignore', 'ignore', process.env.PW_RUNNER_DEBUG ? 'inherit' : 'ignore', 'ipc']
|
|
|
|
});
|
|
|
|
this.process.on('exit', () => this.emit('exit'));
|
|
|
|
this.process.on('error', e => {}); // do not yell at a send to dead process.
|
|
|
|
this.process.on('message', (message: any) => {
|
|
|
|
const { method, params } = message;
|
|
|
|
this.emit(method, params);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:41:06 -07:00
|
|
|
async init(testGroup: TestGroup) {
|
|
|
|
this.hash = testGroup.workerHash;
|
2021-06-06 17:09:53 -07:00
|
|
|
const params: WorkerInitParams = {
|
|
|
|
workerIndex: this.index,
|
2021-07-29 21:41:06 -07:00
|
|
|
repeatEachIndex: testGroup.repeatEachIndex,
|
|
|
|
projectIndex: testGroup.projectIndex,
|
2021-06-06 17:09:53 -07:00
|
|
|
loader: this.runner._loader.serialize(),
|
|
|
|
};
|
|
|
|
this.process.send({ method: 'init', params });
|
|
|
|
await new Promise(f => this.process.once('message', f)); // Ready ack
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:41:06 -07:00
|
|
|
run(testGroup: TestGroup) {
|
|
|
|
const runPayload: RunPayload = {
|
|
|
|
file: testGroup.requireFile,
|
|
|
|
entries: testGroup.tests.map(test => {
|
|
|
|
return { testId: test._id, retry: test.results.length - 1 };
|
|
|
|
}),
|
|
|
|
};
|
2021-06-06 17:09:53 -07:00
|
|
|
this.process.send({ method: 'run', params: runPayload });
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2021-07-07 12:04:43 -07:00
|
|
|
if (!this.didSendStop)
|
|
|
|
this.process.send({ method: 'stop' });
|
|
|
|
this.didSendStop = true;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function chunkFromParams(params: TestOutputPayload): string | Buffer {
|
|
|
|
if (typeof params.text === 'string')
|
|
|
|
return params.text;
|
|
|
|
return Buffer.from(params.buffer!, 'base64');
|
|
|
|
}
|