build(deps-dev): apply updates
[poolifier.git] / tests / worker / cluster-worker.test.mjs
index 8ceb403ddffbf13b2720be8c215fe73bb4c94903..2ad7911380c9844a12157e0c50f1232f9e991a27 100644 (file)
@@ -1,30 +1,33 @@
 import { expect } from 'expect'
 import { restore, stub } from 'sinon'
-import { ClusterWorker } from '../../lib/index.js'
-import { DEFAULT_TASK_NAME } from '../../lib/utils.js'
 
-describe('Cluster worker test suite', () => {
-  const sendStub = stub().returns()
-  class SpyWorker extends ClusterWorker {
-    getMainWorker () {
-      return { send: sendStub }
-    }
-  }
+import { ClusterWorker } from '../../lib/index.cjs'
+import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
 
+describe('Cluster worker test suite', () => {
   afterEach(() => {
     restore()
   })
 
+  it('Verify worker properties value after initialization', () => {
+    const worker = new ClusterWorker(() => {})
+    expect(worker.isMain).toBe(true)
+    expect(worker.mainWorker).toBe(undefined)
+    expect(worker.taskFunctions).toBeInstanceOf(Map)
+    expect(worker.taskFunctions.size).toBe(2)
+  })
+
   it('Verify that sync kill handler is called when worker is killed', () => {
     const worker = new ClusterWorker(() => {}, {
-      killHandler: stub().returns()
+      killHandler: stub().returns(),
     })
     worker.isMain = false
     worker.getMainWorker = stub().returns({
       id: 1,
-      send: stub().returns()
+      send: stub().returns(),
     })
     worker.handleKillMessage()
+    expect(worker.getMainWorker.calledTwice).toBe(true)
     expect(worker.getMainWorker().send.calledOnce).toBe(true)
     expect(worker.opts.killHandler.calledOnce).toBe(true)
   })
@@ -37,21 +40,27 @@ describe('Cluster worker test suite', () => {
       return 2
     }
     const worker = new ClusterWorker({ fn1, fn2 })
+    worker.getMainWorker = stub().returns({
+      id: 1,
+      send: stub().returns(),
+    })
     expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({
       status: false,
-      error: new TypeError('name parameter is not a string')
+      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')
+      error: new TypeError('name parameter is an empty string'),
     })
-    worker.getMainWorker = stub().returns({
-      id: 1,
-      send: stub().returns()
+    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
+      taskFunction: expect.any(Function),
+    })
+    expect(worker.taskFunctions.get('fn1')).toStrictEqual({
+      taskFunction: expect.any(Function),
+    })
+    expect(worker.taskFunctions.get('fn2')).toStrictEqual({
+      taskFunction: expect.any(Function),
     })
-    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')
@@ -60,19 +69,24 @@ describe('Cluster worker test suite', () => {
       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(DEFAULT_TASK_NAME)).toStrictEqual({
+      taskFunction: expect.any(Function),
+    })
+    expect(worker.taskFunctions.get('fn1')).toStrictEqual({
+      taskFunction: expect.any(Function),
+    })
     expect(worker.taskFunctions.get('fn2')).toBeUndefined()
     expect(worker.taskFunctions.size).toBe(2)
+    expect(worker.getMainWorker.calledTwice).toBe(true)
     expect(worker.getMainWorker().send.calledOnce).toBe(true)
   })
 
@@ -85,9 +99,13 @@ describe('Cluster worker test suite', () => {
     expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
   })
 
-  it('Verify worker invokes the getMainWorker() and send() methods', () => {
-    const worker = new SpyWorker(() => {})
+  it('Verify that sendToMainWorker() method invokes the getMainWorker() and send() methods', () => {
+    const worker = new ClusterWorker(() => {})
+    worker.getMainWorker = stub().returns({
+      send: stub().returns(),
+    })
     worker.sendToMainWorker({ ok: 1 })
+    expect(worker.getMainWorker.calledTwice).toBe(true)
     expect(worker.getMainWorker().send.calledOnce).toBe(true)
   })
 })