mirror of
https://github.com/morluto/rea.git
synced 2026-12-02 13:11:18 -05:00
6bcec126c7
* test(adb): link stub executables to one launched dispatcher macOS checks every newly written executable on its first launch, one file at a time for the whole host (about 150 ms each; 50 fresh stubs launched together took 9 s). The ADB tests wrote a fresh fake adb (and Node.js tool shims) per test, so under parallel load the first launch waited behind every other test's stubs inside the provider's 10 s adb version probe deadline or the test's own deadline. writeShellStub hard-links each stub to one dispatcher the test process has already launched; the dispatcher sources the stub's script with the stub's own $0 and arguments. AdbProvider.test.ts drops from 38 s to 12 s alone. * test(adb): remove the stub dispatcher at worker teardown Forked Vitest workers skip process exit hooks, so the dispatcher directory leaked. The test runner now removes it through the worker cleanup it already uses for the process ownership host.
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { link, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { promisify } from "node:util";
|
|
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
// macOS assesses every newly written executable on its first exec, one file at
|
|
// a time for the whole host (about 150 ms each, measured with 50 parallel
|
|
// launches taking 9 s). A fresh stub per test therefore waits behind every
|
|
// other test's fresh stubs, and that wait lands inside whatever production
|
|
// deadline first runs it. A hard link shares the assessed file, so every stub
|
|
// links one dispatcher this process has already executed. The dispatcher
|
|
// sources `<stub>.sh`; `$0` and the arguments stay the stub's own.
|
|
const DISPATCHER = '#!/bin/sh\n. "$0.sh"\n';
|
|
|
|
let dispatcher: Promise<string> | undefined;
|
|
let dispatcherDirectory: string | undefined;
|
|
|
|
const readyDispatcher = (): Promise<string> =>
|
|
(dispatcher ??= (async () => {
|
|
const directory = await mkdtemp(
|
|
join(await realpath(tmpdir()), "rea-shell-stub-"),
|
|
);
|
|
dispatcherDirectory = directory;
|
|
const path = join(directory, "dispatch");
|
|
await writeFile(`${path}.sh`, "exit 0\n");
|
|
await writeFile(path, DISPATCHER, { mode: 0o755 });
|
|
// Pay the first-exec assessment here, before any stub is linked.
|
|
await execFileAsync(path);
|
|
return path;
|
|
})());
|
|
|
|
/**
|
|
* Write a POSIX shell stub at `path` that runs `script` with the stub's own
|
|
* `$0` and arguments, as an executable `#!/bin/sh` script would.
|
|
*/
|
|
export const writeShellStub = async (
|
|
path: string,
|
|
script: string,
|
|
): Promise<string> => {
|
|
await writeFile(`${path}.sh`, script);
|
|
await link(await readyDispatcher(), path);
|
|
return path;
|
|
};
|
|
|
|
/**
|
|
* Remove this module's dispatcher. The test runner calls it when the worker
|
|
* stops; stubs already linked keep the shared file.
|
|
*/
|
|
export const closeShellStubDispatcher = async (): Promise<void> => {
|
|
const directory = dispatcherDirectory;
|
|
if (directory === undefined) return;
|
|
await dispatcher?.catch(() => undefined);
|
|
await rm(directory, { recursive: true, force: true });
|
|
dispatcherDirectory = undefined;
|
|
dispatcher = undefined;
|
|
};
|