X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=953e50d07d09b43f1d36fb52317434b81e09152e;hb=29d8b961b1907797d251871a97092973d342c63c;hp=6c340ed9f089ea6f9fd5deeb126480dc0e0ab3df;hpb=21f710aa73abbb5d90328cfb199adfc0f7a70406;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 6c340ed9..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', () => { @@ -75,7 +85,43 @@ describe('Abstract pool test suite', () => { ) }) - it('Verify dynamic pool sizing', () => { + 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', @@ -419,7 +469,7 @@ describe('Abstract pool test suite', () => { version, type: PoolTypes.fixed, worker: WorkerTypes.thread, - ready: false, + ready: true, strategy: WorkerChoiceStrategies.ROUND_ROBIN, minSize: numberOfWorkers, maxSize: numberOfWorkers, @@ -428,8 +478,6 @@ describe('Abstract pool test suite', () => { busyWorkerNodes: 0, executedTasks: 0, executingTasks: 0, - queuedTasks: 0, - maxQueuedTasks: 0, failedTasks: 0 }) await pool.destroy() @@ -442,7 +490,7 @@ describe('Abstract pool test suite', () => { version, type: PoolTypes.dynamic, worker: WorkerTypes.cluster, - ready: false, + ready: true, strategy: WorkerChoiceStrategies.ROUND_ROBIN, minSize: Math.floor(numberOfWorkers / 2), maxSize: numberOfWorkers, @@ -451,8 +499,6 @@ describe('Abstract pool test suite', () => { busyWorkerNodes: 0, executedTasks: 0, executingTasks: 0, - queuedTasks: 0, - maxQueuedTasks: 0, failedTasks: 0 }) await pool.destroy() @@ -526,7 +572,7 @@ describe('Abstract pool test suite', () => { id: expect.any(Number), type: WorkerTypes.cluster, dynamic: false, - ready: false + ready: true }) } await pool.destroy() @@ -540,7 +586,8 @@ describe('Abstract pool test suite', () => { id: expect.any(Number), type: WorkerTypes.thread, dynamic: false, - ready: false + ready: true, + messageChannel: expect.any(MessageChannel) }) } }) @@ -647,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) { @@ -675,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() }) @@ -688,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 }) @@ -712,21 +765,20 @@ 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() }) it("Verify that pool event emitter 'ready' event can register a callback", async () => { - const pool = new FixedClusterPool( + const pool = new DynamicClusterPool( + Math.floor(numberOfWorkers / 2), 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 }) @@ -734,7 +786,7 @@ describe('Abstract pool test suite', () => { expect(poolReady).toBe(1) expect(poolInfo).toStrictEqual({ version, - type: PoolTypes.fixed, + type: PoolTypes.dynamic, worker: WorkerTypes.cluster, ready: true, strategy: WorkerChoiceStrategies.ROUND_ROBIN, @@ -745,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() @@ -760,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 }) @@ -784,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()