}
}
+ afterEach(() => {
+ sinon.restore()
+ })
+
it('Simulate pool creation from a non main thread/process', () => {
expect(
() =>
expect(pool.opts.enableTasksQueue).toBe(true)
expect(pool.opts.tasksQueueOptions).toStrictEqual({
concurrency: 2,
- queueMaxSize: 4
+ size: 4
})
expect(pool.opts.workerChoiceStrategy).toBe(
WorkerChoiceStrategies.LEAST_USED
).toThrowError(
new Error("Invalid worker choice strategy 'invalidStrategy'")
)
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategyOptions: {
+ choiceRetries: 'invalidChoiceRetries'
+ }
+ }
+ )
+ ).toThrowError(
+ new TypeError(
+ 'Invalid worker choice strategy options: choice retries must be an integer'
+ )
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategyOptions: {
+ choiceRetries: -1
+ }
+ }
+ )
+ ).toThrowError(
+ new RangeError(
+ "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero"
+ )
+ )
expect(
() =>
new FixedThreadPool(
'Invalid worker choice strategy options: must be a plain object'
)
)
+ expect(() =>
+ pool.setWorkerChoiceStrategyOptions({
+ choiceRetries: 'invalidChoiceRetries'
+ })
+ ).toThrowError(
+ new TypeError(
+ 'Invalid worker choice strategy options: choice retries must be an integer'
+ )
+ )
+ expect(() =>
+ pool.setWorkerChoiceStrategyOptions({ choiceRetries: -1 })
+ ).toThrowError(
+ new RangeError(
+ "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero"
+ )
+ )
expect(() =>
pool.setWorkerChoiceStrategyOptions({ weights: {} })
).toThrowError(
expect(pool.opts.enableTasksQueue).toBe(true)
expect(pool.opts.tasksQueueOptions).toStrictEqual({
concurrency: 1,
- queueMaxSize: 4
+ size: 4
})
pool.enableTasksQueue(true, { concurrency: 2 })
expect(pool.opts.enableTasksQueue).toBe(true)
expect(pool.opts.tasksQueueOptions).toStrictEqual({
concurrency: 2,
- queueMaxSize: 4
+ size: 4
})
pool.enableTasksQueue(false)
expect(pool.opts.enableTasksQueue).toBe(false)
)
expect(pool.opts.tasksQueueOptions).toStrictEqual({
concurrency: 1,
- queueMaxSize: 4
+ size: 4
})
pool.setTasksQueueOptions({ concurrency: 2 })
expect(pool.opts.tasksQueueOptions).toStrictEqual({
concurrency: 2,
- queueMaxSize: 4
+ size: 4
})
expect(() =>
pool.setTasksQueueOptions('invalidTasksQueueOptions')
expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError(
new TypeError('Invalid worker node tasks concurrency: must be an integer')
)
- expect(() => pool.setTasksQueueOptions({ queueMaxSize: 0 })).toThrowError(
- new RangeError(
- 'Invalid worker node tasks queue max size: 0 is a negative integer or zero'
+ expect(() => pool.setTasksQueueOptions({ queueMaxSize: 2 })).toThrowError(
+ new Error(
+ 'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead'
)
)
- expect(() => pool.setTasksQueueOptions({ queueMaxSize: -1 })).toThrowError(
+ expect(() => pool.setTasksQueueOptions({ size: 0 })).toThrowError(
new RangeError(
- 'Invalid worker node tasks queue max size: -1 is a negative integer or zero'
+ 'Invalid worker node tasks queue size: 0 is a negative integer or zero'
)
)
- expect(() => pool.setTasksQueueOptions({ queueMaxSize: 0.2 })).toThrowError(
- new TypeError(
- 'Invalid worker node tasks queue max size: must be an integer'
+ expect(() => pool.setTasksQueueOptions({ size: -1 })).toThrowError(
+ new RangeError(
+ 'Invalid worker node tasks queue size: -1 is a negative integer or zero'
)
)
+ expect(() => pool.setTasksQueueOptions({ size: 0.2 })).toThrowError(
+ new TypeError('Invalid worker node tasks queue size: must be an integer')
+ )
await pool.destroy()
})
executing: 0,
queued: 0,
maxQueued: 0,
+ stolen: 0,
failed: 0
},
runTime: {
}
})
+ it('Verify that pool execute() arguments are checked', async () => {
+ const pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ await expect(pool.execute(undefined, 0)).rejects.toThrowError(
+ new TypeError('name argument must be a string')
+ )
+ await expect(pool.execute(undefined, '')).rejects.toThrowError(
+ new TypeError('name argument must not be an empty string')
+ )
+ await expect(pool.execute(undefined, undefined, {})).rejects.toThrowError(
+ new TypeError('transferList argument must be an array')
+ )
+ await expect(pool.execute(undefined, 'unknown')).rejects.toBe(
+ "Task function 'unknown' not found"
+ )
+ await pool.destroy()
+ await expect(pool.execute(undefined, undefined, {})).rejects.toThrowError(
+ new Error('Cannot execute a task on destroyed pool')
+ )
+ })
+
it('Verify that pool worker tasks usage are computed', async () => {
const pool = new FixedClusterPool(
numberOfWorkers,
executing: maxMultiplier,
queued: 0,
maxQueued: 0,
+ stolen: 0,
failed: 0
},
runTime: {
executing: 0,
queued: 0,
maxQueued: 0,
+ stolen: 0,
failed: 0
},
runTime: {
executing: 0,
queued: 0,
maxQueued: 0,
+ stolen: 0,
failed: 0
},
runTime: {
executing: 0,
queued: 0,
maxQueued: 0,
+ stolen: 0,
failed: 0
},
runTime: {
++poolBackPressure
poolInfo = info
})
- for (let i = 0; i < numberOfWorkers * 2; i++) {
+ for (let i = 0; i < numberOfWorkers + 1; i++) {
promises.add(pool.execute())
}
await Promise.all(promises)
- expect(poolBackPressure).toBe(2)
+ expect(poolBackPressure).toBe(1)
expect(poolInfo).toStrictEqual({
version,
type: PoolTypes.fixed,
maxQueuedTasks: expect.any(Number),
queuedTasks: expect.any(Number),
backPressure: true,
+ stolenTasks: expect.any(Number),
failedTasks: expect.any(Number)
})
expect(pool.hasBackPressure.called).toBe(true)
executed: expect.any(Number),
executing: expect.any(Number),
failed: 0,
- queued: 0
+ queued: 0,
+ stolen: 0
},
runTime: {
history: expect.any(CircularArray)