refactor(ngcc): create new entry-point for cluster workers (#36637)

PR Close #36637
This commit is contained in:
Pete Bacon Darwin 2020-04-15 16:35:09 +01:00 committed by Matias Niemelä
parent 7e5e60b757
commit 443f5eee85
7 changed files with 232 additions and 247 deletions

View File

@ -5,11 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be * Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {FileSystem} from '../../../../src/ngtsc/file_system';
/// <reference types="node" />
import * as cluster from 'cluster';
import {AsyncLocker} from '../../locking/async_locker'; import {AsyncLocker} from '../../locking/async_locker';
import {Logger} from '../../logging/logger'; import {Logger} from '../../logging/logger';
import {PackageJsonUpdater} from '../../writing/package_json_updater'; import {PackageJsonUpdater} from '../../writing/package_json_updater';
@ -17,8 +13,6 @@ import {AnalyzeEntryPointsFn, CreateCompileFn, Executor} from '../api';
import {CreateTaskCompletedCallback} from '../tasks/api'; import {CreateTaskCompletedCallback} from '../tasks/api';
import {ClusterMaster} from './master'; import {ClusterMaster} from './master';
import {ClusterWorker} from './worker';
/** /**
* An `Executor` that processes tasks in parallel (on multiple processes) and completes * An `Executor` that processes tasks in parallel (on multiple processes) and completes
@ -26,26 +20,19 @@ import {ClusterWorker} from './worker';
*/ */
export class ClusterExecutor implements Executor { export class ClusterExecutor implements Executor {
constructor( constructor(
private workerCount: number, private logger: Logger, private workerCount: number, private fileSystem: FileSystem, private logger: Logger,
private pkgJsonUpdater: PackageJsonUpdater, private lockFile: AsyncLocker, private pkgJsonUpdater: PackageJsonUpdater, private lockFile: AsyncLocker,
private createTaskCompletedCallback: CreateTaskCompletedCallback) {} private createTaskCompletedCallback: CreateTaskCompletedCallback) {}
async execute(analyzeEntryPoints: AnalyzeEntryPointsFn, createCompileFn: CreateCompileFn): async execute(analyzeEntryPoints: AnalyzeEntryPointsFn, _createCompileFn: CreateCompileFn):
Promise<void> { Promise<void> {
if (cluster.isMaster) { return this.lockFile.lock(() => {
// This process is the cluster master. this.logger.debug(
return this.lockFile.lock(() => { `Running ngcc on ${this.constructor.name} (using ${this.workerCount} worker processes).`);
this.logger.debug(`Running ngcc on ${this.constructor.name} (using ${ const master = new ClusterMaster(
this.workerCount} worker processes).`); this.workerCount, this.fileSystem, this.logger, this.pkgJsonUpdater, analyzeEntryPoints,
const master = new ClusterMaster( this.createTaskCompletedCallback);
this.workerCount, this.logger, this.pkgJsonUpdater, analyzeEntryPoints, return master.run();
this.createTaskCompletedCallback); });
return master.run();
});
} else {
// This process is a cluster worker.
const worker = new ClusterWorker(this.logger, createCompileFn);
return worker.run();
}
} }
} }

View File

