- [`pool.execute(data, name, transferList)`](#poolexecutedata-name-transferlist)
- [`pool.start()`](#poolstart)
- [`pool.destroy()`](#pooldestroy)
+ - [`pool.hasTaskFunction(name)`](#poolhastaskfunctionname)
+ - [`pool.addTaskFunction(name, fn)`](#pooladdtaskfunctionname-fn)
+ - [`pool.removeTaskFunction(name)`](#poolremovetaskfunctionname)
- [`pool.listTaskFunctionNames()`](#poollisttaskfunctionnames)
+ - [`pool.setDefaultTaskFunction(name)`](#poolsetdefaulttaskfunctionname)
- [`PoolOptions`](#pooloptions)
- [`ThreadPoolOptions extends PoolOptions`](#threadpooloptions-extends-pooloptions)
- [`ClusterPoolOptions extends PoolOptions`](#clusterpooloptions-extends-pooloptions)
This method is available on both pool implementations and returns a boolean.
+### `pool.addTaskFunction(name, fn)`
+
+`name` (mandatory) The task function name.
+`fn` (mandatory) The task function.
+
+This method is available on both pool implementations and returns a boolean promise.
+
+### `pool.removeTaskFunction(name)`
+
+`name` (mandatory) The task function name.
+
+This method is available on both pool implementations and returns a boolean promise.
+
### `pool.listTaskFunctionNames()`
This method is available on both pool implementations and returns an array of the task function names.
+### `pool.setDefaultTaskFunction(name)`
+
+`name` (mandatory) The task function name.
+
+This method is available on both pool implementations and returns a boolean promise.
+
### `PoolOptions`
An object with these properties:
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)
+ 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 that does not exist 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 () => {
expect(killHandlerStub.calledOnce).toBe(true)
})
- it('Verify that handleError() method works properly', () => {
+ it('Verify that handleError() method is working properly', () => {
const error = new Error('Error as an error')
const worker = new ClusterWorker(() => {})
expect(worker.handleError(error)).not.toBeInstanceOf(Error)
).toThrowError('Main worker not set')
})
- it('Verify that hasTaskFunction() works', () => {
+ it('Verify that hasTaskFunction() is working', () => {
const fn1 = () => {
return 1
}
expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
})
- it('Verify that addTaskFunction() works', () => {
+ it('Verify that addTaskFunction() is working', () => {
const fn1 = () => {
return 1
}
)
})
- it('Verify that removeTaskFunction() works', () => {
+ it('Verify that removeTaskFunction() is working', () => {
const fn1 = () => {
return 1
}
expect(worker.getMainWorker().send.calledOnce).toBe(true)
})
- it('Verify that listTaskFunctionNames() works', () => {
+ it('Verify that listTaskFunctionNames() is working', () => {
const fn1 = () => {
return 1
}
])
})
- it('Verify that setDefaultTaskFunction() works', () => {
+ it('Verify that setDefaultTaskFunction() is working', () => {
const fn1 = () => {
return 1
}