Skip to content

Commit

Permalink
[jest] try running unit tests in parallel (elastic#130823)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer authored and kertal committed May 24, 2022
1 parent 50fc387 commit cd67162
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
9 changes: 8 additions & 1 deletion .buildkite/scripts/steps/test/jest_parallel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ JOB_COUNT=$BUILDKITE_PARALLEL_JOB_COUNT
i=0
exitCode=0

# run unit tests in parallel
if [[ "$1" == 'jest.config.js' ]]; then
parallelism="-w2"
else
parallelism="--runInBand"
fi

while read -r config; do
if [ "$((i % JOB_COUNT))" -eq "$JOB" ]; then
echo "--- $ node scripts/jest --config $config"
node --max-old-space-size=14336 ./scripts/jest --config="$config" --runInBand --coverage=false --passWithNoTests
node --max-old-space-size=14336 ./scripts/jest --config="$config" "$parallelism" --coverage=false --passWithNoTests
lastCode=$?

if [ $lastCode -ne 0 ]; then
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-cli-dev-mode/src/dev_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const defaultOptions: Options = {
processExit$,
sigint$,
sigterm$,
forceColor: true,
};

expect.addSnapshotSerializer(extendedEnvSerializer);
Expand All @@ -80,7 +81,6 @@ beforeEach(() => {
jest.clearAllMocks();
log.messages.length = 0;
process.execArgv = ['--inheritted', '--exec', '--argv'];
process.env.FORCE_COLOR = process.env.FORCE_COLOR || '1';
currentProc = undefined;
});

Expand Down
10 changes: 9 additions & 1 deletion packages/kbn-cli-dev-mode/src/dev_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Options {
sigint$?: Rx.Observable<void>;
sigterm$?: Rx.Observable<void>;
mapLogLine?: DevServer['mapLogLine'];
forceColor?: boolean;
}

export class DevServer {
Expand All @@ -50,6 +51,7 @@ export class DevServer {
private readonly argv: string[];
private readonly gracefulTimeout: number;
private readonly mapLogLine?: (line: string) => string | null;
private readonly forceColor: boolean;

constructor(options: Options) {
this.log = options.log;
Expand All @@ -62,6 +64,7 @@ export class DevServer {
this.sigint$ = options.sigint$ ?? Rx.fromEvent<void>(process, 'SIGINT');
this.sigterm$ = options.sigterm$ ?? Rx.fromEvent<void>(process, 'SIGTERM');
this.mapLogLine = options.mapLogLine;
this.forceColor = options.forceColor ?? !!process.stdout.isTTY;
}

isReady$() {
Expand Down Expand Up @@ -141,8 +144,13 @@ export class DevServer {
})
);

const serverOptions = {
script: this.script,
argv: this.argv,
forceColor: this.forceColor,
};
const runServer = () =>
usingServerProcess(this.script, this.argv, (proc) => {
usingServerProcess(serverOptions, (proc) => {
this.phase$.next('starting');
this.ready$.next(false);

Expand Down
13 changes: 9 additions & 4 deletions packages/kbn-cli-dev-mode/src/using_server_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ interface ProcResource extends Rx.Unsubscribable {
unsubscribe(): void;
}

interface Options {
script: string;
argv: string[];
forceColor: boolean;
}

export function usingServerProcess<T>(
script: string,
argv: string[],
options: Options,
fn: (proc: execa.ExecaChildProcess) => Rx.Observable<T>
) {
return Rx.using(
(): ProcResource => {
const proc = execa.node(script, argv, {
const proc = execa.node(options.script, options.argv, {
stdio: 'pipe',
nodeOptions: [
...process.execArgv,
Expand All @@ -36,7 +41,7 @@ export function usingServerProcess<T>(
NODE_OPTIONS: process.env.NODE_OPTIONS,
isDevCliChild: 'true',
ELASTIC_APM_SERVICE_NAME: 'kibana',
...(process.stdout.isTTY ? { FORCE_COLOR: 'true' } : {}),
...(options.forceColor ? { FORCE_COLOR: 'true' } : {}),
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/cli_encryption_keys/encryption_config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('encryption key configuration', () => {
let encryptionConfig = null;

beforeEach(() => {
jest.spyOn(fs, 'readFileSync').mockReturnValue('xpack.security.encryptionKey: foo');
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('xpack.security.encryptionKey: foo');
jest.spyOn(crypto, 'randomBytes').mockReturnValue('random-key');
encryptionConfig = new EncryptionConfig();
});
Expand Down

0 comments on commit cd67162

Please sign in to comment.