)
class StubPoolWithRemoveAllWorker extends FixedThreadPool {
removeAllWorker () {
- this.workers = []
+ this.workerNodes = []
this.promiseResponseMap.clear()
}
}
errorHandler: e => console.error(e)
}
)
- ).toThrowError(new Error('Cannot start a pool from a worker!'))
+ ).toThrowError('Cannot start a pool from a worker!')
})
it('Verify that filePath is checked', () => {
it('Verify that numberOfWorkers is checked', () => {
expect(() => new FixedThreadPool()).toThrowError(
- new Error(
- 'Cannot instantiate a pool without specifying the number of workers'
- )
+ 'Cannot instantiate a pool without specifying the number of workers'
)
})
expect(pool.opts.enableEvents).toBe(true)
expect(pool.emitter).toBeDefined()
expect(pool.opts.enableTasksQueue).toBe(false)
+ expect(pool.opts.tasksQueueOptions).toBeUndefined()
expect(pool.opts.workerChoiceStrategy).toBe(
WorkerChoiceStrategies.ROUND_ROBIN
)
workerChoiceStrategyOptions: { medRunTime: true },
enableEvents: false,
enableTasksQueue: true,
+ tasksQueueOptions: { concurrency: 2 },
messageHandler: testHandler,
errorHandler: testHandler,
onlineHandler: testHandler,
expect(pool.opts.enableEvents).toBe(false)
expect(pool.emitter).toBeUndefined()
expect(pool.opts.enableTasksQueue).toBe(true)
+ expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 })
expect(pool.opts.workerChoiceStrategy).toBe(
WorkerChoiceStrategies.LESS_USED
)
await pool.destroy()
})
- it('Simulate worker not found during getWorkerTasksUsage', async () => {
+ it('Verify that pool options are valid', async () => {
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ enableTasksQueue: true,
+ tasksQueueOptions: { concurrency: 0 }
+ }
+ )
+ ).toThrowError("Invalid worker tasks concurrency '0'")
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategy: 'invalidStrategy'
+ }
+ )
+ ).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
+ })
+
+ it('Simulate worker not found at getWorkerTasksUsage()', async () => {
const pool = new StubPoolWithRemoveAllWorker(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
errorHandler: e => console.error(e)
}
)
+ expect(pool.workerNodes.length).toBe(numberOfWorkers)
// Simulate worker not found.
pool.removeAllWorker()
+ expect(pool.workerNodes.length).toBe(0)
expect(() => pool.getWorkerTasksUsage()).toThrowError(
workerNotFoundInPoolError
)