build(deps-dev): apply updates
[poolifier.git] / tests / worker / thread-worker.test.mjs
CommitLineData
a074ffee 1import { expect } from 'expect'
bb3d0b23 2import { restore, stub } from 'sinon'
a074ffee 3import { ThreadWorker } from '../../lib/index.js'
f80125ca 4import { DEFAULT_TASK_NAME } from '../../lib/utils.js'
fd027a65 5
e1ffb94f 6describe('Thread worker test suite', () => {
e1ffb94f 7 class SpyWorker extends ThreadWorker {
85aeb3f3
JB
8 constructor (fn) {
9 super(fn)
bb3d0b23 10 this.port = { postMessage: stub().returns() }
e1ffb94f 11 }
74a2a194 12 }
74a2a194 13
bb3d0b23
JB
14 afterEach(() => {
15 restore()
16 })
17
f80125ca
JB
18 it('Verify that sync kill handler is called when worker is killed', () => {
19 const worker = new ThreadWorker(() => {}, {
20 killHandler: stub().returns()
21 })
22 worker.isMain = false
23 worker.port = {
24 postMessage: stub().returns(),
25 unref: stub().returns(),
26 close: stub().returns()
27 }
28 worker.handleKillMessage()
29 expect(worker.port.postMessage.calledOnce).toBe(true)
30 expect(worker.port.unref.calledOnce).toBe(true)
31 expect(worker.port.close.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 ThreadWorker({ fn1, fn2 })
43 expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({
44 status: false,
45 error: new TypeError('name parameter is not a string')
46 })
47 expect(worker.removeTaskFunction('', fn1)).toStrictEqual({
48 status: false,
49 error: new TypeError('name parameter is an empty string')
50 })
51 worker.port = {
52 postMessage: stub().returns()
53 }
54 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
55 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
56 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
57 expect(worker.taskFunctions.size).toBe(3)
58 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
59 worker.taskFunctions.get('fn1')
60 )
61 expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
62 status: false,
63 error: new Error(
64 'Cannot remove the task function with the default reserved name'
65 )
66 })
67 expect(worker.removeTaskFunction('fn1')).toStrictEqual({
68 status: false,
69 error: new Error(
70 'Cannot remove the task function used as the default task function'
71 )
72 })
73 worker.removeTaskFunction('fn2')
74 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
75 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
76 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
77 expect(worker.taskFunctions.size).toBe(2)
78 expect(worker.port.postMessage.calledOnce).toBe(true)
79 })
80
2431bdb4
JB
81 it('Verify that handleError() method is working properly', () => {
82 const error = new Error('Error as an error')
83 const worker = new ThreadWorker(() => {})
18ecc6c5 84 expect(worker.handleError(error)).toBeInstanceOf(Error)
2431bdb4
JB
85 expect(worker.handleError(error)).toStrictEqual(error)
86 const errorMessage = 'Error as a string'
87 expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
88 })
89
85aeb3f3 90 it('Verify worker invokes the postMessage() method on port property', () => {
74a2a194
APA
91 const worker = new SpyWorker(() => {})
92 worker.sendToMainWorker({ ok: 1 })
bb3d0b23 93 expect(worker.port.postMessage.calledOnce).toBe(true)
74a2a194 94 })
fd027a65 95})