|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const serializeJavascript = require('serialize-javascript'); |
| 4 | +const workerpool = require('workerpool'); |
| 5 | +const {deserialize} = require('./serializer'); |
| 6 | +const debug = require('debug')('mocha:parallel:pool'); |
| 7 | +const {cpus} = require('os'); |
| 8 | +const {createInvalidArgumentTypeError} = require('./errors'); |
| 9 | + |
| 10 | +const WORKER_PATH = require.resolve('./worker.js'); |
| 11 | + |
| 12 | +/** |
| 13 | + * A mapping of Mocha `Options` objects to serialized values. |
| 14 | + * |
| 15 | + * This is helpful because we tend to same the same options over and over |
| 16 | + * over IPC. |
| 17 | + * @type {WeakMap<Options,string>} |
| 18 | + */ |
| 19 | +let optionsCache = new WeakMap(); |
| 20 | + |
| 21 | +/** |
| 22 | + * Count of CPU cores |
| 23 | + */ |
| 24 | +const CPU_COUNT = cpus().length; |
| 25 | + |
| 26 | +/** |
| 27 | + * Default max number of workers. |
| 28 | + * |
| 29 | + * We are already using one core for the main process, so assume only _n - 1_ are left. |
| 30 | + * |
| 31 | + * This is a reasonable default, but YMMV. |
| 32 | + */ |
| 33 | +const DEFAULT_MAX_WORKERS = CPU_COUNT - 1; |
| 34 | + |
| 35 | +/** |
| 36 | + * These options are passed into the [workerpool](https://npm.im/workerpool) module. |
| 37 | + * @type {Partial<WorkerPoolOptions>} |
| 38 | + */ |
| 39 | +const WORKER_POOL_DEFAULT_OPTS = { |
| 40 | + // use child processes, not worker threads! |
| 41 | + workerType: 'process', |
| 42 | + // ensure the same flags sent to `node` for this `mocha` invocation are passed |
| 43 | + // along to children |
| 44 | + forkOpts: {execArgv: process.execArgv}, |
| 45 | + maxWorkers: DEFAULT_MAX_WORKERS |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * A wrapper around a third-party worker pool implementation. |
| 50 | + */ |
| 51 | +class WorkerPool { |
| 52 | + constructor(opts = WORKER_POOL_DEFAULT_OPTS) { |
| 53 | + const maxWorkers = Math.max(1, opts.maxWorkers); |
| 54 | + |
| 55 | + if (maxWorkers < 2) { |
| 56 | + debug( |
| 57 | + 'not enough CPU cores available (%d) to run multiple jobs; avoid --parallel on this machine', |
| 58 | + CPU_COUNT |
| 59 | + ); |
| 60 | + } else if (maxWorkers >= CPU_COUNT) { |
| 61 | + debug( |
| 62 | + '%d concurrent job(s) requested, but only %d core(s) available', |
| 63 | + maxWorkers, |
| 64 | + CPU_COUNT |
| 65 | + ); |
| 66 | + } |
| 67 | + debug( |
| 68 | + 'run(): starting worker pool of max size %d, using node args: %s', |
| 69 | + maxWorkers, |
| 70 | + process.execArgv.join(' ') |
| 71 | + ); |
| 72 | + |
| 73 | + this.options = Object.assign({}, opts, {maxWorkers}); |
| 74 | + this._pool = workerpool.pool(WORKER_PATH, this.options); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Terminates all workers in the pool. |
| 79 | + * @param {boolean} [force] - Whether to force-kill workers. By default, lets workers finish their current task before termination. |
| 80 | + * @private |
| 81 | + * @returns {Promise<void>} |
| 82 | + */ |
| 83 | + async terminate(force = false) { |
| 84 | + return this._pool.terminate(force); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Adds a test file run to the worker pool queue for execution by a worker process. |
| 89 | + * |
| 90 | + * Handles serialization/deserialization. |
| 91 | + * |
| 92 | + * @param {string} filepath - Filepath of test |
| 93 | + * @param {Options} [options] - Options for Mocha instance |
| 94 | + * @private |
| 95 | + * @returns {Promise<SerializedWorkerResult>} |
| 96 | + */ |
| 97 | + async run(filepath, options = {}) { |
| 98 | + if (!filepath || typeof filepath !== 'string') { |
| 99 | + throw createInvalidArgumentTypeError( |
| 100 | + 'Expected a non-empty filepath', |
| 101 | + 'filepath', |
| 102 | + 'string' |
| 103 | + ); |
| 104 | + } |
| 105 | + const serializedOptions = WorkerPool.serializeOptions(options); |
| 106 | + const result = await this._pool.exec('run', [filepath, serializedOptions]); |
| 107 | + return deserialize(result); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns stats about the state of the worker processes in the pool. |
| 112 | + * |
| 113 | + * Used for debugging. |
| 114 | + * |
| 115 | + * @private |
| 116 | + */ |
| 117 | + stats() { |
| 118 | + return this._pool.stats(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Instantiates a {@link WorkerPool}. |
| 123 | + */ |
| 124 | + static create(...args) { |
| 125 | + return new WorkerPool(...args); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Given Mocha options object `opts`, serialize into a format suitable for |
| 130 | + * transmission over IPC. |
| 131 | + * |
| 132 | + * @param {Options} [opts] - Mocha options |
| 133 | + * @private |
| 134 | + * @returns {string} Serialized options |
| 135 | + */ |
| 136 | + static serializeOptions(opts = {}) { |
| 137 | + if (!optionsCache.has(opts)) { |
| 138 | + const serialized = serializeJavascript(opts, { |
| 139 | + unsafe: true, // this means we don't care about XSS |
| 140 | + ignoreFunction: true // do not serialize functions |
| 141 | + }); |
| 142 | + optionsCache.set(opts, serialized); |
| 143 | + debug( |
| 144 | + 'serializeOptions(): serialized options %O to: %s', |
| 145 | + opts, |
| 146 | + serialized |
| 147 | + ); |
| 148 | + } |
| 149 | + return optionsCache.get(opts); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Resets internal cache of serialized options objects. |
| 154 | + * |
| 155 | + * For testing/debugging |
| 156 | + * @private |
| 157 | + */ |
| 158 | + static resetOptionsCache() { |
| 159 | + optionsCache = new WeakMap(); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +exports.WorkerPool = WorkerPool; |
0 commit comments