/
child-process-test-runner-decorator.ts
59 lines (49 loc) · 1.97 KB
/
child-process-test-runner-decorator.ts
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
import { StrykerOptions } from '@stryker-mutator/api/core';
import { TestRunner, DryRunOptions, MutantRunOptions, MutantRunResult, DryRunResult } from '@stryker-mutator/api/test-runner';
import { ExpirableTask } from '@stryker-mutator/util';
import ChildProcessCrashedError from '../child-proxy/child-process-crashed-error';
import ChildProcessProxy from '../child-proxy/child-process-proxy';
import { LoggingClientContext } from '../logging';
import { ChildProcessTestRunnerWorker } from './child-process-test-runner-worker';
const MAX_WAIT_FOR_DISPOSE = 2000;
/**
* Runs the given test runner in a child process and forwards reports about test results
*/
export default class ChildProcessTestRunnerDecorator implements TestRunner {
private readonly worker: ChildProcessProxy<ChildProcessTestRunnerWorker>;
constructor(options: StrykerOptions, sandboxWorkingDirectory: string, loggingContext: LoggingClientContext) {
this.worker = ChildProcessProxy.create(
require.resolve('./child-process-test-runner-worker'),
loggingContext,
options,
{},
sandboxWorkingDirectory,
ChildProcessTestRunnerWorker,
options.testRunnerNodeArgs
);
}
public init(): Promise<void> {
return this.worker.proxy.init();
}
public dryRun(options: DryRunOptions): Promise<DryRunResult> {
return this.worker.proxy.dryRun(options);
}
public mutantRun(options: MutantRunOptions): Promise<MutantRunResult> {
return this.worker.proxy.mutantRun(options);
}
public async dispose(): Promise<void> {
await ExpirableTask.timeout(
// First let the inner test runner dispose
this.worker.proxy.dispose().catch((error) => {
// It's OK if the child process is already down.
if (!(error instanceof ChildProcessCrashedError)) {
throw error;
}
}),
// ... but don't wait forever on that
MAX_WAIT_FOR_DISPOSE
);
// After that, dispose the child process itself
await this.worker.dispose();
}
}