1 | import { DryRunStatus, DryRunResult, DryRunOptions, MutantRunResult, MutantRunOptions, MutantRunStatus } from '@stryker-mutator/api/test-runner';
|
2 | import { errorToString } from '@stryker-mutator/util';
|
3 | import log4js from 'log4js';
|
4 |
|
5 | import { OutOfMemoryError } from '../child-proxy/out-of-memory-error.js';
|
6 |
|
7 | import { TestRunnerDecorator } from './test-runner-decorator.js';
|
8 |
|
9 | const ERROR_MESSAGE = 'Test runner crashed. Tried twice to restart it without any luck. Last time the error message was: ';
|
10 | export const MAX_RETRIES = 2;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export class RetryRejectedDecorator extends TestRunnerDecorator {
|
16 | private readonly log = log4js.getLogger(RetryRejectedDecorator.name);
|
17 |
|
18 | public async dryRun(options: DryRunOptions): Promise<DryRunResult> {
|
19 | const result = await this.run(() => super.dryRun(options));
|
20 | if (typeof result === 'string') {
|
21 | return {
|
22 | status: DryRunStatus.Error,
|
23 | errorMessage: result,
|
24 | };
|
25 | } else {
|
26 | return result;
|
27 | }
|
28 | }
|
29 |
|
30 | public async mutantRun(options: MutantRunOptions): Promise<MutantRunResult> {
|
31 | const result = await this.run(() => super.mutantRun(options));
|
32 | if (typeof result === 'string') {
|
33 | return {
|
34 | status: MutantRunStatus.Error,
|
35 | errorMessage: result,
|
36 | };
|
37 | } else {
|
38 | return result;
|
39 | }
|
40 | }
|
41 |
|
42 | private async run<T extends DryRunResult | MutantRunResult>(
|
43 | actRun: () => Promise<T>,
|
44 | attemptsLeft = MAX_RETRIES,
|
45 | lastError?: unknown
|
46 | ): Promise<T | string> {
|
47 | if (attemptsLeft > 0) {
|
48 | try {
|
49 | return await actRun();
|
50 | } catch (error) {
|
51 | if (error instanceof OutOfMemoryError) {
|
52 | this.log.info(
|
53 | "Test runner process [%s] ran out of memory. You probably have a memory leak in your tests. Don't worry, Stryker will restart the process, but you might want to investigate this later, because this decreases performance.",
|
54 | error.pid
|
55 | );
|
56 | }
|
57 | await this.recover();
|
58 | return this.run(actRun, attemptsLeft - 1, error);
|
59 | }
|
60 | } else {
|
61 | await this.recover();
|
62 | return `${ERROR_MESSAGE}${errorToString(lastError)}`;
|
63 | }
|
64 | }
|
65 | }
|