Merge dependabot/npm_and_yarn/examples/typescript/websocket-server-pool/ws-hybrid...
[poolifier.git] / tests / worker / thread-worker.test.mjs
... / ...
CommitLineData
1import { expect } from 'expect'
2import { restore, stub } from 'sinon'
3
4import { ThreadWorker } from '../../lib/index.cjs'
5import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
6
7describe('Thread worker test suite', () => {
8 afterEach(() => {
9 restore()
10 })
11
12 it('Verify that sync kill handler is called when worker is killed', () => {
13 const worker = new ThreadWorker(() => {}, {
14 killHandler: stub().returns()
15 })
16 worker.isMain = false
17 worker.port = {
18 postMessage: stub().returns(),
19 unref: stub().returns(),
20 close: stub().returns()
21 }
22 worker.handleKillMessage()
23 expect(worker.port.postMessage.calledOnce).toBe(true)
24 expect(worker.port.unref.calledOnce).toBe(true)
25 expect(worker.port.close.calledOnce).toBe(true)
26 expect(worker.opts.killHandler.calledOnce).toBe(true)
27 })
28
29 it('Verify that removeTaskFunction() is working', () => {
30 const fn1 = () => {
31 return 1
32 }
33 const fn2 = () => {
34 return 2
35 }
36 const worker = new ThreadWorker({ fn1, fn2 })
37 worker.port = {
38 postMessage: stub().returns()
39 }
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 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
49 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
50 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
51 expect(worker.taskFunctions.size).toBe(3)
52 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
53 worker.taskFunctions.get('fn1')
54 )
55 expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
56 status: false,
57 error: new Error(
58 'Cannot remove the task function with the default reserved name'
59 )
60 })
61 expect(worker.removeTaskFunction('fn1')).toStrictEqual({
62 status: false,
63 error: new Error(
64 'Cannot remove the task function used as the default task function'
65 )
66 })
67 worker.removeTaskFunction('fn2')
68 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
69 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
70 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
71 expect(worker.taskFunctions.size).toBe(2)
72 expect(worker.port.postMessage.calledOnce).toBe(true)
73 })
74
75 it('Verify that handleError() method is working properly', () => {
76 const error = new Error('Error as an error')
77 const worker = new ThreadWorker(() => {})
78 expect(worker.handleError(error)).toBeInstanceOf(Error)
79 expect(worker.handleError(error)).toStrictEqual(error)
80 const errorMessage = 'Error as a string'
81 expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
82 })
83
84 it('Verify that sendToMainWorker() method invokes the port property postMessage() method', () => {
85 const worker = new ThreadWorker(() => {})
86 worker.port = { postMessage: stub().returns() }
87 worker.sendToMainWorker({ ok: 1 })
88 expect(worker.port.postMessage.calledOnce).toBe(true)
89 })
90})