test: cleanups
[poolifier.git] / tests / worker / cluster-worker.test.mjs
1 import { expect } from 'expect'
2 import { restore, stub } from 'sinon'
3
4 import { ClusterWorker } from '../../lib/index.cjs'
5 import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
6
7 describe('Cluster worker test suite', () => {
8 afterEach(() => {
9 restore()
10 })
11
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
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()
30 expect(worker.getMainWorker.calledTwice).toBe(true)
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 })
43 worker.getMainWorker = stub().returns({
44 id: 1,
45 send: stub().returns()
46 })
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 })
55 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
56 taskFunction: expect.any(Function)
57 })
58 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
59 taskFunction: expect.any(Function)
60 })
61 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
62 taskFunction: expect.any(Function)
63 })
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'
72 )
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'
78 )
79 })
80 worker.removeTaskFunction('fn2')
81 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
82 taskFunction: expect.any(Function)
83 })
84 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
85 taskFunction: expect.any(Function)
86 })
87 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
88 expect(worker.taskFunctions.size).toBe(2)
89 expect(worker.getMainWorker.calledTwice).toBe(true)
90 expect(worker.getMainWorker().send.calledOnce).toBe(true)
91 })
92
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
102 it('Verify that sendToMainWorker() method invokes the getMainWorker() and send() methods', () => {
103 const worker = new ClusterWorker(() => {})
104 worker.getMainWorker = stub().returns({
105 send: stub().returns()
106 })
107 worker.sendToMainWorker({ ok: 1 })
108 expect(worker.getMainWorker.calledTwice).toBe(true)
109 expect(worker.getMainWorker().send.calledOnce).toBe(true)
110 })
111 })