test: verify worker properties value
[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(() => {}, {
22 killHandler: stub().returns()
23 })
24 worker.isMain = false
25 worker.getMainWorker = stub().returns({
26 id: 1,
27 send: stub().returns()
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,
45 send: stub().returns()
46 })
f80125ca
JB
47 expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({
48 status: false,
49 error: new TypeError('name parameter is not a string')
50 })
51 expect(worker.removeTaskFunction('', fn1)).toStrictEqual({
52 status: false,
53 error: new TypeError('name parameter is an empty string')
54 })
f80125ca
JB
55 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
56 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
57 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
58 expect(worker.taskFunctions.size).toBe(3)
59 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
60 worker.taskFunctions.get('fn1')
61 )
62 expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
63 status: false,
64 error: new Error(
65 'Cannot remove the task function with the default reserved name'
66 )
67 })
68 expect(worker.removeTaskFunction('fn1')).toStrictEqual({
69 status: false,
70 error: new Error(
71 'Cannot remove the task function used as the default task function'
72 )
73 })
74 worker.removeTaskFunction('fn2')
75 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
76 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
77 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
78 expect(worker.taskFunctions.size).toBe(2)
5eb72b9e 79 expect(worker.getMainWorker.calledTwice).toBe(true)
f80125ca
JB
80 expect(worker.getMainWorker().send.calledOnce).toBe(true)
81 })
82
18ecc6c5
JB
83 it('Verify that handleError() method is working properly', () => {
84 const error = new Error('Error as an error')
85 const worker = new ClusterWorker(() => {})
86 expect(worker.handleError(error)).not.toBeInstanceOf(Error)
87 expect(worker.handleError(error)).toStrictEqual(error.message)
88 const errorMessage = 'Error as a string'
89 expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
90 })
91
5eb72b9e
JB
92 it('Verify that sendToMainWorker() method invokes the getMainWorker() and send() methods', () => {
93 const worker = new ClusterWorker(() => {})
94 worker.getMainWorker = stub().returns({
95 send: stub().returns()
96 })
f4fb3543 97 worker.sendToMainWorker({ ok: 1 })
5eb72b9e 98 expect(worker.getMainWorker.calledTwice).toBe(true)
bb3d0b23 99 expect(worker.getMainWorker().send.calledOnce).toBe(true)
f4fb3543 100 })
fd027a65 101})