describe('Abstract pool test suite', () => {
const numberOfWorkers = 1
- const workerNotFoundInPoolError = new Error(
- 'Worker could not be found in the pool worker nodes'
- )
class StubPoolWithRemoveAllWorker extends FixedThreadPool {
removeAllWorker () {
this.workerNodes = []
new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
).toThrowError(
new TypeError(
- 'Cannot instantiate a pool with a non integer number of workers'
+ 'Cannot instantiate a pool with a non safe integer number of workers'
)
)
})
numberOfWorkers,
'./tests/worker-files/thread/testWorker.js',
{
- workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED,
- workerChoiceStrategyOptions: { medRunTime: true },
+ workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED,
+ workerChoiceStrategyOptions: {
+ medRunTime: true,
+ weights: { 0: 300 }
+ },
enableEvents: false,
enableTasksQueue: true,
tasksQueueOptions: { concurrency: 2 },
expect(pool.opts.enableTasksQueue).toBe(true)
expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 })
expect(pool.opts.workerChoiceStrategy).toBe(
- WorkerChoiceStrategies.LESS_USED
+ WorkerChoiceStrategies.LEAST_USED
)
expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
- medRunTime: true
+ medRunTime: true,
+ weights: { 0: 300 }
})
expect(pool.opts.messageHandler).toStrictEqual(testHandler)
expect(pool.opts.errorHandler).toStrictEqual(testHandler)
}
)
).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategyOptions: { weights: {} }
+ }
+ )
+ ).toThrowError(
+ 'Invalid worker choice strategy options: must have a weight for each worker node'
+ )
})
it('Verify that worker choice strategy options can be set', async () => {
await pool.destroy()
})
- it('Simulate worker not found at getWorkerTasksUsage()', async () => {
+ it('Simulate worker not found', async () => {
const pool = new StubPoolWithRemoveAllWorker(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
// Simulate worker not found.
pool.removeAllWorker()
expect(pool.workerNodes.length).toBe(0)
- expect(() => pool.getWorkerTasksUsage()).toThrowError(
- workerNotFoundInPoolError
- )
await pool.destroy()
})