-const { MessageChannel } = require('worker_threads')
const { expect } = require('expect')
const {
DynamicClusterPool,
WorkerChoiceStrategies.ROUND_ROBIN
)
expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ choiceRetries: 6,
+ runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ })
+ expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
+ choiceRetries: 6,
runTime: { median: false },
waitTime: { median: false },
elu: { median: false }
WorkerChoiceStrategies.LEAST_USED
)
expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ choiceRetries: 6,
runTime: { median: true },
+ waitTime: { median: false },
+ elu: { median: false },
+ weights: { 0: 300, 1: 200 }
+ })
+ expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
+ choiceRetries: 6,
+ runTime: { median: true },
+ waitTime: { median: false },
+ elu: { median: false },
weights: { 0: 300, 1: 200 }
})
expect(pool.opts.messageHandler).toStrictEqual(testHandler)
}
)
).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
- expect(
- () =>
- new FixedThreadPool(
- numberOfWorkers,
- './tests/worker-files/thread/testWorker.js',
- {
- workerChoiceStrategyOptions: 'invalidOptions'
- }
- )
- ).toThrowError(
- 'Invalid worker choice strategy options: must be a plain object'
- )
expect(
() =>
new FixedThreadPool(
{ workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
)
expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ choiceRetries: 6,
+ runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ })
+ expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
+ choiceRetries: 6,
runTime: { median: false },
waitTime: { median: false },
elu: { median: false }
for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
.workerChoiceStrategies) {
expect(workerChoiceStrategy.opts).toStrictEqual({
+ choiceRetries: 6,
runTime: { median: false },
waitTime: { median: false },
elu: { median: false }
elu: { median: true }
})
expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ choiceRetries: 6,
runTime: { median: true },
+ waitTime: { median: false },
+ elu: { median: true }
+ })
+ expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
+ choiceRetries: 6,
+ runTime: { median: true },
+ waitTime: { median: false },
elu: { median: true }
})
for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
.workerChoiceStrategies) {
expect(workerChoiceStrategy.opts).toStrictEqual({
+ choiceRetries: 6,
runTime: { median: true },
+ waitTime: { median: false },
elu: { median: true }
})
}
elu: { median: false }
})
expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ choiceRetries: 6,
runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ })
+ expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
+ choiceRetries: 6,
+ runTime: { median: false },
+ waitTime: { median: false },
elu: { median: false }
})
for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
.workerChoiceStrategies) {
expect(workerChoiceStrategy.opts).toStrictEqual({
+ choiceRetries: 6,
runTime: { median: false },
+ waitTime: { median: false },
elu: { median: false }
})
}
id: expect.any(Number),
type: WorkerTypes.thread,
dynamic: false,
- ready: true,
- messageChannel: expect.any(MessageChannel)
+ ready: true
})
}
})
await pool.destroy()
})
- it('Verify that multiple tasks worker is working', async () => {
+ it('Verify that listTaskFunctions() 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.listTaskFunctions()).toStrictEqual([
+ 'default',
+ 'jsonIntegerSerialization',
+ 'factorial',
+ 'fibonacci'
+ ])
+ const fixedClusterPool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js'
+ )
+ await waitPoolEvents(fixedClusterPool, PoolEvents.ready, 1)
+ expect(fixedClusterPool.listTaskFunctions()).toStrictEqual([
+ 'default',
+ 'jsonIntegerSerialization',
+ 'factorial',
+ 'fibonacci'
+ ])
+ })
+
+ it('Verify that multiple task functions worker is working', async () => {
const pool = new DynamicClusterPool(
Math.floor(numberOfWorkers / 2),
numberOfWorkers,
- './tests/worker-files/cluster/testMultiTasksWorker.js'
+ './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js'
)
const data = { n: 10 }
const result0 = await pool.execute(data)
expect(result2).toBe(3628800)
const result3 = await pool.execute(data, 'fibonacci')
expect(result3).toBe(55)
+ expect(pool.info.executingTasks).toBe(0)
+ expect(pool.info.executedTasks).toBe(4)
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.info.taskFunctions).toStrictEqual([
+ 'default',
+ 'jsonIntegerSerialization',
+ 'factorial',
+ 'fibonacci'
+ ])
+ expect(workerNode.taskFunctionsUsage.size).toBe(3)
+ for (const name of pool.listTaskFunctions()) {
+ expect(workerNode.getTaskFunctionWorkerUsage(name)).toStrictEqual({
+ tasks: {
+ executed: expect.any(Number),
+ executing: expect.any(Number),
+ failed: 0,
+ queued: 0
+ },
+ runTime: {
+ history: expect.any(CircularArray)
+ },
+ waitTime: {
+ history: expect.any(CircularArray)
+ },
+ elu: {
+ idle: {
+ history: expect.any(CircularArray)
+ },
+ active: {
+ history: expect.any(CircularArray)
+ }
+ }
+ })
+ expect(
+ workerNode.getTaskFunctionWorkerUsage(name).tasks.executing
+ ).toBeGreaterThanOrEqual(0)
+ }
+ }
})
})