build(deps-dev): bump typescript
[poolifier.git] / tests / worker / thread-worker.test.mjs
CommitLineData
a074ffee 1import { expect } from 'expect'
bb3d0b23 2import { restore, stub } from 'sinon'
ded253e2 3
d35e5717
JB
4import { ThreadWorker } from '../../lib/index.cjs'
5import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
fd027a65 6
e1ffb94f 7describe('Thread 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 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
f80125ca
JB
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 })
5eb72b9e
JB
45 worker.port = {
46 postMessage: stub().returns()
47 }
f80125ca
JB
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 })
f80125ca
JB
56 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
57 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
58 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
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(Function)
77 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
78 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
79 expect(worker.taskFunctions.size).toBe(2)
80 expect(worker.port.postMessage.calledOnce).toBe(true)
81 })
82
2431bdb4
JB
83 it('Verify that handleError() method is working properly', () => {
84 const error = new Error('Error as an error')
85 const worker = new ThreadWorker(() => {})
18ecc6c5 86 expect(worker.handleError(error)).toBeInstanceOf(Error)
2431bdb4
JB
87 expect(worker.handleError(error)).toStrictEqual(error)
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 port property postMessage() method', () => {
93 const worker = new ThreadWorker(() => {})
94 worker.port = { postMessage: stub().returns() }
74a2a194 95 worker.sendToMainWorker({ ok: 1 })
bb3d0b23 96 expect(worker.port.postMessage.calledOnce).toBe(true)
74a2a194 97 })
fd027a65 98})