@ -10,7 +10,7 @@
import * as cluster from 'cluster'; import * as cluster from 'cluster';
import {resolve} from '../../../../src/ngtsc/file_system'; import {FileSystem} from '../../../../src/ngtsc/file_system';
import {Logger} from '../../logging/logger'; import {Logger} from '../../logging/logger';
import {PackageJsonUpdater} from '../../writing/package_json_updater'; import {PackageJsonUpdater} from '../../writing/package_json_updater';
import {AnalyzeEntryPointsFn} from '../api'; import {AnalyzeEntryPointsFn} from '../api';
@ -33,13 +33,16 @@ export class ClusterMaster {
private onTaskCompleted: TaskCompletedCallback; private onTaskCompleted: TaskCompletedCallback;
constructor( constructor(
private maxWorkerCount: number, private logger: Logger, private maxWorkerCount: number, private fileSystem: FileSystem, private logger: Logger,
private pkgJsonUpdater: PackageJsonUpdater, analyzeEntryPoints: AnalyzeEntryPointsFn, private pkgJsonUpdater: PackageJsonUpdater, analyzeEntryPoints: AnalyzeEntryPointsFn,
createTaskCompletedCallback: CreateTaskCompletedCallback) { createTaskCompletedCallback: CreateTaskCompletedCallback) {
if (!cluster.isMaster) { if (!cluster.isMaster) {
throw new Error('Tried to instantiate `ClusterMaster` on a worker process.'); throw new Error('Tried to instantiate `ClusterMaster` on a worker process.');
} }
// Set the worker entry-point
cluster.setupMaster({exec: this.fileSystem.resolve(__dirname, 'worker.js')});
this.taskQueue = analyzeEntryPoints(); this.taskQueue = analyzeEntryPoints();
this.onTaskCompleted = createTaskCompletedCallback(this.taskQueue); this.onTaskCompleted = createTaskCompletedCallback(this.taskQueue);
} }
@ -227,7 +230,7 @@ export class ClusterMaster {
JSON.stringify(msg)); JSON.stringify(msg));
} }
const expectedPackageJsonPath = resolve(task.entryPoint.path, 'package.json'); const expectedPackageJsonPath = this.fileSystem.resolve(task.entryPoint.path, 'package.json');
const parsedPackageJson = task.entryPoint.packageJson; const parsedPackageJson = task.entryPoint.packageJson;
if (expectedPackageJsonPath !== msg.packageJsonPath) { if (expectedPackageJsonPath !== msg.packageJsonPath) {

View File

@ -5,58 +5,102 @@
* Use of this source code is governed by an MIT-style license that can be * Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
/// <reference types="node" /> /// <reference types="node" />
import * as cluster from 'cluster'; import * as cluster from 'cluster';
import {Logger} from '../../logging/logger'; import {absoluteFrom, CachedFileSystem, getFileSystem, NodeJSFileSystem, setFileSystem} from '../../../../src/ngtsc/file_system';
import {CompileFn, CreateCompileFn} from '../api'; import {readConfiguration} from '../../../../src/perform_compile';
import {parseCommandLineOptions} from '../../command_line_options';
import {ConsoleLogger} from '../../logging/console_logger';
import {Logger, LogLevel} from '../../logging/logger';
import {getPathMappingsFromTsConfig} from '../../utils';
import {DirectPackageJsonUpdater} from '../../writing/package_json_updater';
import {CreateCompileFn} from '../api';
import {getCreateCompileFn} from '../create_compile_function';
import {stringifyTask} from '../tasks/utils'; import {stringifyTask} from '../tasks/utils';
import {MessageToWorker} from './api'; import {MessageToWorker} from './api';
import {ClusterPackageJsonUpdater} from './package_json_updater';
import {sendMessageToMaster} from './utils'; import {sendMessageToMaster} from './utils';
// Cluster worker entry point
/** if (require.main === module) {
* A cluster worker is responsible for processing one task (i.e. one format property for a specific process.title = 'ngcc (worker)';
* entry-point) at a time and reporting results back to the cluster master. setFileSystem(new CachedFileSystem(new NodeJSFileSystem()));
*/ let {
export class ClusterWorker { basePath,
private compile: CompileFn; targetEntryPointPath,
createNewEntryPointFormats = false,
constructor(private logger: Logger, createCompileFn: CreateCompileFn) { logger = new ConsoleLogger(LogLevel.info),
if (cluster.isMaster) { pathMappings,
throw new Error('Tried to instantiate `ClusterWorker` on the master process.'); errorOnFailedEntryPoint = false,
} enableI18nLegacyMessageIdFormat = true,
tsConfigPath
this.compile = createCompileFn( } = parseCommandLineOptions(process.argv.slice(2));
(_task, outcome, message) => (async () => {
sendMessageToMaster({type: 'task-completed', outcome, message})); try {
} if (!!targetEntryPointPath) {
// targetEntryPointPath forces us to error if an entry-point fails.
run(): Promise<void> { errorOnFailedEntryPoint = true;
// Listen for `ProcessTaskMessage`s and process tasks.
cluster.worker.on('message', (msg: MessageToWorker) => {
try {
switch (msg.type) {
case 'process-task':
this.logger.debug(
`[Worker #${cluster.worker.id}] Processing task: ${stringifyTask(msg.task)}`);
return this.compile(msg.task);
default:
throw new Error(
`[Worker #${cluster.worker.id}] Invalid message received: ${JSON.stringify(msg)}`);
}
} catch (err) {
sendMessageToMaster({
type: 'error',
error: (err instanceof Error) ? (err.stack || err.message) : err,
});
} }
});
// Return a promise that is never resolved. const fileSystem = getFileSystem();
return new Promise(() => undefined); const absBasePath = absoluteFrom(basePath);
} const projectPath = fileSystem.dirname(absBasePath);
const tsConfig =
tsConfigPath !== null ? readConfiguration(tsConfigPath || projectPath) : null;
if (pathMappings === undefined) {
pathMappings = getPathMappingsFromTsConfig(tsConfig, projectPath);
}
const pkgJsonUpdater =
new ClusterPackageJsonUpdater(new DirectPackageJsonUpdater(fileSystem));
// The function for creating the `compile()` function.
const createCompileFn = getCreateCompileFn(
fileSystem, logger, pkgJsonUpdater, createNewEntryPointFormats, errorOnFailedEntryPoint,
enableI18nLegacyMessageIdFormat, tsConfig, pathMappings);
await startWorker(logger, createCompileFn);
process.exitCode = 0;
} catch (e) {
console.error(e.stack || e.message);
process.exitCode = 1;
}
})();
} }
export async function startWorker(logger: Logger, createCompileFn: CreateCompileFn): Promise<void> {
if (cluster.isMaster) {
throw new Error('Tried to run cluster worker on the master process.');
}
const compile = createCompileFn(
(_task, outcome, message) => sendMessageToMaster({type: 'task-completed', outcome, message}));
// Listen for `ProcessTaskMessage`s and process tasks.
cluster.worker.on('message', (msg: MessageToWorker) => {
try {
switch (msg.type) {
case 'process-task':
logger.debug(
`[Worker #${cluster.worker.id}] Processing task: ${stringifyTask(msg.task)}`);
return compile(msg.task);
default:
throw new Error(
`[Worker #${cluster.worker.id}] Invalid message received: ${JSON.stringify(msg)}`);
}
} catch (err) {
sendMessageToMaster({
type: 'error',
error: (err instanceof Error) ? (err.stack || err.message) : err,
});
}
});
// Return a promise that is never resolved.
return new Promise(() => undefined);
}

View File

@ -178,7 +178,7 @@ function getExecutor(
// Execute in parallel. Use up to 8 CPU cores for workers, always reserving one for master. // Execute in parallel. Use up to 8 CPU cores for workers, always reserving one for master.
const workerCount = Math.min(8, os.cpus().length - 1); const workerCount = Math.min(8, os.cpus().length - 1);
return new ClusterExecutor( return new ClusterExecutor(
workerCount, logger, pkgJsonUpdater, locker, createTaskCompletedCallback); workerCount, fileSystem, logger, pkgJsonUpdater, locker, createTaskCompletedCallback);
} else { } else {
// Execute serially, on a single thread (async). // Execute serially, on a single thread (async).
return new SingleProcessExecutorAsync(logger, locker, createTaskCompletedCallback); return new SingleProcessExecutorAsync(logger, locker, createTaskCompletedCallback);

View File

@ -29,6 +29,7 @@ ts_library(
"@npm//magic-string", "@npm//magic-string",
"@npm//sourcemap-codec", "@npm//sourcemap-codec",
"@npm//typescript", "@npm//typescript",
"@npm//yargs",
], ],
) )

