+ expect(pool.opts).toStrictEqual({
+ startWorkers: true,
+ enableEvents: false,
+ restartWorkerOnError: false,
+ enableTasksQueue: true,
+ tasksQueueOptions: {
+ concurrency: 2,
+ size: 4,
+ taskStealing: true,
+ tasksStealingOnBackPressure: true
+ },
+ workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED,
+ workerChoiceStrategyOptions: {
+ retries: 6,
+ runTime: { median: true },
+ waitTime: { median: false },
+ elu: { median: false },
+ weights: { 0: 300, 1: 200 }
+ },
+ onlineHandler: testHandler,
+ messageHandler: testHandler,
+ errorHandler: testHandler,
+ exitHandler: testHandler
+ })
+ expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
+ retries: 6,
+ runTime: { median: true },
+ waitTime: { median: false },
+ elu: { median: false },
+ weights: { 0: 300, 1: 200 }
+ })
+ for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+ .workerChoiceStrategies) {
+ expect(workerChoiceStrategy.opts).toStrictEqual({
+ retries: 6,
+ runTime: { median: true },
+ waitTime: { median: false },
+ elu: { median: false },
+ weights: { 0: 300, 1: 200 }
+ })
+ }
+ await pool.destroy()
+ })
+
+ it('Verify that pool options are validated', async () => {
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategy: 'invalidStrategy'
+ }
+ )
+ ).toThrowError(
+ new Error("Invalid worker choice strategy 'invalidStrategy'")
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategyOptions: {
+ retries: 'invalidChoiceRetries'
+ }
+ }
+ )
+ ).toThrowError(
+ new TypeError(
+ 'Invalid worker choice strategy options: retries must be an integer'
+ )
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategyOptions: {
+ retries: -1
+ }
+ }
+ )
+ ).toThrowError(
+ new RangeError(
+ "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero"
+ )
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategyOptions: { weights: {} }
+ }
+ )
+ ).toThrowError(
+ new Error(
+ 'Invalid worker choice strategy options: must have a weight for each worker node'
+ )
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategyOptions: { measurement: 'invalidMeasurement' }
+ }
+ )
+ ).toThrowError(
+ new Error(
+ "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'"
+ )
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ enableTasksQueue: true,
+ tasksQueueOptions: 'invalidTasksQueueOptions'
+ }
+ )
+ ).toThrowError(
+ new TypeError('Invalid tasks queue options: must be a plain object')
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ enableTasksQueue: true,
+ tasksQueueOptions: { concurrency: 0 }
+ }
+ )
+ ).toThrowError(
+ new RangeError(
+ 'Invalid worker node tasks concurrency: 0 is a negative integer or zero'
+ )
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ enableTasksQueue: true,
+ tasksQueueOptions: { concurrency: -1 }
+ }
+ )
+ ).toThrowError(
+ new RangeError(
+ 'Invalid worker node tasks concurrency: -1 is a negative integer or zero'
+ )
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ enableTasksQueue: true,
+ tasksQueueOptions: { concurrency: 0.2 }
+ }
+ )
+ ).toThrowError(
+ new TypeError('Invalid worker node tasks concurrency: must be an integer')
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ enableTasksQueue: true,
+ tasksQueueOptions: { size: 0 }
+ }
+ )
+ ).toThrowError(
+ new RangeError(
+ 'Invalid worker node tasks queue size: 0 is a negative integer or zero'
+ )
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ enableTasksQueue: true,
+ tasksQueueOptions: { size: -1 }
+ }
+ )
+ ).toThrowError(
+ new RangeError(
+ 'Invalid worker node tasks queue size: -1 is a negative integer or zero'
+ )
+ )
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ enableTasksQueue: true,
+ tasksQueueOptions: { size: 0.2 }
+ }
+ )
+ ).toThrowError(
+ new TypeError('Invalid worker node tasks queue size: must be an integer')
+ )
+ })
+
+ it('Verify that pool 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({
+ retries: 6,
+ runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ })
+ expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
+ retries: 6,
+ runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ })
+ for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+ .workerChoiceStrategies) {
+ expect(workerChoiceStrategy.opts).toStrictEqual({
+ retries: 6,
+ runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ })
+ }
+ expect(
+ pool.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ ).toStrictEqual({
+ runTime: {
+ aggregate: true,
+ average: true,
+ median: false
+ },
+ waitTime: {
+ aggregate: false,
+ average: false,
+ median: false
+ },
+ elu: {
+ aggregate: true,
+ average: true,
+ median: false
+ }
+ })
+ pool.setWorkerChoiceStrategyOptions({
+ runTime: { median: true },
+ elu: { median: true }
+ })
+ expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ retries: 6,
+ runTime: { median: true },
+ waitTime: { median: false },
+ elu: { median: true }
+ })
+ expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
+ retries: 6,
+ runTime: { median: true },
+ waitTime: { median: false },
+ elu: { median: true }
+ })
+ for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+ .workerChoiceStrategies) {
+ expect(workerChoiceStrategy.opts).toStrictEqual({
+ retries: 6,
+ runTime: { median: true },
+ waitTime: { median: false },
+ elu: { median: true }
+ })
+ }
+ expect(
+ pool.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ ).toStrictEqual({
+ runTime: {
+ aggregate: true,
+ average: false,
+ median: true
+ },
+ waitTime: {
+ aggregate: false,
+ average: false,
+ median: false
+ },
+ elu: {
+ aggregate: true,
+ average: false,
+ median: true
+ }
+ })
+ pool.setWorkerChoiceStrategyOptions({
+ runTime: { median: false },
+ elu: { median: false }
+ })
+ expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ retries: 6,
+ runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ })
+ expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
+ retries: 6,
+ runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ })
+ for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+ .workerChoiceStrategies) {
+ expect(workerChoiceStrategy.opts).toStrictEqual({
+ retries: 6,
+ runTime: { median: false },
+ waitTime: { median: false },
+ elu: { median: false }
+ })
+ }
+ expect(
+ pool.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+ ).toStrictEqual({
+ runTime: {
+ aggregate: true,
+ average: true,
+ median: false
+ },
+ waitTime: {
+ aggregate: false,
+ average: false,
+ median: false
+ },
+ elu: {
+ aggregate: true,
+ average: true,
+ median: false
+ }
+ })
+ expect(() =>
+ pool.setWorkerChoiceStrategyOptions('invalidWorkerChoiceStrategyOptions')
+ ).toThrowError(
+ new TypeError(
+ 'Invalid worker choice strategy options: must be a plain object'
+ )
+ )
+ expect(() =>
+ pool.setWorkerChoiceStrategyOptions({
+ retries: 'invalidChoiceRetries'
+ })
+ ).toThrowError(
+ new TypeError(
+ 'Invalid worker choice strategy options: retries must be an integer'
+ )
+ )
+ expect(() =>
+ pool.setWorkerChoiceStrategyOptions({ retries: -1 })
+ ).toThrowError(
+ new RangeError(
+ "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero"
+ )
+ )
+ expect(() =>
+ pool.setWorkerChoiceStrategyOptions({ weights: {} })
+ ).toThrowError(
+ new Error(
+ 'Invalid worker choice strategy options: must have a weight for each worker node'
+ )
+ )
+ expect(() =>
+ pool.setWorkerChoiceStrategyOptions({ measurement: 'invalidMeasurement' })
+ ).toThrowError(
+ new Error(
+ "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'"
+ )
+ )
+ await pool.destroy()
+ })
+
+ it('Verify that pool 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,
+ size: 4,
+ taskStealing: true,
+ tasksStealingOnBackPressure: true
+ })
+ pool.enableTasksQueue(true, { concurrency: 2 })
+ expect(pool.opts.enableTasksQueue).toBe(true)
+ expect(pool.opts.tasksQueueOptions).toStrictEqual({
+ concurrency: 2,
+ size: 4,
+ taskStealing: true,
+ tasksStealingOnBackPressure: true
+ })
+ pool.enableTasksQueue(false)
+ expect(pool.opts.enableTasksQueue).toBe(false)
+ expect(pool.opts.tasksQueueOptions).toBeUndefined()
+ await pool.destroy()
+ })
+
+ it('Verify that pool 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,
+ size: 4,
+ taskStealing: true,
+ tasksStealingOnBackPressure: true
+ })
+ pool.setTasksQueueOptions({ concurrency: 2 })
+ expect(pool.opts.tasksQueueOptions).toStrictEqual({
+ concurrency: 2,
+ size: 4,
+ taskStealing: true,
+ tasksStealingOnBackPressure: true
+ })
+ expect(() =>
+ pool.setTasksQueueOptions('invalidTasksQueueOptions')
+ ).toThrowError(
+ new TypeError('Invalid tasks queue options: must be a plain object')
+ )
+ expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError(
+ new RangeError(
+ 'Invalid worker node tasks concurrency: 0 is a negative integer or zero'
+ )
+ )
+ expect(() => pool.setTasksQueueOptions({ concurrency: -1 })).toThrowError(
+ new RangeError(
+ 'Invalid worker node tasks concurrency: -1 is a negative integer or zero'
+ )
+ )
+ expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError(
+ new TypeError('Invalid worker node tasks concurrency: must be an integer')
+ )
+ expect(() => pool.setTasksQueueOptions({ size: 0 })).toThrowError(
+ new RangeError(
+ 'Invalid worker node tasks queue size: 0 is a negative integer or zero'
+ )
+ )
+ 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()
+ })
+
+ it('Verify that pool info is set', async () => {
+ let pool = new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(pool.info).toStrictEqual({
+ version,
+ type: PoolTypes.fixed,
+ worker: WorkerTypes.thread,
+ started: true,
+ ready: true,
+ strategy: WorkerChoiceStrategies.ROUND_ROBIN,
+ minSize: numberOfWorkers,
+ maxSize: numberOfWorkers,
+ workerNodes: numberOfWorkers,
+ idleWorkerNodes: numberOfWorkers,
+ busyWorkerNodes: 0,
+ executedTasks: 0,
+ executingTasks: 0,
+ failedTasks: 0
+ })
+ await pool.destroy()
+ pool = new DynamicClusterPool(
+ Math.floor(numberOfWorkers / 2),
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ expect(pool.info).toStrictEqual({
+ version,
+ type: PoolTypes.dynamic,
+ worker: WorkerTypes.cluster,
+ started: true,
+ ready: true,
+ strategy: WorkerChoiceStrategies.ROUND_ROBIN,
+ minSize: Math.floor(numberOfWorkers / 2),
+ maxSize: numberOfWorkers,
+ workerNodes: Math.floor(numberOfWorkers / 2),
+ idleWorkerNodes: Math.floor(numberOfWorkers / 2),
+ busyWorkerNodes: 0,
+ executedTasks: 0,
+ executingTasks: 0,
+ failedTasks: 0
+ })
+ await pool.destroy()
+ })
+
+ it('Verify that pool worker tasks usage are initialized', async () => {
+ const pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode).toBeInstanceOf(WorkerNode)
+ expect(workerNode.usage).toStrictEqual({
+ tasks: {
+ executed: 0,
+ executing: 0,
+ queued: 0,
+ maxQueued: 0,
+ stolen: 0,
+ failed: 0
+ },
+ runTime: {
+ history: new CircularArray()
+ },
+ waitTime: {
+ history: new CircularArray()
+ },
+ elu: {
+ idle: {
+ history: new CircularArray()
+ },
+ active: {
+ history: new CircularArray()
+ }
+ }
+ })
+ }
+ await pool.destroy()
+ })
+
+ it('Verify that pool worker tasks queue are initialized', async () => {
+ let pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode).toBeInstanceOf(WorkerNode)
+ expect(workerNode.tasksQueue).toBeInstanceOf(Deque)
+ expect(workerNode.tasksQueue.size).toBe(0)
+ expect(workerNode.tasksQueue.maxSize).toBe(0)
+ }
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ Math.floor(numberOfWorkers / 2),
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode).toBeInstanceOf(WorkerNode)
+ expect(workerNode.tasksQueue).toBeInstanceOf(Deque)
+ expect(workerNode.tasksQueue.size).toBe(0)
+ expect(workerNode.tasksQueue.maxSize).toBe(0)
+ }
+ await pool.destroy()
+ })
+
+ it('Verify that pool worker info are initialized', async () => {
+ let pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode).toBeInstanceOf(WorkerNode)
+ expect(workerNode.info).toStrictEqual({
+ id: expect.any(Number),
+ type: WorkerTypes.cluster,
+ dynamic: false,
+ ready: true
+ })
+ }
+ await pool.destroy()
+ pool = new DynamicThreadPool(
+ Math.floor(numberOfWorkers / 2),
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'