await pool.destroy()
})
+ 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),
'factorial',
'fibonacci'
])
+ await dynamicThreadPool.destroy()
const fixedClusterPool = new FixedClusterPool(
numberOfWorkers,
'./tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js'
'factorial',
'fibonacci'
])
- await dynamicThreadPool.destroy()
await fixedClusterPool.destroy()
})
- it('Verify that hasTaskFunction() is working', async () => {
+ 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.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)
- const fixedClusterPool = new FixedClusterPool(
- numberOfWorkers,
- './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js'
- )
- await waitPoolEvents(fixedClusterPool, 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()
- await fixedClusterPool.destroy()
+ 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 () => {