Merge branch 'master' into feature/task-functions
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index 93e7c256dabaad671a78e53bc1923418bfa6ceef..99ebbfa1b9c930a2dd6182e517c71f2f23ef2d35 100644 (file)
@@ -1243,7 +1243,124 @@ describe('Abstract pool test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify that listTaskFunctions() is working', async () => {
+  it('Verify that hasTaskFunction() is working', async () => {
+    const dynamicThreadPool = new DynamicThreadPool(
+      Math.floor(numberOfWorkers / 2),
+      numberOfWorkers,
+      './tests/worker-files/thread/testMultipleTaskFunctionsWorker.js'
+    )
+    await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1)
+    expect(dynamicThreadPool.hasTaskFunction(DEFAULT_TASK_NAME)).toBe(true)
+    expect(dynamicThreadPool.hasTaskFunction('jsonIntegerSerialization')).toBe(
+      true
+    )
+    expect(dynamicThreadPool.hasTaskFunction('factorial')).toBe(true)
+    expect(dynamicThreadPool.hasTaskFunction('fibonacci')).toBe(true)
+    expect(dynamicThreadPool.hasTaskFunction('unknown')).toBe(false)
+    await dynamicThreadPool.destroy()
+    const fixedClusterPool = new FixedClusterPool(
+      numberOfWorkers,
+      './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js'
+    )
+    await waitPoolEvents(fixedClusterPool, PoolEvents.ready, 1)
+    expect(fixedClusterPool.hasTaskFunction(DEFAULT_TASK_NAME)).toBe(true)
+    expect(fixedClusterPool.hasTaskFunction('jsonIntegerSerialization')).toBe(
+      true
+    )
+    expect(fixedClusterPool.hasTaskFunction('factorial')).toBe(true)
+    expect(fixedClusterPool.hasTaskFunction('fibonacci')).toBe(true)
+    expect(fixedClusterPool.hasTaskFunction('unknown')).toBe(false)
+    await fixedClusterPool.destroy()
+  })
+
+  it('Verify that addTaskFunction() is working', async () => {
+    const dynamicThreadPool = new DynamicThreadPool(
+      Math.floor(numberOfWorkers / 2),
+      numberOfWorkers,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1)
+    await expect(
+      dynamicThreadPool.addTaskFunction(0, () => {})
+    ).rejects.toThrowError(new TypeError('name argument must be a string'))
+    await expect(
+      dynamicThreadPool.addTaskFunction('', () => {})
+    ).rejects.toThrowError(
+      new TypeError('name argument must not be an empty string')
+    )
+    await expect(
+      dynamicThreadPool.addTaskFunction('test', 0)
+    ).rejects.toThrowError(new TypeError('fn argument must be a function'))
+    await expect(
+      dynamicThreadPool.addTaskFunction('test', '')
+    ).rejects.toThrowError(new TypeError('fn argument must be a function'))
+    expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
+      DEFAULT_TASK_NAME,
+      'test'
+    ])
+    const echoTaskFunction = data => {
+      return data
+    }
+    await expect(
+      dynamicThreadPool.addTaskFunction('echo', echoTaskFunction)
+    ).resolves.toBe(true)
+    expect(dynamicThreadPool.taskFunctions.size).toBe(1)
+    expect(dynamicThreadPool.taskFunctions.get('echo')).toStrictEqual(
+      echoTaskFunction
+    )
+    expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
+      DEFAULT_TASK_NAME,
+      'test',
+      'echo'
+    ])
+    const taskFunctionData = { test: 'test' }
+    const echoResult = await dynamicThreadPool.execute(taskFunctionData, 'echo')
+    expect(echoResult).toStrictEqual(taskFunctionData)
+    await dynamicThreadPool.destroy()
+  })
+
+  it('Verify that removeTaskFunction() is working', async () => {
+    const dynamicThreadPool = new DynamicThreadPool(
+      Math.floor(numberOfWorkers / 2),
+      numberOfWorkers,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1)
+    expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
+      DEFAULT_TASK_NAME,
+      'test'
+    ])
+    await expect(
+      dynamicThreadPool.removeTaskFunction('test')
+    ).rejects.toThrowError(
+      new Error('Cannot remove a task function not handled on the pool side')
+    )
+    const echoTaskFunction = data => {
+      return data
+    }
+    await dynamicThreadPool.addTaskFunction('echo', echoTaskFunction)
+    expect(dynamicThreadPool.taskFunctions.size).toBe(1)
+    expect(dynamicThreadPool.taskFunctions.get('echo')).toStrictEqual(
+      echoTaskFunction
+    )
+    expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
+      DEFAULT_TASK_NAME,
+      'test',
+      'echo'
+    ])
+    await expect(dynamicThreadPool.removeTaskFunction('echo')).resolves.toBe(
+      true
+    )
+    expect(dynamicThreadPool.taskFunctions.size).toBe(0)
+    expect(dynamicThreadPool.taskFunctions.get('echo')).toBeUndefined()
+    expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
+      DEFAULT_TASK_NAME,
+      'test'
+    ])
+    await dynamicThreadPool.destroy()
+  })
+
+  it('Verify that listTaskFunctionNames() is working', async () => {
     const dynamicThreadPool = new DynamicThreadPool(
       Math.floor(numberOfWorkers / 2),
       numberOfWorkers,
@@ -1256,6 +1373,7 @@ describe('Abstract pool test suite', () => {
       'factorial',
       'fibonacci'
     ])
+    await dynamicThreadPool.destroy()
     const fixedClusterPool = new FixedClusterPool(
       numberOfWorkers,
       './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js'
@@ -1267,10 +1385,42 @@ describe('Abstract pool test suite', () => {
       'factorial',
       'fibonacci'
     ])
-    await dynamicThreadPool.destroy()
     await fixedClusterPool.destroy()
   })
 
+  it('Verify that setDefaultTaskFunction() is working', async () => {
+    const dynamicThreadPool = new DynamicThreadPool(
+      Math.floor(numberOfWorkers / 2),
+      numberOfWorkers,
+      './tests/worker-files/thread/testMultipleTaskFunctionsWorker.js'
+    )
+    await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1)
+    expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
+      DEFAULT_TASK_NAME,
+      'jsonIntegerSerialization',
+      'factorial',
+      'fibonacci'
+    ])
+    await expect(
+      dynamicThreadPool.setDefaultTaskFunction('factorial')
+    ).resolves.toBe(true)
+    expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
+      DEFAULT_TASK_NAME,
+      'factorial',
+      'jsonIntegerSerialization',
+      'fibonacci'
+    ])
+    await expect(
+      dynamicThreadPool.setDefaultTaskFunction('fibonacci')
+    ).resolves.toBe(true)
+    expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
+      DEFAULT_TASK_NAME,
+      'fibonacci',
+      'jsonIntegerSerialization',
+      'factorial'
+    ])
+  })
+
   it('Verify that multiple task functions worker is working', async () => {
     const pool = new DynamicClusterPool(
       Math.floor(numberOfWorkers / 2),