-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- moved all non-CLI Node.js-specific sources into `lib/nodejs/` - renamed `WorkerPool` to `BufferedWorkerPool` and udpated filename accordingly. This is in anticipation of eventually adding a "streaming" worker pool that would communicate via IPC (e.g., `process.send()` and `process.on("message")`); see josdejong/workerpool#51 Signed-off-by: Christopher Hiller <[email protected]>
- Loading branch information
Showing
17 changed files
with
213 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
'use strict'; | ||
/** | ||
* @module IPC | ||
*/ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const { | ||
EVENT_SUITE_BEGIN, | ||
EVENT_SUITE_END, | ||
EVENT_TEST_FAIL, | ||
EVENT_TEST_PASS, | ||
EVENT_TEST_PENDING, | ||
EVENT_TEST_BEGIN, | ||
EVENT_TEST_END, | ||
EVENT_TEST_RETRY, | ||
EVENT_DELAY_BEGIN, | ||
EVENT_DELAY_END, | ||
EVENT_HOOK_BEGIN, | ||
EVENT_HOOK_END, | ||
EVENT_RUN_END | ||
} = require('../runner').constants; | ||
const {SerializableEvent, SerializableWorkerResult} = require('../serializer'); | ||
const debug = require('debug')('mocha:reporters:ipc'); | ||
const Base = require('../reporters/base'); | ||
|
||
/** | ||
* List of events to listen to; these will be buffered and sent | ||
* when `Mocha#run` is complete (via {@link IPC#done}). | ||
*/ | ||
const EVENT_NAMES = [ | ||
EVENT_SUITE_BEGIN, | ||
EVENT_SUITE_END, | ||
EVENT_TEST_BEGIN, | ||
EVENT_TEST_PENDING, | ||
EVENT_TEST_FAIL, | ||
EVENT_TEST_PASS, | ||
EVENT_TEST_RETRY, | ||
EVENT_TEST_END, | ||
EVENT_HOOK_BEGIN, | ||
EVENT_HOOK_END | ||
]; | ||
|
||
/** | ||
* Like {@link EVENT_NAMES}, except we expect these events to only be emitted | ||
* by the `Runner` once. | ||
*/ | ||
const ONCE_EVENT_NAMES = [EVENT_DELAY_BEGIN, EVENT_DELAY_END]; | ||
|
||
/** | ||
* The `IPC` 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. | ||
*/ | ||
class IPC extends Base { | ||
/** | ||
* Listens for {@link Runner} events and retains them in an `events` instance prop. | ||
* @param {Runner} runner | ||
*/ | ||
constructor(runner, opts) { | ||
super(runner, opts); | ||
|
||
/** | ||
* Retained list of events emitted from the {@link Runner} instance. | ||
* @type {IPCEvent[]} | ||
* @memberOf IPC | ||
*/ | ||
const 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. | ||
*/ | ||
const 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); | ||
|
||
EVENT_NAMES.forEach(evt => { | ||
runner.on(evt, createListener(evt)); | ||
}); | ||
ONCE_EVENT_NAMES.forEach(evt => { | ||
runner.once(evt, createListener(evt)); | ||
}); | ||
|
||
runner.once(EVENT_RUN_END, () => { | ||
debug('received EVENT_RUN_END'); | ||
listeners.forEach((listener, evt) => { | ||
runner.removeListener(evt, listener); | ||
listeners.delete(evt); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Calls the {@link Mocha#run} callback (`callback`) with the test failure | ||
* count and the array of {@link IPCEvent} objects. Resets the array. | ||
* @param {number} failures - Number of failed tests | ||
* @param {Function} callback - The callback passed to {@link Mocha#run}. | ||
*/ | ||
done(failures, callback) { | ||
callback(SerializableWorkerResult.create(this.events, failures)); | ||
this.events = []; // defensive | ||
} | ||
} | ||
|
||
/** | ||
* Serializable event data from a `Runner`. Keys of the `data` property | ||
* beginning with `__` will be converted into a function which returns the value | ||
* upon deserialization. | ||
* @typedef {Object} IPCEvent | ||
* @property {string} name - Event name | ||
* @property {object} data - Event parameters | ||
*/ | ||
|
||
module.exports = IPC; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.