build(deps-dev): apply updates
[poolifier.git] / tests / worker / cluster-worker.test.mjs
CommitLineData
a074ffee 1import { expect } from 'expect'
bb3d0b23 2import { restore, stub } from 'sinon'
ded253e2 3
d35e5717
JB
4import { ClusterWorker } from '../../lib/index.cjs'
5import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
fd027a65
S
6
7describe('Cluster worker test suite', () => {
bb3d0b23
JB
8 afterEach(() => {
9 restore()
10 })
11
394676d1
JB
12 it('Verify worker properties value after initialization', () => {
13 const worker = new ClusterWorker(() => {})
14 expect(worker.isMain).toBe(true)
15 expect(worker.mainWorker).toBe(undefined)
16 expect(worker.taskFunctions).toBeInstanceOf(Map)
17 expect(worker.taskFunctions.size).toBe(2)
18 })
19
f80125ca
JB
20 it('Verify that sync kill handler is called when worker is killed', () => {
21 const worker = new ClusterWorker(() => {}, {
3a502712 22 killHandler: stub().returns(),
f80125ca
JB
23 })
24 worker.isMain = false
25 worker.getMainWorker = stub().returns({
26 id: 1,
3a502712 27 send: stub().returns(),
f80125ca
JB
28 })
29 worker.handleKillMessage()
5eb72b9e 30 expect(worker.getMainWorker.calledTwice).toBe(true)
f80125ca
JB
31 expect(worker.getMainWorker().send.calledOnce).toBe(true)
32 expect(worker.opts.killHandler.calledOnce).toBe(true)
33 })
34
35 it('Verify that removeTaskFunction() is working', () => {
36 const fn1 = () => {
37 return 1
38 }
39 const fn2 = () => {
40 return 2
41 }
42 const worker = new ClusterWorker({ fn1, fn2 })
5eb72b9e
JB
43 worker.getMainWorker = stub().returns({
44 id: 1,
3a502712 45 send: stub().returns(),
5eb72b9e 46 })
f80125ca
JB
47 expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({
48 status: false,
3a502712 49 error: new TypeError('name parameter is not a string'),
f80125ca
JB
50 })
51 expect(worker.removeTaskFunction('', fn1)).toStrictEqual({
52 status: false,
3a502712 53 error: new TypeError('name parameter is an empty string'),
f80125ca 54 })
915040cc 55 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
3a502712 56 taskFunction: expect.any(Function),
915040cc
JB
57 })
58 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
3a502712 59 taskFunction: expect.any(Function),
915040cc
JB
60 })
61 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
3a502712 62 taskFunction: expect.any(Function),
915040cc 63 })
f80125ca
JB
64 expect(worker.taskFunctions.size).toBe(3)
65 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
66 worker.taskFunctions.get('fn1')
67 )
68 expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
69 status: false,
70 error: new Error(
71 'Cannot remove the task function with the default reserved name'
3a502712 72 ),
f80125ca
JB
73 })
74 expect(worker.removeTaskFunction('fn1')).toStrictEqual({
75 status: false,
76 error: new Error(
77 'Cannot remove the task function used as the default task function'
3a502712 78 ),
f80125ca
JB
79 })
80 worker.removeTaskFunction('fn2')
915040cc 81 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
3a502712 82 taskFunction: expect.any(Function),
915040cc
JB
83 })
84 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
3a502712 85 taskFunction: expect.any(Function),
915040cc 86 })
f80125ca
JB
87 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
88 expect(worker.taskFunctions.size).toBe(2)
5eb72b9e 89 expect(worker.getMainWorker.calledTwice).toBe(true)
f80125ca
JB
90 expect(worker.getMainWorker().send.calledOnce).toBe(true)
91 })
92
18ecc6c5
JB
93 it('Verify that handleError() method is working properly', () => {
94 const error = new Error('Error as an error')
95 const worker = new ClusterWorker(() => {})
96 expect(worker.handleError(error)).not.toBeInstanceOf(Error)
97 expect(worker.handleError(error)).toStrictEqual(error.message)
98 const errorMessage = 'Error as a string'
99 expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
100 })
101
5eb72b9e
JB
102 it('Verify that sendToMainWorker() method invokes the getMainWorker() and send() methods', () => {
103 const worker = new ClusterWorker(() => {})
104 worker.getMainWorker = stub().returns({
3a502712 105 send: stub().returns(),
5eb72b9e 106 })
f4fb3543 107 worker.sendToMainWorker({ ok: 1 })
5eb72b9e 108 expect(worker.getMainWorker.calledTwice).toBe(true)
bb3d0b23 109 expect(worker.getMainWorker().send.calledOnce).toBe(true)
f4fb3543 110 })
fd027a65 111})