+ 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 () => {
+ 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)
+ await pool.destroy()
+ })
+
+ it('Verify that worker pool tasks usage are initialized', async () => {
+ const pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.workerUsage).toStrictEqual({
+ tasks: {
+ executed: 0,
+ executing: 0,
+ queued: 0,
+ failed: 0
+ },
+ runTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ waitTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ elu: undefined
+ })
+ }
+ await pool.destroy()
+ })
+
+ it('Verify that worker pool tasks queue are initialized', async () => {
+ const pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.tasksQueue).toBeDefined()
+ expect(workerNode.tasksQueue).toBeInstanceOf(Queue)
+ expect(workerNode.tasksQueue.size).toBe(0)
+ }
+ await pool.destroy()
+ })
+
+ it('Verify that worker pool tasks usage are computed', async () => {
+ const pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ const promises = new Set()
+ const maxMultiplier = 2
+ for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) {
+ promises.add(pool.execute())
+ }
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.workerUsage).toStrictEqual({
+ tasks: {
+ executed: 0,
+ executing: maxMultiplier,
+ queued: 0,
+ failed: 0
+ },
+ runTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ waitTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ elu: undefined
+ })
+ }
+ await Promise.all(promises)
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.workerUsage).toStrictEqual({
+ tasks: {
+ executed: maxMultiplier,
+ executing: 0,
+ queued: 0,
+ failed: 0
+ },
+ runTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ waitTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ elu: undefined
+ })
+ }
+ await pool.destroy()
+ })
+
+ it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => {
+ const pool = new DynamicThreadPool(
+ numberOfWorkers,
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ const promises = new Set()
+ const maxMultiplier = 2
+ for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) {
+ promises.add(pool.execute())
+ }
+ await Promise.all(promises)
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.workerUsage).toStrictEqual({
+ tasks: {
+ executed: expect.any(Number),
+ executing: 0,
+ queued: 0,
+ failed: 0
+ },
+ runTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ waitTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ elu: undefined
+ })
+ expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0)
+ expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
+ maxMultiplier
+ )
+ }
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.workerUsage).toStrictEqual({
+ tasks: {
+ executed: 0,
+ executing: 0,
+ queued: 0,
+ failed: 0
+ },
+ runTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ waitTime: {
+ aggregation: 0,
+ average: 0,
+ median: 0,
+ history: expect.any(CircularArray)
+ },
+ elu: undefined
+ })
+ expect(workerNode.workerUsage.runTime.history.length).toBe(0)
+ expect(workerNode.workerUsage.waitTime.history.length).toBe(0)
+ }
+ await pool.destroy()
+ })
+
+ it("Verify that pool event emitter 'full' event can register a callback", async () => {
+ const pool = new DynamicThreadPool(
+ numberOfWorkers,
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ const promises = new Set()
+ let poolFull = 0
+ let poolInfo
+ pool.emitter.on(PoolEvents.full, info => {
+ ++poolFull
+ poolInfo = info
+ })
+ for (let i = 0; i < numberOfWorkers * 2; i++) {
+ promises.add(pool.execute())
+ }
+ await Promise.all(promises)
+ // The `full` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
+ // So in total numberOfWorkers * 2 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = max = numberOfWorkers.
+ expect(poolFull).toBe(numberOfWorkers * 2)
+ expect(poolInfo).toStrictEqual({
+ type: PoolTypes.dynamic,
+ worker: WorkerTypes.thread,
+ minSize: expect.any(Number),
+ maxSize: expect.any(Number),
+ workerNodes: expect.any(Number),
+ idleWorkerNodes: expect.any(Number),
+ busyWorkerNodes: expect.any(Number),
+ executedTasks: expect.any(Number),
+ executingTasks: expect.any(Number),
+ queuedTasks: expect.any(Number),
+ maxQueuedTasks: expect.any(Number),
+ failedTasks: expect.any(Number)
+ })
+ await pool.destroy()
+ })
+
+ it("Verify that pool event emitter 'busy' event can register a callback", async () => {
+ const pool = new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ const promises = new Set()