feat: switch to `EventEmitterAsyncRessource` type from `@types/node`
[poolifier.git] / tests / worker / cluster-worker.test.mjs
1 import { expect } from 'expect'
2 import { restore, stub } from 'sinon'
3 import { ClusterWorker } from '../../lib/index.js'
4 import { DEFAULT_TASK_NAME } from '../../lib/utils.js'
5
6 describe('Cluster worker test suite', () => {
7 const sendStub = stub().returns()
8 class SpyWorker extends ClusterWorker {
9 getMainWorker () {
10 return { send: sendStub }
11 }
12 }
13
14 afterEach(() => {
15 restore()
16 })
17
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
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
88 it('Verify worker invokes the getMainWorker() and send() methods', () => {
89 const worker = new SpyWorker(() => {})
90 worker.sendToMainWorker({ ok: 1 })
91 expect(worker.getMainWorker().send.calledOnce).toBe(true)
92 })
93 })