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