2019-08-29 11:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2019-08-29 11:47:54 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// <reference types="node" />
|
|
|
|
|
|
|
|
import * as cluster from 'cluster';
|
|
|
|
import {EventEmitter} from 'events';
|
|
|
|
|
2020-04-29 14:28:13 -04:00
|
|
|
import {AbsoluteFsPath} from '../../../../src/ngtsc/file_system';
|
2020-05-14 15:06:12 -04:00
|
|
|
import {MockLogger} from '../../../../src/ngtsc/logging/testing';
|
2020-04-29 14:28:13 -04:00
|
|
|
import {CreateCompileFn} from '../../../src/execution/api';
|
2020-04-15 11:35:09 -04:00
|
|
|
import {startWorker} from '../../../src/execution/cluster/worker';
|
2020-03-14 09:38:27 -04:00
|
|
|
import {Task, TaskCompletedCallback, TaskProcessingOutcome} from '../../../src/execution/tasks/api';
|
2020-04-29 14:28:13 -04:00
|
|
|
import {FileToWrite} from '../../../src/rendering/utils';
|
2020-04-29 14:28:27 -04:00
|
|
|
import {mockProperty, spyProperty} from '../../helpers/spy_utils';
|
2019-08-29 11:47:54 -04:00
|
|
|
|
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
describe('startWorker()', () => {
|
2019-08-29 11:47:54 -04:00
|
|
|
const runAsClusterMaster = mockProperty(cluster, 'isMaster');
|
|
|
|
const mockProcessSend = mockProperty(process, 'send');
|
|
|
|
let processSendSpy: jasmine.Spy;
|
|
|
|
let compileFnSpy: jasmine.Spy;
|
|
|
|
let createCompileFnSpy: jasmine.Spy;
|
2019-12-05 14:02:57 -05:00
|
|
|
let mockLogger: MockLogger;
|
2019-08-29 11:47:54 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
compileFnSpy = jasmine.createSpy('compileFn');
|
|
|
|
createCompileFnSpy = jasmine.createSpy('createCompileFn').and.returnValue(compileFnSpy);
|
|
|
|
|
|
|
|
processSendSpy = jasmine.createSpy('process.send');
|
|
|
|
mockProcessSend(processSendSpy);
|
2019-12-05 14:02:57 -05:00
|
|
|
|
|
|
|
mockLogger = new MockLogger();
|
2019-08-29 11:47:54 -04:00
|
|
|
});
|
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
describe('(on cluster master)', () => {
|
|
|
|
beforeEach(() => runAsClusterMaster(true));
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
it('should throw an error', async () => {
|
|
|
|
await expectAsync(startWorker(mockLogger, createCompileFnSpy))
|
|
|
|
.toBeRejectedWithError('Tried to run cluster worker on the master process.');
|
|
|
|
expect(createCompileFnSpy).not.toHaveBeenCalled();
|
2019-08-29 11:47:54 -04:00
|
|
|
});
|
2020-04-15 11:35:09 -04:00
|
|
|
});
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
describe('(on cluster worker)', () => {
|
|
|
|
// The `cluster.worker` property is normally `undefined` on the master process and set to the
|
|
|
|
// current `cluster.worker` on worker processes.
|
|
|
|
const mockClusterWorker = mockProperty(cluster, 'worker');
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
runAsClusterMaster(false);
|
|
|
|
mockClusterWorker(Object.assign(new EventEmitter(), {id: 42}) as cluster.Worker);
|
|
|
|
});
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
it('should create the `compileFn()`', () => {
|
|
|
|
startWorker(mockLogger, createCompileFnSpy);
|
2020-04-29 14:28:11 -04:00
|
|
|
expect(createCompileFnSpy).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function));
|
2019-08-29 11:47:54 -04:00
|
|
|
});
|
|
|
|
|
2020-04-29 14:28:13 -04:00
|
|
|
it('should set up `compileFn()` to send `transformed-files` messages to master', () => {
|
|
|
|
startWorker(mockLogger, createCompileFnSpy);
|
|
|
|
|
|
|
|
const mockTransformedFiles: FileToWrite[] = [
|
|
|
|
{path: '/foo' as AbsoluteFsPath, contents: 'FOO'},
|
|
|
|
{path: '/bar' as AbsoluteFsPath, contents: 'BAR'},
|
|
|
|
];
|
|
|
|
const beforeWritingFiles: Parameters<CreateCompileFn>[0] =
|
|
|
|
createCompileFnSpy.calls.argsFor(0)[0];
|
|
|
|
|
|
|
|
beforeWritingFiles(mockTransformedFiles);
|
|
|
|
|
|
|
|
expect(processSendSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(processSendSpy)
|
|
|
|
.toHaveBeenCalledWith(
|
|
|
|
{type: 'transformed-files', files: ['/foo', '/bar']}, jasmine.any(Function));
|
|
|
|
});
|
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
it('should set up `compileFn()` to send `task-completed` messages to master', () => {
|
|
|
|
startWorker(mockLogger, createCompileFnSpy);
|
2020-04-29 14:28:11 -04:00
|
|
|
const onTaskCompleted: TaskCompletedCallback = createCompileFnSpy.calls.argsFor(0)[1];
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
onTaskCompleted(null as any, TaskProcessingOutcome.Processed, null);
|
|
|
|
expect(processSendSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(processSendSpy)
|
|
|
|
.toHaveBeenCalledWith(
|
2020-04-29 14:28:13 -04:00
|
|
|
{type: 'task-completed', outcome: TaskProcessingOutcome.Processed, message: null},
|
|
|
|
jasmine.any(Function));
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
processSendSpy.calls.reset();
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
onTaskCompleted(null as any, TaskProcessingOutcome.Failed, 'error message');
|
|
|
|
expect(processSendSpy).toHaveBeenCalledTimes(1);
|
2020-04-29 14:28:13 -04:00
|
|
|
expect(processSendSpy)
|
|
|
|
.toHaveBeenCalledWith(
|
|
|
|
{
|
|
|
|
type: 'task-completed',
|
|
|
|
outcome: TaskProcessingOutcome.Failed,
|
|
|
|
message: 'error message',
|
|
|
|
},
|
|
|
|
jasmine.any(Function));
|
2020-04-15 11:35:09 -04:00
|
|
|
});
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
it('should return a promise (that is never resolved)', done => {
|
|
|
|
const promise = startWorker(mockLogger, createCompileFnSpy);
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
expect(promise).toEqual(jasmine.any(Promise));
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
promise.then(
|
|
|
|
() => done.fail('Expected promise not to resolve'),
|
|
|
|
() => done.fail('Expected promise not to reject'));
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
// 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);
|
|
|
|
});
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
it('should handle `process-task` messages', () => {
|
|
|
|
const mockTask = {
|
|
|
|
entryPoint: {name: 'foo'},
|
|
|
|
formatProperty: 'es2015',
|
|
|
|
processDts: true,
|
|
|
|
} as unknown as Task;
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
startWorker(mockLogger, createCompileFnSpy);
|
|
|
|
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
expect(compileFnSpy).toHaveBeenCalledWith(mockTask);
|
|
|
|
expect(processSendSpy).not.toHaveBeenCalled();
|
2019-12-05 14:02:57 -05:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
expect(mockLogger.logs.debug[0]).toEqual([
|
|
|
|
'[Worker #42] Processing task: {entryPoint: foo, formatProperty: es2015, processDts: true}',
|
|
|
|
]);
|
|
|
|
});
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
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;
|
2019-12-05 14:02:57 -05:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
let err: string|Error;
|
|
|
|
compileFnSpy.and.callFake(() => {
|
|
|
|
throw err;
|
|
|
|
});
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
startWorker(mockLogger, createCompileFnSpy);
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
err = 'Error string.';
|
|
|
|
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
|
2020-04-29 14:28:13 -04:00
|
|
|
expect(processSendSpy)
|
|
|
|
.toHaveBeenCalledWith({type: 'error', error: err}, jasmine.any(Function));
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
err = new Error('Error object.');
|
|
|
|
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
|
2020-04-29 14:28:13 -04:00
|
|
|
expect(processSendSpy)
|
|
|
|
.toHaveBeenCalledWith({type: 'error', error: err.stack}, jasmine.any(Function));
|
2020-04-15 11:35:09 -04:00
|
|
|
});
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-29 14:28:27 -04:00
|
|
|
it('should exit on `ENOMEM` errors during task processing', () => {
|
|
|
|
const processExitSpy = jasmine.createSpy('process.exit');
|
|
|
|
const {
|
|
|
|
setMockValue: mockProcessExit,
|
|
|
|
installSpies: installProcessExitSpies,
|
|
|
|
uninstallSpies: uninstallProcessExitSpies,
|
|
|
|
} = spyProperty(process, 'exit');
|
|
|
|
|
|
|
|
try {
|
|
|
|
installProcessExitSpies();
|
|
|
|
mockProcessExit(processExitSpy as unknown as typeof process.exit);
|
|
|
|
|
|
|
|
const mockTask = {
|
|
|
|
entryPoint: {name: 'foo'},
|
|
|
|
formatProperty: 'es2015',
|
|
|
|
processDts: true,
|
|
|
|
} as unknown as Task;
|
|
|
|
|
|
|
|
const noMemError = Object.assign(new Error('ENOMEM: not enough memory'), {code: 'ENOMEM'});
|
|
|
|
compileFnSpy.and.throwError(noMemError);
|
|
|
|
|
|
|
|
startWorker(mockLogger, createCompileFnSpy);
|
|
|
|
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
|
|
|
|
|
|
|
|
expect(mockLogger.logs.warn).toEqual([[`[Worker #42] ${noMemError.stack}`]]);
|
|
|
|
expect(processExitSpy).toHaveBeenCalledWith(1);
|
|
|
|
expect(processSendSpy).not.toHaveBeenCalled();
|
|
|
|
} finally {
|
|
|
|
uninstallProcessExitSpies();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
it('should throw, when an unknown message type is received', () => {
|
|
|
|
startWorker(mockLogger, createCompileFnSpy);
|
|
|
|
cluster.worker.emit('message', {type: 'unknown', foo: 'bar'});
|
2019-08-29 11:47:54 -04:00
|
|
|
|
2020-04-15 11:35:09 -04:00
|
|
|
expect(compileFnSpy).not.toHaveBeenCalled();
|
2020-04-29 14:28:13 -04:00
|
|
|
expect(processSendSpy)
|
|
|
|
.toHaveBeenCalledWith(
|
|
|
|
{
|
|
|
|
type: 'error',
|
|
|
|
error: jasmine.stringMatching(
|
|
|
|
'Error: \\[Worker #42\\] Invalid message received: {"type":"unknown","foo":"bar"}'),
|
|
|
|
},
|
|
|
|
jasmine.any(Function));
|
2019-08-29 11:47:54 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|