View File

@ -8,47 +8,44 @@
/// <reference types="node" /> /// <reference types="node" />
import {getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import * as cluster from 'cluster'; import * as cluster from 'cluster';
import {MockFileSystemNative} from '../../../../src/ngtsc/file_system/testing'; import {MockFileSystemNative, runInEachFileSystem} from '../../../../src/ngtsc/file_system/testing';
import {ClusterExecutor} from '../../../src/execution/cluster/executor'; import {ClusterExecutor} from '../../../src/execution/cluster/executor';
import {ClusterMaster} from '../../../src/execution/cluster/master'; import {ClusterMaster} from '../../../src/execution/cluster/master';
import {ClusterWorker} from '../../../src/execution/cluster/worker';
import {AsyncLocker} from '../../../src/locking/async_locker'; import {AsyncLocker} from '../../../src/locking/async_locker';
import {PackageJsonUpdater} from '../../../src/writing/package_json_updater'; import {PackageJsonUpdater} from '../../../src/writing/package_json_updater';
import {MockLockFile} from '../../helpers/mock_lock_file'; import {MockLockFile} from '../../helpers/mock_lock_file';
import {MockLogger} from '../../helpers/mock_logger'; import {MockLogger} from '../../helpers/mock_logger';
import {mockProperty} from '../../helpers/spy_utils'; import {mockProperty} from '../../helpers/spy_utils';
runInEachFileSystem(() => {
describe('ClusterExecutor', () => {
const runAsClusterMaster = mockProperty(cluster, 'isMaster');
let masterRunSpy: jasmine.Spy;
let mockLogger: MockLogger;
let lockFileLog: string[];
let mockLockFile: MockLockFile;
let locker: AsyncLocker;
let executor: ClusterExecutor;
let createTaskCompletedCallback: jasmine.Spy;
describe('ClusterExecutor', () => { beforeEach(() => {
const runAsClusterMaster = mockProperty(cluster, 'isMaster'); masterRunSpy = spyOn(ClusterMaster.prototype, 'run')
let masterRunSpy: jasmine.Spy; .and.returnValue(Promise.resolve('CusterMaster#run()' as any));
let workerRunSpy: jasmine.Spy; createTaskCompletedCallback = jasmine.createSpy('createTaskCompletedCallback');
let mockLogger: MockLogger;
let lockFileLog: string[];
let mockLockFile: MockLockFile;
let locker: AsyncLocker;
let executor: ClusterExecutor;
let createTaskCompletedCallback: jasmine.Spy;
beforeEach(() => { mockLogger = new MockLogger();
masterRunSpy = spyOn(ClusterMaster.prototype, 'run') lockFileLog = [];
.and.returnValue(Promise.resolve('CusterMaster#run()' as any)); mockLockFile = new MockLockFile(new MockFileSystemNative(), lockFileLog);
workerRunSpy = spyOn(ClusterWorker.prototype, 'run') locker = new AsyncLocker(mockLockFile, mockLogger, 200, 2);
.and.returnValue(Promise.resolve('CusterWorker#run()' as any)); executor = new ClusterExecutor(
createTaskCompletedCallback = jasmine.createSpy('createTaskCompletedCallback'); 42, getFileSystem(), mockLogger, null as unknown as PackageJsonUpdater, locker,
createTaskCompletedCallback);
});
mockLogger = new MockLogger(); describe('execute()', () => {
lockFileLog = [];
mockLockFile = new MockLockFile(new MockFileSystemNative(), lockFileLog);
locker = new AsyncLocker(mockLockFile, mockLogger, 200, 2);
executor = new ClusterExecutor(
42, mockLogger, null as unknown as PackageJsonUpdater, locker, createTaskCompletedCallback);
});
describe('execute()', () => {
describe('(on cluster master)', () => {
beforeEach(() => runAsClusterMaster(true)); beforeEach(() => runAsClusterMaster(true));
it('should log debug info about the executor', async () => { it('should log debug info about the executor', async () => {
@ -68,7 +65,6 @@ describe('ClusterExecutor', () => {
.toBe('CusterMaster#run()' as any); .toBe('CusterMaster#run()' as any);
expect(masterRunSpy).toHaveBeenCalledWith(); expect(masterRunSpy).toHaveBeenCalledWith();
expect(workerRunSpy).not.toHaveBeenCalled();
expect(analyzeEntryPointsSpy).toHaveBeenCalledWith(); expect(analyzeEntryPointsSpy).toHaveBeenCalledWith();
expect(createCompilerFnSpy).not.toHaveBeenCalled(); expect(createCompilerFnSpy).not.toHaveBeenCalled();
@ -102,7 +98,7 @@ describe('ClusterExecutor', () => {
}); });
executor = new ClusterExecutor( executor = new ClusterExecutor(
42, mockLogger, null as unknown as PackageJsonUpdater, locker, 42, getFileSystem(), mockLogger, null as unknown as PackageJsonUpdater, locker,
createTaskCompletedCallback); createTaskCompletedCallback);
let error = ''; let error = '';
try { try {
@ -122,7 +118,7 @@ describe('ClusterExecutor', () => {
}); });
executor = new ClusterExecutor( executor = new ClusterExecutor(
42, mockLogger, null as unknown as PackageJsonUpdater, locker, 42, getFileSystem(), mockLogger, null as unknown as PackageJsonUpdater, locker,
createTaskCompletedCallback); createTaskCompletedCallback);
let error = ''; let error = '';
try { try {
@ -135,36 +131,5 @@ describe('ClusterExecutor', () => {
expect(masterRunSpy).toHaveBeenCalled(); expect(masterRunSpy).toHaveBeenCalled();
}); });
}); });
describe('(on cluster worker)', () => {
beforeEach(() => runAsClusterMaster(false));
it('should not log debug info about the executor', async () => {
const anyFn: () => any = () => undefined;
await executor.execute(anyFn, anyFn);
expect(mockLogger.logs.debug).toEqual([]);
});
it('should delegate to `ClusterWorker#run()`', async () => {
const analyzeEntryPointsSpy = jasmine.createSpy('analyzeEntryPoints');
const createCompilerFnSpy = jasmine.createSpy('createCompilerFn');
expect(await executor.execute(analyzeEntryPointsSpy, createCompilerFnSpy))
.toBe('CusterWorker#run()' as any);
expect(masterRunSpy).not.toHaveBeenCalledWith();
expect(workerRunSpy).toHaveBeenCalled();
expect(analyzeEntryPointsSpy).not.toHaveBeenCalled();
expect(createCompilerFnSpy).toHaveBeenCalledWith(jasmine.any(Function));
});
it('should not call LockFile.write() or LockFile.remove()', async () => {
const anyFn: () => any = () => undefined;
await executor.execute(anyFn, anyFn);
expect(lockFileLog).toEqual([]);
});
});
}); });
}); });

View File

@ -11,13 +11,13 @@
import * as cluster from 'cluster'; import * as cluster from 'cluster';
import {EventEmitter} from 'events'; import {EventEmitter} from 'events';
import {ClusterWorker} from '../../../src/execution/cluster/worker'; import {startWorker} from '../../../src/execution/cluster/worker';
import {Task, TaskCompletedCallback, TaskProcessingOutcome} from '../../../src/execution/tasks/api'; import {Task, TaskCompletedCallback, TaskProcessingOutcome} from '../../../src/execution/tasks/api';
import {MockLogger} from '../../helpers/mock_logger'; import {MockLogger} from '../../helpers/mock_logger';
import {mockProperty} from '../../helpers/spy_utils'; import {mockProperty} from '../../helpers/spy_utils';
describe('ClusterWorker', () => { describe('startWorker()', () => {
const runAsClusterMaster = mockProperty(cluster, 'isMaster'); const runAsClusterMaster = mockProperty(cluster, 'isMaster');
const mockProcessSend = mockProperty(process, 'send'); const mockProcessSend = mockProperty(process, 'send');
let processSendSpy: jasmine.Spy; let processSendSpy: jasmine.Spy;
@ -35,131 +35,116 @@ describe('ClusterWorker', () => {
mockLogger = new MockLogger(); mockLogger = new MockLogger();
}); });
describe('constructor()', () => { describe('(on cluster master)', () => {
describe('(on cluster master)', () => { beforeEach(() => runAsClusterMaster(true));
beforeEach(() => runAsClusterMaster(true));
it('should throw an error', () => { it('should throw an error', async () => {
expect(() => new ClusterWorker(mockLogger, createCompileFnSpy)) await expectAsync(startWorker(mockLogger, createCompileFnSpy))
.toThrowError('Tried to instantiate `ClusterWorker` on the master process.'); .toBeRejectedWithError('Tried to run cluster worker on the master process.');
expect(createCompileFnSpy).not.toHaveBeenCalled(); expect(createCompileFnSpy).not.toHaveBeenCalled();
});
});
describe('(on cluster worker)', () => {
beforeEach(() => runAsClusterMaster(false));
it('should create the `compileFn()`', () => {
new ClusterWorker(mockLogger, createCompileFnSpy);
expect(createCompileFnSpy).toHaveBeenCalledWith(jasmine.any(Function));
});
it('should set up `compileFn()` to send `task-completed` messages to master', () => {
new ClusterWorker(mockLogger, createCompileFnSpy);
const onTaskCompleted: TaskCompletedCallback = createCompileFnSpy.calls.argsFor(0)[0];
onTaskCompleted(null as any, TaskProcessingOutcome.Processed, null);
expect(processSendSpy).toHaveBeenCalledTimes(1);
expect(processSendSpy)
.toHaveBeenCalledWith(
{type: 'task-completed', outcome: TaskProcessingOutcome.Processed, message: null});
processSendSpy.calls.reset();
onTaskCompleted(null as any, TaskProcessingOutcome.Failed, 'error message');
expect(processSendSpy).toHaveBeenCalledTimes(1);
expect(processSendSpy).toHaveBeenCalledWith({
type: 'task-completed',
outcome: TaskProcessingOutcome.Failed,
message: 'error message',
});
});
}); });
}); });
describe('run()', () => { describe('(on cluster worker)', () => {
describe( // The `cluster.worker` property is normally `undefined` on the master process and set to the
'(on cluster master)', // current `cluster.worker` on worker processes.
() => {/* No tests needed, becasue the constructor would have thrown. */}); const mockClusterWorker = mockProperty(cluster, 'worker');
describe('(on cluster worker)', () => { beforeEach(() => {
// The `cluster.worker` property is normally `undefined` on the master process and set to the runAsClusterMaster(false);
// current `cluster.Worker` on worker processes. mockClusterWorker(Object.assign(new EventEmitter(), {id: 42}) as cluster.Worker);
const mockClusterWorker = mockProperty(cluster, 'worker'); });
let worker: ClusterWorker;
beforeEach(() => { it('should create the `compileFn()`', () => {
runAsClusterMaster(false); startWorker(mockLogger, createCompileFnSpy);
mockClusterWorker(Object.assign(new EventEmitter(), {id: 42}) as cluster.Worker); expect(createCompileFnSpy).toHaveBeenCalledWith(jasmine.any(Function));
});
worker = new ClusterWorker(mockLogger, createCompileFnSpy); it('should set up `compileFn()` to send `task-completed` messages to master', () => {
startWorker(mockLogger, createCompileFnSpy);
const onTaskCompleted: TaskCompletedCallback = createCompileFnSpy.calls.argsFor(0)[0];
onTaskCompleted(null as any, TaskProcessingOutcome.Processed, null);
expect(processSendSpy).toHaveBeenCalledTimes(1);
expect(processSendSpy)
.toHaveBeenCalledWith(
{type: 'task-completed', outcome: TaskProcessingOutcome.Processed, message: null});
processSendSpy.calls.reset();
onTaskCompleted(null as any, TaskProcessingOutcome.Failed, 'error message');
expect(processSendSpy).toHaveBeenCalledTimes(1);
expect(processSendSpy).toHaveBeenCalledWith({
type: 'task-completed',
outcome: TaskProcessingOutcome.Failed,
message: 'error message',
});
});
it('should return a promise (that is never resolved)', done => {
const promise = startWorker(mockLogger, createCompileFnSpy);
expect(promise).toEqual(jasmine.any(Promise));
promise.then(
() => done.fail('Expected promise not to resolve'),
() => done.fail('Expected promise not to reject'));
// We can't wait forever to verify that the promise is not resolved, but at least verify
// that it is not resolved immediately.
setTimeout(done, 100);
});
it('should handle `process-task` messages', () => {
const mockTask = {
entryPoint: {name: 'foo'},
formatProperty: 'es2015',
processDts: true,
} as unknown as Task;
startWorker(mockLogger, createCompileFnSpy);
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
expect(compileFnSpy).toHaveBeenCalledWith(mockTask);
expect(processSendSpy).not.toHaveBeenCalled();
expect(mockLogger.logs.debug[0]).toEqual([
'[Worker #42] Processing task: {entryPoint: foo, formatProperty: es2015, processDts: true}',
]);
});
it('should send errors during task processing back to the master process', () => {
const mockTask = {
entryPoint: {name: 'foo'},
formatProperty: 'es2015',
processDts: true,
} as unknown as Task;
let err: string|Error;
compileFnSpy.and.callFake(() => {
throw err;
}); });
it('should return a promise (that is never resolved)', done => { startWorker(mockLogger, createCompileFnSpy);
const promise = worker.run();
expect(promise).toEqual(jasmine.any(Promise)); err = 'Error string.';
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
expect(processSendSpy).toHaveBeenCalledWith({type: 'error', error: err});
promise.then( err = new Error('Error object.');
() => done.fail('Expected promise not to resolve'), cluster.worker.emit('message', {type: 'process-task', task: mockTask});
() => done.fail('Expected promise not to reject')); expect(processSendSpy).toHaveBeenCalledWith({type: 'error', error: err.stack});
});
// We can't wait forever to verify that the promise is not resolved, but at least verify it('should throw, when an unknown message type is received', () => {
// that it is not resolved immediately. startWorker(mockLogger, createCompileFnSpy);
setTimeout(done, 100); cluster.worker.emit('message', {type: 'unknown', foo: 'bar'});
});
it('should handle `process-task` messages', () => { expect(compileFnSpy).not.toHaveBeenCalled();
const mockTask = { expect(processSendSpy).toHaveBeenCalledWith({
entryPoint: {name: 'foo'}, type: 'error',
formatProperty: 'es2015', error: jasmine.stringMatching(
processDts: true, 'Error: \\[Worker #42\\] Invalid message received: {"type":"unknown","foo":"bar"}'),
} as unknown as Task;
worker.run();
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
expect(compileFnSpy).toHaveBeenCalledWith(mockTask);
expect(processSendSpy).not.toHaveBeenCalled();
expect(mockLogger.logs.debug[0]).toEqual([
'[Worker #42] Processing task: {entryPoint: foo, formatProperty: es2015, processDts: true}',
]);
});
it('should send errors during task processing back to the master process', () => {
const mockTask = {
entryPoint: {name: 'foo'},
formatProperty: 'es2015',
processDts: true,
} as unknown as Task;
let err: string|Error;
compileFnSpy.and.callFake(() => {
throw err;
});
worker.run();
err = 'Error string.';
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
expect(processSendSpy).toHaveBeenCalledWith({type: 'error', error: err});
err = new Error('Error object.');
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
expect(processSendSpy).toHaveBeenCalledWith({type: 'error', error: err.stack});
});
it('should throw, when an unknown message type is received', () => {
worker.run();
cluster.worker.emit('message', {type: 'unknown', foo: 'bar'});
expect(compileFnSpy).not.toHaveBeenCalled();
expect(processSendSpy).toHaveBeenCalledWith({
type: 'error',
error: jasmine.stringMatching(
'Error: \\[Worker #42\\] Invalid message received: {"type":"unknown","foo":"bar"}'),
});
}); });
}); });
}); });