@@ -11,7 +11,6 @@ const serializeJavascript = require('serialize-javascript');
1111const workerpool = require ( 'workerpool' ) ;
1212const { deserialize} = require ( './serializer' ) ;
1313const debug = require ( 'debug' ) ( 'mocha:parallel:buffered-worker-pool' ) ;
14- const { cpus} = require ( 'os' ) ;
1514const { createInvalidArgumentTypeError} = require ( '../errors' ) ;
1615
1716const WORKER_PATH = require . resolve ( './worker.js' ) ;
@@ -25,20 +24,6 @@ const WORKER_PATH = require.resolve('./worker.js');
2524 */
2625let optionsCache = new WeakMap ( ) ;
2726
28- /**
29- * Count of CPU cores
30- */
31- const CPU_COUNT = cpus ( ) . length ;
32-
33- /**
34- * Default max number of workers.
35- *
36- * We are already using one core for the main process, so assume only _n - 1_ are left.
37- *
38- * This is a reasonable default, but YMMV.
39- */
40- const DEFAULT_MAX_WORKERS = CPU_COUNT - 1 ;
41-
4227/**
4328 * These options are passed into the [workerpool](https://npm.im/workerpool) module.
4429 * @type {Partial<WorkerPoolOptions> }
@@ -49,29 +34,38 @@ const WORKER_POOL_DEFAULT_OPTS = {
4934 // ensure the same flags sent to `node` for this `mocha` invocation are passed
5035 // along to children
5136 forkOpts : { execArgv : process . execArgv } ,
52- maxWorkers : DEFAULT_MAX_WORKERS
37+ maxWorkers : workerpool . cpus - 1
5338} ;
5439
5540/**
5641 * A wrapper around a third-party worker pool implementation.
5742 * @private
5843 */
5944class BufferedWorkerPool {
45+ /**
46+ * Creates an underlying worker pool instance; determines max worker count
47+ * @param {Partial<WorkerPoolOptions> } [opts] - Options
48+ */
6049 constructor ( opts = { } ) {
61- const maxWorkers = Math . max ( 1 , opts . maxWorkers || 0 ) ;
50+ const maxWorkers = Math . max (
51+ 1 ,
52+ typeof opts . maxWorkers === 'undefined'
53+ ? WORKER_POOL_DEFAULT_OPTS . maxWorkers
54+ : opts . maxWorkers
55+ ) ;
6256
63- if ( maxWorkers < 2 ) {
64- /* istanbul ignore next */
57+ /* istanbul ignore next */
58+ if ( workerpool . cpus < 2 ) {
59+ // TODO: decide whether we should warn
6560 debug (
66- 'not enough CPU cores available (%d) to run multiple jobs; avoid --parallel on this machine' ,
67- CPU_COUNT
61+ 'not enough CPU cores available to run multiple jobs; avoid --parallel on this machine'
6862 ) ;
69- } else if ( maxWorkers >= CPU_COUNT ) {
70- /* istanbul ignore next */
63+ } else if ( maxWorkers >= workerpool . cpus ) {
64+ // TODO: decide whether we should warn
7165 debug (
7266 '%d concurrent job(s) requested, but only %d core(s) available' ,
7367 maxWorkers ,
74- CPU_COUNT
68+ workerpool . cpus
7569 ) ;
7670 }
7771 /* istanbul ignore next */
0 commit comments