Skip to content

Commit

Permalink
parallel mode reporter improvements
Browse files Browse the repository at this point in the history
- `Suite`s, `Test`s, etc., now have unique identifiers which can optionally be used to re-created object references by a reporter which opts-in to the behavior
- A reporter can swap out the worker’s reporter for a custom one
- fixed an issue in rollup config causing weird problems
- changed how we define `global.Mocha` in `browser-entry.js` (tangential)
- removed `object.assign` polyfill (tangential)
- renamed misnamed `buffered-runner.spec.js` to `parallel-buffered-runner.spec.js`
- added a few unit tests
- refactored some tests; mainly modernizing some syntax
  • Loading branch information
boneskull committed Sep 30, 2020
1 parent 8f5d6a9 commit 08c37af
Show file tree
Hide file tree
Showing 16 changed files with 1,073 additions and 676 deletions.
24 changes: 15 additions & 9 deletions lib/hook.js
@@ -1,7 +1,8 @@
'use strict';

var Runnable = require('./runnable');
var inherits = require('./utils').inherits;
const {inherits, constants} = require('./utils');
const {MOCHA_ID_PROP_NAME} = constants;

/**
* Expose `Hook`.
Expand Down Expand Up @@ -63,16 +64,21 @@ Hook.prototype.serialize = function serialize() {
return {
$$isPending: this.isPending(),
$$titlePath: this.titlePath(),
ctx: {
currentTest: {
title: this.ctx && this.ctx.currentTest && this.ctx.currentTest.title
}
},
ctx:
this.ctx && this.ctx.currentTest
? {
currentTest: {
title: this.ctx.currentTest.title,
[MOCHA_ID_PROP_NAME]: this.ctx.currentTest.id
}
}
: {},
parent: {
root: this.parent.root,
title: this.parent.title
[MOCHA_ID_PROP_NAME]: this.parent.id
},
title: this.title,
type: this.type
type: this.type,
[MOCHA_ID_PROP_NAME]: this.id,
__mocha_partial__: true
};
};
148 changes: 131 additions & 17 deletions lib/nodejs/parallel-buffered-runner.js
Expand Up @@ -12,7 +12,12 @@ const {EVENT_RUN_BEGIN, EVENT_RUN_END} = Runner.constants;
const debug = require('debug')('mocha:parallel:parallel-buffered-runner');
const {BufferedWorkerPool} = require('./buffered-worker-pool');
const {setInterval, clearInterval} = global;
const {createMap} = require('../utils');
const {createMap, constants} = require('../utils');
const {MOCHA_ID_PROP_NAME} = constants;

const DEFAULT_WORKER_REPORTER = require.resolve(
'./reporters/parallel-buffered'
);

/**
* List of options to _not_ serialize for transmission to workers
Expand Down Expand Up @@ -68,7 +73,7 @@ const states = createMap({
/**
* This `Runner` delegates tests runs to worker threads. Does not execute any
* {@link Runnable}s by itself!
* @private
* @public
*/
class ParallelBufferedRunner extends Runner {
constructor(...args) {
Expand All @@ -88,6 +93,10 @@ class ParallelBufferedRunner extends Runner {
}
});

this._workerReporter = DEFAULT_WORKER_REPORTER;
this._linkPartialObjects = false;
this._linkedObjectMap = new Map();

