X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=0929adc36a60dcfa0422ad6908041484736ee28c;hb=b414b84cc22efd9df5d9b7c5a470125955cd0f1c;hp=5496746dbc5e6f1edd7a0ee881807ff8ee57d6fa;hpb=c62a9bbc12416e26acbb3b86b5090d6186b14b60;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 5496746d..0929adc3 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -1,4 +1,3 @@ -const { MessageChannel } = require('worker_threads') const { expect } = require('expect') const { DynamicClusterPool, @@ -30,10 +29,12 @@ 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!') + ).toThrowError( + 'Cannot start a pool from a worker with the same type as the pool' + ) }) it('Verify that filePath is checked', () => { @@ -147,7 +148,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 pool size equal to zero' + 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero' ) ) }) @@ -166,6 +167,13 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + choiceRetries: 6, + runTime: { median: false }, + waitTime: { median: false }, + elu: { median: false } + }) + expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ + choiceRetries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -175,7 +183,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', @@ -204,7 +212,17 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.LEAST_USED ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + choiceRetries: 6, + runTime: { median: true }, + waitTime: { median: false }, + elu: { median: false }, + weights: { 0: 300, 1: 200 } + }) + expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ + choiceRetries: 6, runTime: { median: true }, + waitTime: { median: false }, + elu: { median: false }, weights: { 0: 300, 1: 200 } }) expect(pool.opts.messageHandler).toStrictEqual(testHandler) @@ -225,18 +243,6 @@ describe('Abstract pool test suite', () => { } ) ).toThrowError("Invalid worker choice strategy 'invalidStrategy'") - expect( - () => - new FixedThreadPool( - numberOfWorkers, - './tests/worker-files/thread/testWorker.js', - { - workerChoiceStrategyOptions: 'invalidOptions' - } - ) - ).toThrowError( - 'Invalid worker choice strategy options: must be a plain object' - ) expect( () => new FixedThreadPool( @@ -303,6 +309,13 @@ describe('Abstract pool test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + choiceRetries: 6, + runTime: { median: false }, + waitTime: { median: false }, + elu: { median: false } + }) + expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ + choiceRetries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -310,6 +323,7 @@ describe('Abstract pool test suite', () => { for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ + choiceRetries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -339,13 +353,23 @@ describe('Abstract pool test suite', () => { elu: { median: true } }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + choiceRetries: 6, runTime: { median: true }, + waitTime: { median: false }, + elu: { median: true } + }) + expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ + choiceRetries: 6, + runTime: { median: true }, + waitTime: { median: false }, elu: { median: true } }) for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ + choiceRetries: 6, runTime: { median: true }, + waitTime: { median: false }, elu: { median: true } }) } @@ -373,13 +397,23 @@ describe('Abstract pool test suite', () => { elu: { median: false } }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + choiceRetries: 6, runTime: { median: false }, + waitTime: { median: false }, + elu: { median: false } + }) + expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ + choiceRetries: 6, + runTime: { median: false }, + waitTime: { median: false }, elu: { median: false } }) for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ + choiceRetries: 6, runTime: { median: false }, + waitTime: { median: false }, elu: { median: false } }) } @@ -586,8 +620,7 @@ describe('Abstract pool test suite', () => { id: expect.any(Number), type: WorkerTypes.thread, dynamic: false, - ready: true, - messageChannel: expect.any(MessageChannel) + ready: true }) } }) @@ -741,7 +774,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 }) @@ -778,7 +811,7 @@ describe('Abstract pool test suite', () => { ) let poolInfo let poolReady = 0 - pool.emitter.on(PoolEvents.ready, info => { + pool.emitter.on(PoolEvents.ready, (info) => { ++poolReady poolInfo = info }) @@ -810,7 +843,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,11 +872,37 @@ describe('Abstract pool test suite', () => { await pool.destroy() }) - it('Verify that multiple tasks worker is working', async () => { + it('Verify that listTaskFunctions() is working', async () => { + const dynamicThreadPool = new DynamicThreadPool( + Math.floor(numberOfWorkers / 2), + numberOfWorkers, + './tests/worker-files/thread/testMultipleTaskFunctionsWorker.js' + ) + await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1) + expect(dynamicThreadPool.listTaskFunctions()).toStrictEqual([ + 'default', + 'jsonIntegerSerialization', + 'factorial', + 'fibonacci' + ]) + const fixedClusterPool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js' + ) + await waitPoolEvents(fixedClusterPool, PoolEvents.ready, 1) + expect(fixedClusterPool.listTaskFunctions()).toStrictEqual([ + 'default', + 'jsonIntegerSerialization', + 'factorial', + 'fibonacci' + ]) + }) + + it('Verify that multiple task functions worker is working', async () => { const pool = new DynamicClusterPool( Math.floor(numberOfWorkers / 2), numberOfWorkers, - './tests/worker-files/cluster/testMultiTasksWorker.js' + './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js' ) const data = { n: 10 } const result0 = await pool.execute(data) @@ -854,5 +913,43 @@ describe('Abstract pool test suite', () => { expect(result2).toBe(3628800) const result3 = await pool.execute(data, 'fibonacci') expect(result3).toBe(55) + expect(pool.info.executingTasks).toBe(0) + expect(pool.info.executedTasks).toBe(4) + for (const workerNode of pool.workerNodes) { + expect(workerNode.info.taskFunctions).toStrictEqual([ + 'default', + 'jsonIntegerSerialization', + 'factorial', + 'fibonacci' + ]) + expect(workerNode.taskFunctionsUsage.size).toBe(3) + for (const name of pool.listTaskFunctions()) { + expect(workerNode.getTaskFunctionWorkerUsage(name)).toStrictEqual({ + tasks: { + executed: expect.any(Number), + executing: expect.any(Number), + failed: 0, + queued: 0 + }, + runTime: { + history: expect.any(CircularArray) + }, + waitTime: { + history: expect.any(CircularArray) + }, + elu: { + idle: { + history: expect.any(CircularArray) + }, + active: { + history: expect.any(CircularArray) + } + } + }) + expect( + workerNode.getTaskFunctionWorkerUsage(name).tasks.executing + ).toBeGreaterThanOrEqual(0) + } + } }) })