UNPKG

@wdio/local-runner

Version:
658 lines (646 loc) • 19.6 kB
// src/index.ts import logger2 from "@wdio/logger"; import { WritableStreamBuffer } from "stream-buffers"; import { startDisplayDaemonFromConfig } from "@wdio/display-server"; // src/worker.ts import url from "node:url"; import path from "node:path"; import { fork } from "node:child_process"; import { constants } from "node:os"; import { EventEmitter } from "node:events"; import logger from "@wdio/logger"; // src/transformStream.ts import split from "split2"; import { Transform } from "node:stream"; // src/constants.ts var SHUTDOWN_TIMEOUT = 5e3; var DEBUGGER_MESSAGES = [ "Debugger listening on", "Debugger attached", "Waiting for the debugger" ]; var BUFFER_OPTIONS = { initialSize: 1e3 * 1024, // start at 100 kilobytes. incrementAmount: 100 * 1024 // grow by 10 kilobytes each time buffer overflows. }; // src/transformStream.ts function runnerTransformStream(cid, inputStream, aggregator) { return inputStream.pipe(split(/\r?\n/, (line) => `${line} `)).pipe(ignore(DEBUGGER_MESSAGES)).pipe(map((line) => { const newLine = `[${cid}] ${line}`; aggregator?.push(newLine); return newLine; })); } function ignore(patternsToIgnore) { return new Transform({ decodeStrings: false, transform(chunk, encoding, next) { if (patternsToIgnore.some((m) => chunk.startsWith(m))) { return next(); } return next(null, chunk); }, final(next) { this.unpipe(); next(); } }); } function map(mapper) { return new Transform({ decodeStrings: false, transform(chunk, encoding, next) { return next(null, mapper(chunk)); }, final(next) { this.unpipe(); next(); } }); } // src/repl.ts import WDIORepl from "@wdio/repl"; var WDIORunnerRepl = class extends WDIORepl { childProcess; callback; commandIsRunning = false; constructor(childProcess, options) { super(options); this.childProcess = childProcess; } _getError(params) { if (!params.error) { return null; } const err = new Error(params.message); err.stack = params.stack; return err; } eval(cmd, context, filename, callback) { if (this.commandIsRunning) { return; } this.commandIsRunning = true; this.childProcess.send({ origin: "debugger", name: "eval", content: { cmd } }); this.callback = callback; } onResult(params) { const error = this._getError(params); if (this.callback) { this.callback(error, params.result); } this.commandIsRunning = false; } start(context) { this.childProcess.send({ origin: "debugger", name: "start" }); return super.start(context); } }; // src/replQueue.ts var ReplQueue = class { _repls = []; runningRepl; add(childProcess, options, onStart, onEnd) { this._repls.push({ childProcess, options, onStart, onEnd }); } next() { if (this.isRunning || this._repls.length === 0) { return; } const nextRepl = this._repls.shift(); if (!nextRepl) { return; } const { childProcess, options, onStart, onEnd } = nextRepl; const runningRepl = this.runningRepl = new WDIORunnerRepl(childProcess, options); onStart(); runningRepl.start().then(() => { const ev = { origin: "debugger", name: "stop" }; runningRepl.childProcess.send(ev); onEnd(ev); delete this.runningRepl; this.next(); }); } get isRunning() { return Boolean(this.runningRepl); } }; // src/stdStream.ts import { Transform as Transform2 } from "node:stream"; // src/utils.ts function removeLastListener(target, eventName) { const listener = target.listeners(eventName).reverse()[0]; if (listener) { target.removeListener(eventName, listener); } } // src/stdStream.ts var RunnerStream = class extends Transform2 { constructor() { super(); this.on("pipe", () => { removeLastListener(this, "close"); removeLastListener(this, "drain"); removeLastListener(this, "error"); removeLastListener(this, "finish"); removeLastListener(this, "unpipe"); }); } _transform(chunk, _encoding, callback) { callback(void 0, chunk); } _final(callback) { this.unpipe(); callback(); } }; // src/worker.ts var log = logger("@wdio/local-runner"); var replQueue = new ReplQueue(); var __dirname = path.dirname(url.fileURLToPath(import.meta.url)); var ACCEPTABLE_BUSY_COMMANDS = ["workerRequest", "endSession"]; var stdOutStream = new RunnerStream(); var stdErrStream = new RunnerStream(); stdOutStream.pipe(process.stdout); stdErrStream.pipe(process.stderr); var GRACEFUL_SHUTDOWN_SIGNALS = ["SIGTERM", "SIGINT"]; var SIGNAL_EXIT_CODE_OFFSET = 128; var UNKNOWN_SIGNAL_EXIT_CODE = 1; function getExitCodeForSignal(signal) { const signalNumber = signal ? constants.signals[signal] : void 0; return signalNumber ? SIGNAL_EXIT_CODE_OFFSET + signalNumber : UNKNOWN_SIGNAL_EXIT_CODE; } var NODE_OPTIONS_WITH_VALUE = /* @__PURE__ */ new Set([ "-r", "--require", "--import", "--experimental-loader", "--loader", "--conditions", "-C", "--max-old-space-size", "--max-semi-space-size" ]); var REPEATABLE_NODE_OPTIONS = /* @__PURE__ */ new Set([ "-r", "--require", "--import", "--experimental-loader", "--loader", "--conditions", "-C" ]); function nodeOptionName(group) { return group.split(" ")[0].split("=")[0]; } function tokenizeNodeOptions(value) { const tokens = []; let current = ""; let quote; const push = () => { if (current) { tokens.push(current); current = ""; } }; for (let i = 0; i < value.length; i++) { const char = value[i]; if (quote) { if (char === "\\" && i + 1 < value.length) { current += char + value[i + 1]; i++; continue; } current += char; if (char === quote) { quote = void 0; } continue; } if (char === '"' || char === "'") { quote = char; current += char; continue; } if (/\s/.test(char)) { push(); continue; } current += char; } push(); return tokens; } function parseNodeOptionGroups(value) { const parts = tokenizeNodeOptions(value); const groups = []; for (let i = 0; i < parts.length; i++) { const token = parts[i]; const name = token.split("=")[0]; const next = parts[i + 1]; if (!token.includes("=") && NODE_OPTIONS_WITH_VALUE.has(name) && next && !next.startsWith("-")) { groups.push(`${token} ${next}`); i++; continue; } groups.push(token); } return groups; } function mergeWorkerNodeOptions(parent, runner) { const merged = parseNodeOptionGroups(parent); if (typeof runner !== "string") { return merged.join(" "); } for (const group of parseNodeOptionGroups(runner)) { const name = nodeOptionName(group); if (REPEATABLE_NODE_OPTIONS.has(name)) { if (!merged.includes(group)) { merged.push(group); } continue; } const existing = merged.findIndex((item) => nodeOptionName(item) === name); if (existing === -1) { merged.push(group); } else { merged[existing] = group; } } return merged.join(" "); } var WorkerInstance = class extends EventEmitter { cid; config; configFile; // requestedCapabilities caps; // actual capabilities returned by driver capabilities; specs; execArgv; retries; stdout; stderr; childProcess; sessionId; server; logsAggregator = []; instances; isMultiRemote; isBusy = false; isKilled = false; isReady; isSetup; isReadyResolver = () => { }; isSetupResolver = () => { }; /** * assigns paramters to scope of instance * @param {object} config parsed configuration object * @param {string} cid capability id (e.g. 0-1) * @param {string} configFile path to config file (for sub process to parse) * @param {object} caps capability object * @param {string[]} specs list of paths to test files to run in this worker * @param {number} retries number of retries remaining * @param {object} execArgv execution arguments for the test run */ constructor(config, { cid, configFile, caps, specs, execArgv, retries }, stdout, stderr) { super(); this.cid = cid; this.config = config; this.configFile = configFile; this.caps = caps; this.capabilities = caps; this.specs = specs; this.execArgv = execArgv; this.retries = retries; this.stdout = stdout; this.stderr = stderr; this.isReady = new Promise((resolve) => { this.isReadyResolver = resolve; }); this.isSetup = new Promise((resolve) => { this.isSetupResolver = resolve; }); } /** * spawns process to kick of wdio-runner */ async startProcess() { const { cid, execArgv } = this; const argv = process.argv.slice(2); const runnerEnv = Object.assign({}, process.env, this.config.runnerEnv, { WDIO_WORKER_ID: cid, NODE_ENV: process.env.NODE_ENV || "test" }); if (this.config.outputDir) { let logFileRunner = `wdio-${cid}.log`; if (this.specs.length && this.specs[0]) { const specBaseName = path.basename(this.specs[0], path.extname(this.specs[0])); logFileRunner = `${specBaseName}-${cid}.log`; } runnerEnv.WDIO_LOG_PATH = path.join(this.config.outputDir, logFileRunner); } const runnerOverride = this.config.runnerEnv?.NODE_OPTIONS; const nodeOptions = mergeWorkerNodeOptions( process.env.NODE_OPTIONS ?? "", typeof runnerOverride === "string" ? runnerOverride : void 0 ); const hasSourceMaps = nodeOptions.split(" ").includes("--enable-source-maps"); const merged = this.shouldEnableSourceMaps() && !hasSourceMaps ? `${nodeOptions} --enable-source-maps`.trim() : nodeOptions; if (merged) { runnerEnv.NODE_OPTIONS = merged; } else { delete runnerEnv.NODE_OPTIONS; } log.info(`Start worker ${cid} with arg: ${argv.join(" ")}`); const childProcess = this.childProcess = fork( path.join(__dirname, "run.js"), argv, { cwd: process.cwd(), env: runnerEnv, execArgv, stdio: ["inherit", "pipe", "pipe", "ipc"] } ); childProcess.on("message", this._handleMessage.bind(this)); childProcess.on("error", this._handleError.bind(this)); childProcess.on("exit", this._handleExit.bind(this)); if (!process.env.WDIO_UNIT_TESTS) { if (childProcess.stdout !== null) { if (this.config.groupLogsByTestSpec) { runnerTransformStream(cid, childProcess.stdout, this.logsAggregator); } else { runnerTransformStream(cid, childProcess.stdout).pipe(stdOutStream); } } if (childProcess.stderr !== null) { runnerTransformStream(cid, childProcess.stderr).pipe(stdErrStream); } } return childProcess; } /** * Source maps help debug stack traces but cost worker boot time. * Enable for verbose log levels, or when WDIO_SOURCE_MAPS=1. */ shouldEnableSourceMaps() { if (process.env.WDIO_SOURCE_MAPS === "1" || process.env.WDIO_SOURCE_MAPS === "true") { return true; } const level = this.config.logLevel; return level === "trace" || level === "debug"; } _handleMessage(payload) { const { cid, childProcess } = this; if (payload.name === "finishedCommand") { this.isBusy = false; } if (payload.name === "ready") { this.isReadyResolver(true); } if (payload.name === "testFrameworkInit" && this.retries === -1 && payload.specFileRetries) { this.retries = payload.specFileRetries - 1; } if (payload.name === "sessionStarted") { this.isSetupResolver(true); if (this.retries === -1 && payload.specFileRetries) { this.retries = payload.specFileRetries - 1; } if (payload.content.isMultiRemote) { Object.assign(this, payload.content); } else { const { capabilities, ...connection } = payload.content; this.sessionId = payload.content.sessionId; this.capabilities = capabilities; this.config = { ...this.config, ...connection }; } } if (childProcess && payload.origin === "debugger" && payload.name === "start") { replQueue.add( childProcess, { prompt: `[${cid}] \u203A `, ...payload.params }, () => this.emit("message", Object.assign(payload, { cid })), (ev) => this.emit("message", ev) ); return replQueue.next(); } if (replQueue.isRunning && payload.origin === "debugger" && payload.name === "result") { replQueue.runningRepl?.onResult(payload.params); } this.emit("message", Object.assign(payload, { cid })); } _handleError(payload) { const { cid } = this; this.emit("error", Object.assign(payload, { cid })); } /** * Node calls this with `(code, signal)` where `code` is `null` whenever the * worker was terminated by a signal rather than exiting on its own, e.g. on * a segmentation fault. Only the second argument identifies that case. */ _handleExit(exitCode, signal = null) { const { cid, childProcess, specs, retries } = this; const wasKilledIntentionally = this.isKilled; delete this.childProcess; this.isBusy = false; this.isKilled = true; const resolvedExitCode = exitCode ?? getExitCodeForSignal(signal); const crashed = signal !== null && !wasKilledIntentionally && !GRACEFUL_SHUTDOWN_SIGNALS.includes(signal); if (crashed) { log.error( `Runner ${cid} was terminated by ${signal} and did not report an exit code. The spec file(s) ${specs.join(", ")} are reported as failed with exit code ${resolvedExitCode}. This usually means the process ran out of memory or a native module crashed.` ); } else { log.debug(`Runner ${cid} finished with exit code ${resolvedExitCode}`); } this.emit("exit", { cid, exitCode: resolvedExitCode, specs, retries, signal }); if (childProcess) { childProcess.kill("SIGTERM"); } } /** * Forcefully kill the worker process. * This is used when a worker doesn't respond to graceful shutdown * (e.g., when a test times out with pending async operations). * * @param signal - The signal to send (default: 'SIGTERM', use 'SIGKILL' for force kill) */ kill(signal = "SIGTERM") { if (!this.childProcess) { log.debug(`Worker ${this.cid} has no child process to kill`); return; } log.info(`Killing worker ${this.cid} with ${signal}`); try { this.childProcess.kill(signal); } catch (err) { log.warn(`Failed to kill worker ${this.cid}:`, err); } delete this.childProcess; this.isBusy = false; this.isKilled = true; } /** * sends message to sub process to execute functions in wdio-runner * @param command method to run in wdio-runner * @param args arguments for functions to call */ async postMessage(command, args, requiresSetup = false) { const { cid, configFile, capabilities, specs, retries, isBusy } = this; if (isBusy && !ACCEPTABLE_BUSY_COMMANDS.includes(command)) { return log.info(`worker with cid ${cid} already busy and can't take new commands`); } if (!this.childProcess) { this.childProcess = await this.startProcess(); } const cmd = { cid, command, configFile, args, caps: capabilities, specs, retries }; log.debug(`Send command ${command} to worker with cid "${cid}"`); this.isReady.then(async () => { if (requiresSetup) { await this.isSetup; } if (this.childProcess) { this.childProcess.send(cmd); } }).catch((err) => log.error(`Failed to send command to worker ${this.cid}: ${err.message}`)); this.isBusy = true; } }; // src/index.ts var log2 = logger2("@wdio/local-runner"); var LocalRunner = class { constructor(_options, config) { this._options = _options; this.config = config; } _options; config; workerPool = {}; daemon = null; stdout = new WritableStreamBuffer(BUFFER_OPTIONS); stderr = new WritableStreamBuffer(BUFFER_OPTIONS); /** * Runs before any service `onPrepare`, so drivers those services start inherit the display. */ async initialize() { try { this.daemon = await startDisplayDaemonFromConfig(this.config); } catch (error) { log2.warn("Failed to start a display server; continuing without a virtual display:", error); } } getWorkerCount() { return Object.keys(this.workerPool).length; } async run({ command, args, ...workerOptions }) { const workerCnt = this.getWorkerCount(); if (workerCnt >= process.stdout.getMaxListeners() - 2) { process.stdout.setMaxListeners(workerCnt + 2); process.stderr.setMaxListeners(workerCnt + 2); } const worker = new WorkerInstance( this.config, workerOptions, this.stdout, this.stderr ); this.workerPool[workerOptions.cid] = worker; await worker.postMessage(command, args); return worker; } /** * shutdown all worker processes * * @return {Promise} resolves when all worker have been shutdown or * a timeout was reached */ async shutdown() { log2.info("Shutting down spawned worker"); const workersToShutdown = []; for (const [cid, worker] of Object.entries(this.workerPool)) { const { capabilities, server, sessionId, config, isMultiRemote, instances } = worker; let payload = {}; if (config && config.watch && (sessionId || isMultiRemote)) { payload = { /** * The worker's own session id wins over any id stored on * config. Workers used to share one config object, so the * last session started would otherwise be deleted twice * and the others left open. */ config: { ...server, ...config, ...sessionId ? { sessionId } : {} }, capabilities, watch: true, isMultiRemote, instances }; } else if (!worker.isBusy) { delete this.workerPool[cid]; continue; } if (worker.isBusy) { workersToShutdown.push([cid, worker]); } await worker.postMessage("endSession", payload); } const shutdownResult = await new Promise((resolve) => { const timeout = setTimeout(() => { for (const [cid, worker] of workersToShutdown) { if (worker.isBusy && !worker.isKilled) { log2.warn(`Worker ${cid} did not shut down gracefully, force killing with SIGKILL`); worker.kill("SIGKILL"); } } resolve(false); }, SHUTDOWN_TIMEOUT); const interval = setInterval(() => { const busyWorker = Object.entries(this.workerPool).filter( ([, worker]) => worker.isBusy ).length; log2.info(`Waiting for ${busyWorker} to shut down gracefully`); if (busyWorker === 0) { clearTimeout(timeout); clearInterval(interval); log2.info("shutting down"); return resolve(true); } }, 250); }); return shutdownResult; } /** * Stop the display daemon. Runs after `onComplete`, so a driver or app a * service started in `onPrepare` still has its display while it is torn down. */ async dispose() { await this.daemon?.stop(); } }; export { LocalRunner as default };