this.once(Runner.constants.EVENT_RUN_END, () => {
this._state = COMPLETE;
});
Expand All @@ -98,12 +107,63 @@ class ParallelBufferedRunner extends Runner {
* @param {BufferedWorkerPool} pool - Worker pool
* @param {Options} options - Mocha options
* @returns {FileRunner} Mapping function
* @private
*/
_createFileRunner(pool, options) {
/**
* Emits event and sets `BAILING` state, if necessary.
* @param {Object} event - Event having `eventName`, maybe `data` and maybe `error`
* @param {number} failureCount - Failure count
*/
const emitEvent = (event, failureCount) => {
this.emit(event.eventName, event.data, event.error);
if (
this._state !== BAILING &&
event.data &&
event.data._bail &&
(failureCount || event.error)
) {
debug('run(): nonzero failure count & found bail flag');
// we need to let the events complete for this file, as the worker
// should run any cleanup hooks
this._state = BAILING;
}
};

/**
* Given an event, recursively find any objects in its data that have ID's, and create object references to already-seen objects.
* @param {Object} event - Event having `eventName`, maybe `data` and maybe `error`
*/
const linkEvent = event => {
const stack = [{parent: event, prop: 'data'}];
while (stack.length) {
const {parent, prop} = stack.pop();
const obj = parent[prop];
let newObj;
if (obj && typeof obj === 'object' && obj[MOCHA_ID_PROP_NAME]) {
const id = obj[MOCHA_ID_PROP_NAME];
newObj = this._linkedObjectMap.has(id)
? Object.assign(this._linkedObjectMap.get(id), obj)
: obj;
this._linkedObjectMap.set(id, newObj);
parent[prop] = newObj;
} else {
newObj = obj;
}
Object.keys(newObj).forEach(key => {
const value = obj[key];
if (value && typeof value === 'object' && value[MOCHA_ID_PROP_NAME]) {
stack.push({obj: value, parent: newObj, prop: key});
}
});
}
};

return async file => {
debug('run(): enqueueing test file %s', file);
try {
const {failureCount, events} = await pool.run(file, options);

if (this._state === BAILED) {
// short-circuit after a graceful bail. if this happens,
// some other worker has bailed.
Expand All @@ -119,20 +179,18 @@ class ParallelBufferedRunner extends Runner {
);
this.failures += failureCount; // can this ever be non-numeric?
let event = events.shift();
while (event) {
this.emit(event.eventName, event.data, event.error);
if (
this._state !== BAILING &&
event.data &&
event.data._bail &&
(failureCount || event.error)
) {
debug('run(): nonzero failure count & found bail flag');
// we need to let the events complete for this file, as the worker
// should run any cleanup hooks
this._state = BAILING;

if (this._linkPartialObjects) {
while (event) {
linkEvent(event);
emitEvent(event, failureCount);
event = events.shift();
}
} else {
while (event) {
emitEvent(event, failureCount);
event = events.shift();
}
event = events.shift();
}
if (this._state === BAILING) {
debug('run(): terminating pool due to "bail" flag');
Expand Down Expand Up @@ -166,6 +224,7 @@ class ParallelBufferedRunner extends Runner {
* Returns the listener for later call to `process.removeListener()`.
* @param {BufferedWorkerPool} pool - Worker pool
* @returns {SigIntListener} Listener
* @private
*/
_bindSigIntListener(pool) {
const sigIntListener = async () => {
Expand Down Expand Up @@ -209,15 +268,19 @@ class ParallelBufferedRunner extends Runner {
* @param {{files: string[], options: Options}} opts - Files to run and
* command-line options, respectively.
*/
run(callback, {files, options} = {}) {
run(callback, {files, options = {}} = {}) {
/**
* Listener on `Process.SIGINT` which tries to cleanly terminate the worker pool.
*/
let sigIntListener;

// assign the reporter the worker will use, which will be different than the
// main process' reporter
options = {...options, reporter: this._workerReporter};

// This function should _not_ return a `Promise`; its parent (`Runner#run`)
// returns this instance, so this should do the same. However, we want to make
// use of `async`/`await`, so we use this IIFE.

(async () => {
/**
* This is an interval that outputs stats about the worker pool every so often
Expand Down Expand Up @@ -293,6 +356,57 @@ class ParallelBufferedRunner extends Runner {
})();
return this;
}

/**
* Toggle partial object linking behavior; used for building object references from
* unique ID's.
* @param {boolean} [value] - If `true`, enable partial object linking, otherwise disable
* @returns {Runner}
* @chainable
* @public
* @example
* // this reporter needs proper object references when run in parallel mode
* class MyReporter() {
* constructor(runner) {
* this.runner.linkPartialObjects(true)
* .on(EVENT_SUITE_BEGIN, suite => {
// this Suite may be the same object...
* })
* .on(EVENT_TEST_BEGIN, test => {
* // ...as the `test.parent` property
* });
* }
* }
*/
linkPartialObjects(value) {
this._linkPartialObjects = Boolean(value);
return super.linkPartialObjects(value);
}

/**
* If this class is the `Runner` in use, then this is going to return `true`.
*
* For use by reporters.
* @returns {true}
* @public
*/
isParallelMode() {
return true;
}

/**
* Configures an alternate reporter for worker processes to use. Subclasses
* using worker processes should implement this.
* @public
* @param {string} path - Absolute path to alternate reporter for worker processes to use
* @returns {Runner}
* @throws When in serial mode
* @chainable
*/
workerReporter(reporter) {
this._workerReporter = reporter;
return this;
}
}

module.exports = ParallelBufferedRunner;
Expand Down
90 changes: 61 additions & 29 deletions lib/nodejs/reporters/parallel-buffered.js
@@ -1,7 +1,7 @@
/**
* "Buffered" reporter used internally by a worker process when running in parallel mode.
* @module reporters/parallel-buffered
* @private
* @module nodejs/reporters/parallel-buffered
* @public
*/

'use strict';
Expand Down Expand Up @@ -53,15 +53,16 @@ const EVENT_NAMES = [
const ONCE_EVENT_NAMES = [EVENT_DELAY_BEGIN, EVENT_DELAY_END];

/**
* The `ParallelBuffered` reporter is for use by concurrent runs. Instead of outputting
* to `STDOUT`, etc., it retains a list of events it receives and hands these
* off to the callback passed into {@link Mocha#run}. That callback will then
* return the data to the main process.
* @private
* The `ParallelBuffered` reporter is used by each worker process in "parallel"
* mode, by default. Instead of reporting to to `STDOUT`, etc., it retains a
* list of events it receives and hands these off to the callback passed into
* {@link Mocha#run}. That callback will then return the data to the main
* process.
* @public
*/
class ParallelBuffered extends Base {
/**
* Listens for {@link Runner} events and retains them in an `events` instance prop.
* Calls {@link ParallelBuffered#createListeners}
* @param {Runner} runner
*/
constructor(runner, opts) {
Expand All @@ -70,50 +71,81 @@ class ParallelBuffered extends Base {
/**
* Retained list of events emitted from the {@link Runner} instance.
* @type {BufferedEvent[]}
* @memberOf Buffered
* @public
*/
const events = (this.events = []);
this.events = [];

/**
* mapping of event names to listener functions we've created,
* so we can cleanly _remove_ them from the runner once it's completed.
* Map of `Runner` event names to listeners (for later teardown)
* @public
* @type {Map<string,EventListener>}
*/
const listeners = new Map();
this.listeners = new Map();

/**
* Creates a listener for event `eventName` and adds it to the `listeners`
* map. This is a defensive measure, so that we don't a) leak memory or b)
* remove _other_ listeners that may not be associated with this reporter.
* @param {string} eventName - Event name
*/
const createListener = eventName =>
listeners
.set(eventName, (runnable, err) => {
events.push(SerializableEvent.create(eventName, runnable, err));
})
.get(eventName);
this.createListeners(runner);
}

/**
* Returns a new listener which saves event data in memory to
* {@link ParallelBuffered#events}. Listeners are indexed by `eventName` and stored
* in {@link ParallelBuffered#listeners}. This is a defensive measure, so that we
* don't a) leak memory or b) remove _other_ listeners that may not be
* associated with this reporter.
*
* Subclasses could override this behavior.
*
* @public
* @param {string} eventName - Name of event to create listener for
* @returns {EventListener}
*/
createListener(eventName) {
const listener = (runnable, err) => {
this.events.push(SerializableEvent.create(eventName, runnable, err));
};
return this.listeners.set(eventName, listener).get(eventName);
}

/**
* Creates event listeners (using {@link ParallelBuffered#createListener}) for each
* reporter-relevant event emitted by a {@link Runner}. This array is drained when
* {@link ParallelBuffered#done} is called by {@link Runner#run}.
*
* Subclasses could override this behavior.
* @public
* @param {Runner} runner - Runner instance
* @returns {ParallelBuffered}
* @chainable
*/
createListeners(runner) {
EVENT_NAMES.forEach(evt => {
runner.on(evt, createListener(evt));
runner.on(evt, this.createListener(evt));
});
ONCE_EVENT_NAMES.forEach(evt => {
runner.once(evt, createListener(evt));
runner.once(evt, this.createListener(evt));
});

runner.once(EVENT_RUN_END, () => {
debug('received EVENT_RUN_END');
listeners.forEach((listener, evt) => {
this.listeners.forEach((listener, evt) => {
runner.removeListener(evt, listener);
listeners.delete(evt);
this.listeners.delete(evt);
});
});

return this;
}

/**
* Calls the {@link Mocha#run} callback (`callback`) with the test failure
* count and the array of {@link BufferedEvent} objects. Resets the array.
*
* This is called directly by `Runner#run` and should not be called by any other consumer.
*
* Subclasses could override this.
*
* @param {number} failures - Number of failed tests
* @param {Function} callback - The callback passed to {@link Mocha#run}.
* @public
*/
done(failures, callback) {
callback(SerializableWorkerResult.create(this.events, failures));
Expand Down

0 comments on commit 08c37af

Please sign in to comment.