X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=953e50d07d09b43f1d36fb52317434b81e09152e;hb=de2e7182cca6b34b000a09bf6d0ddcff4757db3a;hp=05e4c34ea094023d61db4f0584c022af645e8c5e;hpb=2dca6cabeaaf9c19cca7574deff10ef7af183a92;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 05e4c34e..953e50d0 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -1,3 +1,4 @@ +const { MessageChannel } = require('worker_threads') const { expect } = require('expect') const { DynamicClusterPool, @@ -5,8 +6,8 @@ const { FixedClusterPool, FixedThreadPool, PoolEvents, - WorkerChoiceStrategies, PoolTypes, + WorkerChoiceStrategies, WorkerTypes } = require('../../../lib') const { CircularArray } = require('../../../lib/circular-array') @@ -29,7 +30,7 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/thread/testWorker.js', { - errorHandler: e => console.error(e) + errorHandler: (e) => console.error(e) } ) ).toThrowError('Cannot start a pool from a worker!') @@ -45,6 +46,15 @@ describe('Abstract pool test suite', () => { expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError( expectedError ) + expect(() => new FixedThreadPool(numberOfWorkers, 0)).toThrowError( + expectedError + ) + expect(() => new FixedThreadPool(numberOfWorkers, true)).toThrowError( + expectedError + ) + expect( + () => new FixedThreadPool(numberOfWorkers, './dummyWorker.ts') + ).toThrowError(new Error("Cannot find the worker file './dummyWorker.ts'")) }) it('Verify that numberOfWorkers is checked', () => { @@ -76,6 +86,42 @@ describe('Abstract pool test suite', () => { }) it('Verify that dynamic pool sizing is checked', () => { + expect( + () => + new DynamicClusterPool( + 1, + undefined, + './tests/worker-files/cluster/testWorker.js' + ) + ).toThrowError( + new TypeError( + 'Cannot instantiate a dynamic pool without specifying the maximum pool size' + ) + ) + expect( + () => + new DynamicThreadPool( + 0.5, + 1, + './tests/worker-files/thread/testWorker.js' + ) + ).toThrowError( + new TypeError( + 'Cannot instantiate a pool with a non safe integer number of workers' + ) + ) + expect( + () => + new DynamicClusterPool( + 0, + 0.5, + './tests/worker-files/cluster/testWorker.js' + ) + ).toThrowError( + new TypeError( + 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size' + ) + ) expect( () => new DynamicThreadPool(2, 1, './tests/worker-files/thread/testWorker.js') @@ -86,7 +132,11 @@ describe('Abstract pool test suite', () => { ) expect( () => - new DynamicThreadPool(1, 1, './tests/worker-files/thread/testWorker.js') + new DynamicClusterPool( + 1, + 1, + './tests/worker-files/cluster/testWorker.js' + ) ).toThrowError( new RangeError( 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead' @@ -97,7 +147,7 @@ describe('Abstract pool test suite', () => { new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js') ).toThrowError( new RangeError( - 'Cannot instantiate a dynamic pool with a minimum pool size and a maximum pool size equal to zero' + 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero' ) ) }) @@ -125,7 +175,7 @@ describe('Abstract pool test suite', () => { expect(pool.opts.onlineHandler).toBeUndefined() expect(pool.opts.exitHandler).toBeUndefined() await pool.destroy() - const testHandler = () => console.log('test handler executed') + const testHandler = () => console.info('test handler executed') pool = new FixedThreadPool( numberOfWorkers, './tests/worker-files/thread/testWorker.js', @@ -415,24 +465,6 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/thread/testWorker.js' ) - expect(pool.info).toStrictEqual({ - version, - type: PoolTypes.fixed, - worker: WorkerTypes.thread, - ready: false, - strategy: WorkerChoiceStrategies.ROUND_ROBIN, - minSize: numberOfWorkers, - maxSize: numberOfWorkers, - workerNodes: numberOfWorkers, - idleWorkerNodes: numberOfWorkers, - busyWorkerNodes: 0, - executedTasks: 0, - executingTasks: 0, - queuedTasks: 0, - maxQueuedTasks: 0, - failedTasks: 0 - }) - await waitPoolEvents(pool, PoolEvents.ready, 1) expect(pool.info).toStrictEqual({ version, type: PoolTypes.fixed, @@ -446,8 +478,6 @@ describe('Abstract pool test suite', () => { busyWorkerNodes: 0, executedTasks: 0, executingTasks: 0, - queuedTasks: 0, - maxQueuedTasks: 0, failedTasks: 0 }) await pool.destroy() @@ -456,24 +486,6 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) - expect(pool.info).toStrictEqual({ - version, - type: PoolTypes.dynamic, - worker: WorkerTypes.cluster, - ready: false, - 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, - queuedTasks: 0, - maxQueuedTasks: 0, - failedTasks: 0 - }) - await waitPoolEvents(pool, PoolEvents.ready, 1) expect(pool.info).toStrictEqual({ version, type: PoolTypes.dynamic, @@ -487,8 +499,6 @@ describe('Abstract pool test suite', () => { busyWorkerNodes: 0, executedTasks: 0, executingTasks: 0, - queuedTasks: 0, - maxQueuedTasks: 0, failedTasks: 0 }) await pool.destroy() @@ -557,15 +567,6 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) - for (const workerNode of pool.workerNodes) { - expect(workerNode.info).toStrictEqual({ - id: expect.any(Number), - type: WorkerTypes.cluster, - dynamic: false, - ready: false - }) - } - await waitPoolEvents(pool, PoolEvents.ready, 1) for (const workerNode of pool.workerNodes) { expect(workerNode.info).toStrictEqual({ id: expect.any(Number), @@ -585,16 +586,8 @@ describe('Abstract pool test suite', () => { id: expect.any(Number), type: WorkerTypes.thread, dynamic: false, - ready: false - }) - } - await waitPoolEvents(pool, PoolEvents.ready, 1) - for (const workerNode of pool.workerNodes) { - expect(workerNode.info).toStrictEqual({ - id: expect.any(Number), - type: WorkerTypes.thread, - dynamic: false, - ready: true + ready: true, + messageChannel: expect.any(MessageChannel) }) } }) @@ -701,6 +694,10 @@ describe('Abstract pool test suite', () => { }) expect(workerNode.usage.tasks.executed).toBeGreaterThan(0) expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(maxMultiplier) + expect(workerNode.usage.runTime.history.length).toBe(0) + expect(workerNode.usage.waitTime.history.length).toBe(0) + expect(workerNode.usage.elu.idle.history.length).toBe(0) + expect(workerNode.usage.elu.active.history.length).toBe(0) } pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) for (const workerNode of pool.workerNodes) { @@ -729,6 +726,8 @@ describe('Abstract pool test suite', () => { }) expect(workerNode.usage.runTime.history.length).toBe(0) expect(workerNode.usage.waitTime.history.length).toBe(0) + expect(workerNode.usage.elu.idle.history.length).toBe(0) + expect(workerNode.usage.elu.active.history.length).toBe(0) } await pool.destroy() }) @@ -742,7 +741,7 @@ describe('Abstract pool test suite', () => { const promises = new Set() let poolFull = 0 let poolInfo - pool.emitter.on(PoolEvents.full, info => { + pool.emitter.on(PoolEvents.full, (info) => { ++poolFull poolInfo = info }) @@ -766,8 +765,6 @@ describe('Abstract pool test suite', () => { 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() @@ -779,9 +776,9 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) - let poolReady = 0 let poolInfo - pool.emitter.on(PoolEvents.ready, info => { + let poolReady = 0 + pool.emitter.on(PoolEvents.ready, (info) => { ++poolReady poolInfo = info }) @@ -800,8 +797,6 @@ describe('Abstract pool test suite', () => { 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() @@ -815,7 +810,7 @@ describe('Abstract pool test suite', () => { const promises = new Set() let poolBusy = 0 let poolInfo - pool.emitter.on(PoolEvents.busy, info => { + pool.emitter.on(PoolEvents.busy, (info) => { ++poolBusy poolInfo = info }) @@ -839,8 +834,6 @@ describe('Abstract pool test suite', () => { 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()