X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fworker%2Fcluster-worker.test.mjs;h=8ceb403ddffbf13b2720be8c215fe73bb4c94903;hb=0bc53e954d6d832092db4ea6ccea8b0f8f071ebe;hp=9349743c50835777ebc642c4d60cea5b0e9a3fee;hpb=bb3d0b23ab89cf90ac2977403d74bb755f4f6b07;p=poolifier.git diff --git a/tests/worker/cluster-worker.test.mjs b/tests/worker/cluster-worker.test.mjs index 9349743c..8ceb403d 100644 --- a/tests/worker/cluster-worker.test.mjs +++ b/tests/worker/cluster-worker.test.mjs @@ -1,6 +1,7 @@ import { expect } from 'expect' import { restore, stub } from 'sinon' import { ClusterWorker } from '../../lib/index.js' +import { DEFAULT_TASK_NAME } from '../../lib/utils.js' describe('Cluster worker test suite', () => { const sendStub = stub().returns() @@ -14,6 +15,67 @@ describe('Cluster worker test suite', () => { restore() }) + it('Verify that sync kill handler is called when worker is killed', () => { + const worker = new ClusterWorker(() => {}, { + killHandler: stub().returns() + }) + worker.isMain = false + worker.getMainWorker = stub().returns({ + id: 1, + send: stub().returns() + }) + worker.handleKillMessage() + expect(worker.getMainWorker().send.calledOnce).toBe(true) + expect(worker.opts.killHandler.calledOnce).toBe(true) + }) + + it('Verify that removeTaskFunction() is working', () => { + const fn1 = () => { + return 1 + } + const fn2 = () => { + return 2 + } + const worker = new ClusterWorker({ fn1, fn2 }) + expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({ + status: false, + error: new TypeError('name parameter is not a string') + }) + expect(worker.removeTaskFunction('', fn1)).toStrictEqual({ + status: false, + error: new TypeError('name parameter is an empty string') + }) + worker.getMainWorker = stub().returns({ + id: 1, + send: stub().returns() + }) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) + expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) + expect(worker.taskFunctions.size).toBe(3) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( + worker.taskFunctions.get('fn1') + ) + expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({ + status: false, + error: new Error( + 'Cannot remove the task function with the default reserved name' + ) + }) + expect(worker.removeTaskFunction('fn1')).toStrictEqual({ + status: false, + error: new Error( + 'Cannot remove the task function used as the default task function' + ) + }) + worker.removeTaskFunction('fn2') + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) + expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get('fn2')).toBeUndefined() + expect(worker.taskFunctions.size).toBe(2) + expect(worker.getMainWorker().send.calledOnce).toBe(true) + }) + it('Verify that handleError() method is working properly', () => { const error = new Error('Error as an error') const worker = new ClusterWorker(() => {})