feat: switch to `EventEmitterAsyncRessource` type for `@types/node`
[poolifier.git] / tests / worker / thread-worker.test.mjs
index 6121995b690ce3549141d7960621ff9bd9015451..26da45e12bbe1ac63e3bfa169564faf7d59abcff 100644 (file)
@@ -1,6 +1,7 @@
 import { expect } from 'expect'
 import { restore, stub } from 'sinon'
 import { ThreadWorker } from '../../lib/index.js'
+import { DEFAULT_TASK_NAME } from '../../lib/utils.js'
 
 describe('Thread worker test suite', () => {
   class SpyWorker extends ThreadWorker {
@@ -14,6 +15,69 @@ describe('Thread worker test suite', () => {
     restore()
   })
 
+  it('Verify that sync kill handler is called when worker is killed', () => {
+    const worker = new ThreadWorker(() => {}, {
+      killHandler: stub().returns()
+    })
+    worker.isMain = false
+    worker.port = {
+      postMessage: stub().returns(),
+      unref: stub().returns(),
+      close: stub().returns()
+    }
+    worker.handleKillMessage()
+    expect(worker.port.postMessage.calledOnce).toBe(true)
+    expect(worker.port.unref.calledOnce).toBe(true)
+    expect(worker.port.close.calledOnce).toBe(true)
+    expect(worker.opts.killHandler.calledOnce).toBe(true)
+  })
+
+  it('Verify that removeTaskFunction() is working', () => {
+    const fn1 = () => {
+      return 1
+    }
+    const fn2 = () => {
+      return 2
+    }
+    const worker = new ThreadWorker({ fn1, fn2 })
+    expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({
+      status: false,
+      error: new TypeError('name parameter is not a string')
+    })
+    expect(worker.removeTaskFunction('', fn1)).toStrictEqual({
+      status: false,
+      error: new TypeError('name parameter is an empty string')
+    })
+    worker.port = {
+      postMessage: stub().returns()
+    }
+    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
+    expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+    expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
+    expect(worker.taskFunctions.size).toBe(3)
+    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
+      worker.taskFunctions.get('fn1')
+    )
+    expect(worker.removeTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
+      status: false,
+      error: new Error(
+        'Cannot remove the task function with the default reserved name'
+      )
+    })
+    expect(worker.removeTaskFunction('fn1')).toStrictEqual({
+      status: false,
+      error: new Error(
+        'Cannot remove the task function used as the default task function'
+      )
+    })
+    worker.removeTaskFunction('fn2')
+    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
+    expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+    expect(worker.taskFunctions.get('fn2')).toBeUndefined()
+    expect(worker.taskFunctions.size).toBe(2)
+    expect(worker.port.postMessage.calledOnce).toBe(true)
+  })
+
   it('Verify that handleError() method is working properly', () => {
     const error = new Error('Error as an error')
     const worker = new ThreadWorker(() => {})