| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {ClusterWorker} from '../../../src/execution/cluster/worker'; | 
					
						
							| 
									
										
										
										
											2020-03-14 13:38:27 +00:00
										 |  |  | import {Task, TaskCompletedCallback, TaskProcessingOutcome} from '../../../src/execution/tasks/api'; | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  | import {MockLogger} from '../../helpers/mock_logger'; | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  | import {mockProperty} from '../../helpers/spy_utils'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('ClusterWorker', () => { | 
					
						
							|  |  |  |   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 21:02:57 +02:00
										 |  |  |   let mockLogger: MockLogger; | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     compileFnSpy = jasmine.createSpy('compileFn'); | 
					
						
							|  |  |  |     createCompileFnSpy = jasmine.createSpy('createCompileFn').and.returnValue(compileFnSpy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     processSendSpy = jasmine.createSpy('process.send'); | 
					
						
							|  |  |  |     mockProcessSend(processSendSpy); | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     mockLogger = new MockLogger(); | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('constructor()', () => { | 
					
						
							|  |  |  |     describe('(on cluster master)', () => { | 
					
						
							|  |  |  |       beforeEach(() => runAsClusterMaster(true)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should throw an error', () => { | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  |         expect(() => new ClusterWorker(mockLogger, createCompileFnSpy)) | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |             .toThrowError('Tried to instantiate `ClusterWorker` on the master process.'); | 
					
						
							|  |  |  |         expect(createCompileFnSpy).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('(on cluster worker)', () => { | 
					
						
							|  |  |  |       beforeEach(() => runAsClusterMaster(false)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should create the `compileFn()`', () => { | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  |         new ClusterWorker(mockLogger, createCompileFnSpy); | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |         expect(createCompileFnSpy).toHaveBeenCalledWith(jasmine.any(Function)); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 13:20:51 +00:00
										 |  |  |       it('should set up `compileFn()` to send `task-completed` messages to master', () => { | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  |         new ClusterWorker(mockLogger, createCompileFnSpy); | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |         const onTaskCompleted: TaskCompletedCallback = createCompileFnSpy.calls.argsFor(0)[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 13:20:51 +00:00
										 |  |  |         onTaskCompleted(null as any, TaskProcessingOutcome.Processed, null); | 
					
						
							| 
									
										
										
										
											2020-02-28 08:53:07 +01:00
										 |  |  |         expect(processSendSpy).toHaveBeenCalledTimes(1); | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |         expect(processSendSpy).toHaveBeenCalledWith({ | 
					
						
							|  |  |  |           type: 'task-completed', | 
					
						
							|  |  |  |           outcome: TaskProcessingOutcome.Processed, | 
					
						
							| 
									
										
										
										
											2020-03-14 13:20:51 +00:00
										 |  |  |           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', | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('run()', () => { | 
					
						
							|  |  |  |     describe( | 
					
						
							|  |  |  |         '(on cluster master)', | 
					
						
							|  |  |  |         () => {/* No tests needed, becasue the constructor would have thrown. */}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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'); | 
					
						
							|  |  |  |       let worker: ClusterWorker; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       beforeEach(() => { | 
					
						
							|  |  |  |         runAsClusterMaster(false); | 
					
						
							|  |  |  |         mockClusterWorker(Object.assign(new EventEmitter(), {id: 42}) as cluster.Worker); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  |         worker = new ClusterWorker(mockLogger, createCompileFnSpy); | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should return a promise (that is never resolved)', done => { | 
					
						
							|  |  |  |         const promise = worker.run(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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', () => { | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  |         const mockTask = { | 
					
						
							|  |  |  |           entryPoint: {name: 'foo'}, | 
					
						
							|  |  |  |           formatProperty: 'es2015', | 
					
						
							|  |  |  |           processDts: true, | 
					
						
							|  |  |  |         } as unknown as Task; | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         worker.run(); | 
					
						
							|  |  |  |         cluster.worker.emit('message', {type: 'process-task', task: mockTask}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(compileFnSpy).toHaveBeenCalledWith(mockTask); | 
					
						
							|  |  |  |         expect(processSendSpy).not.toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(mockLogger.logs.debug[0]).toEqual([ | 
					
						
							|  |  |  |           '[Worker #42] Processing task: {entryPoint: foo, formatProperty: es2015, processDts: true}', | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should send errors during task processing back to the master process', () => { | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  |         const mockTask = { | 
					
						
							|  |  |  |           entryPoint: {name: 'foo'}, | 
					
						
							|  |  |  |           formatProperty: 'es2015', | 
					
						
							|  |  |  |           processDts: true, | 
					
						
							|  |  |  |         } as unknown as Task; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |         let err: string|Error; | 
					
						
							|  |  |  |         compileFnSpy.and.callFake(() => { throw err; }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         worker.run(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         err = 'Error string.'; | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  |         cluster.worker.emit('message', {type: 'process-task', task: mockTask}); | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |         expect(processSendSpy).toHaveBeenCalledWith({type: 'error', error: err}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         err = new Error('Error object.'); | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  |         cluster.worker.emit('message', {type: 'process-task', task: mockTask}); | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |         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( | 
					
						
							| 
									
										
										
										
											2019-12-05 21:02:57 +02:00
										 |  |  |               'Error: \\[Worker #42\\] Invalid message received: {"type":"unknown","foo":"bar"}'), | 
					
						
							| 
									
										
										
										
											2019-08-29 18:47:54 +03:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |