+ it('Verify that pool options are validated', 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'")
+ 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 () => {
+ const pool = new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
+ )
+ expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ medRunTime: false,
+ medWaitTime: false
+ })
+ for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+ .workerChoiceStrategies) {
+ expect(workerChoiceStrategy.opts).toStrictEqual({
+ medRunTime: false,
+ medWaitTime: false
+ })
+ }
+ expect(
+ pool.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ ).toStrictEqual({
+ runTime: true,
+ avgRunTime: true,
+ medRunTime: false,
+ waitTime: false,
+ avgWaitTime: false,
+ medWaitTime: false,
+ elu: false
+ })
+ pool.setWorkerChoiceStrategyOptions({ medRunTime: true })
+ expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ medRunTime: true
+ })
+ for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+ .workerChoiceStrategies) {
+ expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: true })
+ }
+ expect(
+ pool.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ ).toStrictEqual({
+ runTime: true,
+ avgRunTime: false,
+ medRunTime: true,
+ waitTime: false,
+ avgWaitTime: false,
+ medWaitTime: false,
+ elu: false
+ })
+ pool.setWorkerChoiceStrategyOptions({ medRunTime: false })
+ expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ medRunTime: false
+ })
+ for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+ .workerChoiceStrategies) {
+ expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false })
+ }
+ expect(
+ pool.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ ).toStrictEqual({
+ runTime: true,
+ avgRunTime: true,
+ medRunTime: false,
+ waitTime: false,
+ avgWaitTime: false,
+ medWaitTime: false,
+ elu: false
+ })
+ await pool.destroy()
+ })
+
+ it('Verify that tasks queue can be enabled/disabled', async () => {
+ const pool = new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(pool.opts.enableTasksQueue).toBe(false)
+ expect(pool.opts.tasksQueueOptions).toBeUndefined()
+ pool.enableTasksQueue(true)
+ expect(pool.opts.enableTasksQueue).toBe(true)
+ expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 })
+ pool.enableTasksQueue(true, { concurrency: 2 })
+ expect(pool.opts.enableTasksQueue).toBe(true)
+ expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 })
+ pool.enableTasksQueue(false)
+ expect(pool.opts.enableTasksQueue).toBe(false)
+ expect(pool.opts.tasksQueueOptions).toBeUndefined()
+ await pool.destroy()
+ })
+
+ it('Verify that tasks queue options can be set', async () => {
+ const pool = new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ { enableTasksQueue: true }
+ )
+ expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 })
+ pool.setTasksQueueOptions({ concurrency: 2 })
+ expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 })
+ expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError(
+ "Invalid worker tasks concurrency '0'"
+ )
+ await pool.destroy()
+ })
+
+ it('Verify that pool info is set', async () => {
+ let pool = new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(pool.info).toStrictEqual({
+ type: PoolTypes.fixed,
+ worker: WorkerTypes.thread,
+ minSize: numberOfWorkers,
+ maxSize: numberOfWorkers,
+ workerNodes: numberOfWorkers,
+ idleWorkerNodes: numberOfWorkers,
+ busyWorkerNodes: 0,
+ executedTasks: 0,
+ executingTasks: 0,
+ queuedTasks: 0,
+ maxQueuedTasks: 0,
+ failedTasks: 0
+ })
+ await pool.destroy()
+ pool = new DynamicClusterPool(
+ numberOfWorkers,
+ numberOfWorkers * 2,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(pool.info).toStrictEqual({
+ type: PoolTypes.dynamic,
+ worker: WorkerTypes.cluster,
+ minSize: numberOfWorkers,
+ maxSize: numberOfWorkers * 2,
+ workerNodes: numberOfWorkers,
+ idleWorkerNodes: numberOfWorkers,
+ busyWorkerNodes: 0,
+ executedTasks: 0,
+ executingTasks: 0,
+ queuedTasks: 0,
+ maxQueuedTasks: 0,
+ failedTasks: 0
+ })
+ await pool.destroy()
+ })
+
+ it('Simulate worker not found', async () => {