| 1 | import { expect } from 'expect' |
| 2 | import { restore, stub } from 'sinon' |
| 3 | import { ClusterWorker } from '../../lib/index.cjs' |
| 4 | import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs' |
| 5 | |
| 6 | describe('Cluster worker test suite', () => { |
| 7 | afterEach(() => { |
| 8 | restore() |
| 9 | }) |
| 10 | |
| 11 | it('Verify that sync kill handler is called when worker is killed', () => { |
| 12 | const worker = new ClusterWorker(() => {}, { |
| 13 | killHandler: stub().returns() |
| 14 | }) |
| 15 | worker.isMain = false |
| 16 | worker.getMainWorker = stub().returns({ |
| 17 | id: 1, |
| 18 | send: stub().returns() |
| 19 | }) |
| 20 | worker.handleKillMessage() |
| 21 | expect(worker.getMainWorker.calledTwice).toBe(true) |
| 22 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
| 23 | expect(worker.opts.killHandler.calledOnce).toBe(true) |
| 24 | }) |
| 25 | |
| 26 | it('Verify that removeTaskFunction() is working', () => { |
| 27 | const fn1 = () => { |
| 28 | return 1 |
| 29 | } |
| 30 | const fn2 = () => { |
| 31 | return 2 |
| 32 | } |
| 33 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 34 | worker.getMainWorker = stub().returns({ |
| 35 | id: 1, |
| 36 | send: stub().returns() |
| 37 | }) |
| 38 | expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({ |
| 39 | status: false, |
| 40 | error: new TypeError('name parameter is not a string') |
| 41 | }) |
| 42 | expect(worker.removeTaskFunction('', fn1)).toStrictEqual({ |
| 43 | status: false, |
| 44 | error: new TypeError('name parameter is an empty string') |
| 45 | }) |
| 46 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 47 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 48 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 49 | expect(worker.taskFunctions.size).toBe(3) |
| 50 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 51 | worker.taskFunctions.get('fn1') |
| 52 | ) |
| 53 | expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({ |
| 54 | status: false, |
| 55 | error: new Error( |
| 56 | 'Cannot remove the task function with the default reserved name' |
| 57 | ) |
| 58 | }) |
| 59 | expect(worker.removeTaskFunction('fn1')).toStrictEqual({ |
| 60 | status: false, |
| 61 | error: new Error( |
| 62 | 'Cannot remove the task function used as the default task function' |
| 63 | ) |
| 64 | }) |
| 65 | worker.removeTaskFunction('fn2') |
| 66 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
| 67 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 68 | expect(worker.taskFunctions.get('fn2')).toBeUndefined() |
| 69 | expect(worker.taskFunctions.size).toBe(2) |
| 70 | expect(worker.getMainWorker.calledTwice).toBe(true) |
| 71 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
| 72 | }) |
| 73 | |
| 74 | it('Verify that handleError() method is working properly', () => { |
| 75 | const error = new Error('Error as an error') |
| 76 | const worker = new ClusterWorker(() => {}) |
| 77 | expect(worker.handleError(error)).not.toBeInstanceOf(Error) |
| 78 | expect(worker.handleError(error)).toStrictEqual(error.message) |
| 79 | const errorMessage = 'Error as a string' |
| 80 | expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage) |
| 81 | }) |
| 82 | |
| 83 | it('Verify that sendToMainWorker() method invokes the getMainWorker() and send() methods', () => { |
| 84 | const worker = new ClusterWorker(() => {}) |
| 85 | worker.getMainWorker = stub().returns({ |
| 86 | send: stub().returns() |
| 87 | }) |
| 88 | worker.sendToMainWorker({ ok: 1 }) |
| 89 | expect(worker.getMainWorker.calledTwice).toBe(true) |
| 90 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
| 91 | }) |
| 92 | }) |