Merge branch 'master' into task-functions-properties
[poolifier.git] / tests / worker / thread-worker.test.mjs
1 import { expect } from 'expect'
2 import { restore, stub } from 'sinon'
3
4 import { ThreadWorker } from '../../lib/index.cjs'
5 import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
6
7 describe('Thread worker test suite', () => {
8 afterEach(() => {
9 restore()
10 })
11
12 it('Verify worker properties value after initialization', () => {
13 const worker = new ThreadWorker(() => {})
14 expect(worker.isMain).toBe(true)
15 expect(worker.mainWorker).toBe(null)
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 ThreadWorker(() => {}, {
22 killHandler: stub().returns()
23 })
24 worker.isMain = false
25 worker.port = {
26 postMessage: stub().returns(),
27 unref: stub().returns(),
28 close: stub().returns()
29 }
30 worker.handleKillMessage()
31 expect(worker.port.postMessage.calledOnce).toBe(true)
32 expect(worker.port.unref.calledOnce).toBe(true)
33 expect(worker.port.close.calledOnce).toBe(true)
34 expect(worker.opts.killHandler.calledOnce).toBe(true)
35 })
36
37 it('Verify that removeTaskFunction() is working', () => {
38 const fn1 = () => {
39 return 1
40 }
41 const fn2 = () => {
42 return 2
43 }
44 const worker = new ThreadWorker({ fn1, fn2 })
45 worker.port = {
46 postMessage: stub().returns()
47 }
48 expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({
49 status: false,
50 error: new TypeError('name parameter is not a string')
51 })
52 expect(worker.removeTaskFunction('', fn1)).toStrictEqual({
53 status: false,
54 error: new TypeError('name parameter is an empty string')
55 })
56 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
57 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
58 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
59 expect(worker.taskFunctions.size).toBe(3)
60 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
61 worker.taskFunctions.get('fn1')
62 )
63 expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
64 status: false,
65 error: new Error(
66 'Cannot remove the task function with the default reserved name'
67 )
68 })
69 expect(worker.removeTaskFunction('fn1')).toStrictEqual({
70 status: false,
71 error: new Error(
72 'Cannot remove the task function used as the default task function'
73 )
74 })
75 worker.removeTaskFunction('fn2')
76 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
77 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
78 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
79 expect(worker.taskFunctions.size).toBe(2)
80 expect(worker.port.postMessage.calledOnce).toBe(true)
81 })
82
83 it('Verify that handleError() method is working properly', () => {
84 const error = new Error('Error as an error')
85 const worker = new ThreadWorker(() => {})
86 expect(worker.handleError(error)).toBeInstanceOf(Error)
87 expect(worker.handleError(error)).toStrictEqual(error)
88 const errorMessage = 'Error as a string'
89 expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
90 })
91
92 it('Verify that sendToMainWorker() method invokes the port property postMessage() method', () => {
93 const worker = new ThreadWorker(() => {})
94 worker.port = { postMessage: stub().returns() }
95 worker.sendToMainWorker({ ok: 1 })
96 expect(worker.port.postMessage.calledOnce).toBe(true)
97 })
98 })