X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=adf2765f7e67bcb68ffb11a534574fa3e18c3907;hb=c05f0d5094264db0ff50636e794201e60ba7f448;hp=45371a31325e6cea532e87edd9b0c9c0fccd78dd;hpb=87de9ff55a7ad494b9e9500208b9b7319c094ea6;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 45371a31..adf2765f 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -93,8 +93,9 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: false, - medWaitTime: false + runTime: { median: false }, + waitTime: { median: false }, + elu: { median: false } }) expect(pool.opts.messageHandler).toBeUndefined() expect(pool.opts.errorHandler).toBeUndefined() @@ -108,7 +109,7 @@ describe('Abstract pool test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED, workerChoiceStrategyOptions: { - medRunTime: true, + runTime: { median: true }, weights: { 0: 300, 1: 200 } }, enableEvents: false, @@ -130,7 +131,7 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.LEAST_USED ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: true, + runTime: { median: true }, weights: { 0: 300, 1: 200 } }) expect(pool.opts.messageHandler).toStrictEqual(testHandler) @@ -147,21 +148,22 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/thread/testWorker.js', { - enableTasksQueue: true, - tasksQueueOptions: { concurrency: 0 } + workerChoiceStrategy: 'invalidStrategy' } ) - ).toThrowError("Invalid worker tasks concurrency '0'") + ).toThrowError("Invalid worker choice strategy 'invalidStrategy'") expect( () => new FixedThreadPool( numberOfWorkers, './tests/worker-files/thread/testWorker.js', { - workerChoiceStrategy: 'invalidStrategy' + workerChoiceStrategyOptions: 'invalidOptions' } ) - ).toThrowError("Invalid worker choice strategy 'invalidStrategy'") + ).toThrowError( + 'Invalid worker choice strategy options: must be a plain object' + ) expect( () => new FixedThreadPool( @@ -174,6 +176,51 @@ describe('Abstract pool test suite', () => { ).toThrowError( '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( + "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" + ) + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: { concurrency: 0 } + } + ) + ).toThrowError("Invalid worker tasks concurrency '0'") + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: 'invalidTasksQueueOptions' + } + ) + ).toThrowError('Invalid tasks queue options: must be a plain object') + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: { concurrency: 0.2 } + } + ) + ).toThrowError('Invalid worker tasks concurrency: must be an integer') }) it('Verify that worker choice strategy options can be set', async () => { @@ -183,65 +230,120 @@ describe('Abstract pool test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: false, - medWaitTime: false + runTime: { median: false }, + waitTime: { median: false }, + elu: { median: false } }) for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ - medRunTime: false, - medWaitTime: false + runTime: { median: false }, + waitTime: { median: false }, + elu: { median: false } }) } expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, - elu: false + 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 } }) - pool.setWorkerChoiceStrategyOptions({ medRunTime: true }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: true + runTime: { median: true }, + elu: { median: true } }) for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { - expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: true }) + expect(workerChoiceStrategy.opts).toStrictEqual({ + runTime: { median: true }, + elu: { median: true } + }) } expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: false, - medRunTime: true, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, - elu: false + 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 } }) - pool.setWorkerChoiceStrategyOptions({ medRunTime: false }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: false + runTime: { median: false }, + elu: { median: false } }) for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { - expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false }) + expect(workerChoiceStrategy.opts).toStrictEqual({ + runTime: { median: false }, + elu: { median: false } + }) } expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, - elu: false + 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( + 'Invalid worker choice strategy options: must be a plain object' + ) + expect(() => + pool.setWorkerChoiceStrategyOptions({ weights: {} }) + ).toThrowError( + 'Invalid worker choice strategy options: must have a weight for each worker node' + ) + expect(() => + pool.setWorkerChoiceStrategyOptions({ measurement: 'invalidMeasurement' }) + ).toThrowError( + "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" + ) await pool.destroy() }) @@ -273,9 +375,15 @@ describe('Abstract pool test suite', () => { expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 }) pool.setTasksQueueOptions({ concurrency: 2 }) expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) + expect(() => + pool.setTasksQueueOptions('invalidTasksQueueOptions') + ).toThrowError('Invalid tasks queue options: must be a plain object') expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError( "Invalid worker tasks concurrency '0'" ) + expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError( + 'Invalid worker tasks concurrency: must be an integer' + ) await pool.destroy() }) @@ -302,7 +410,7 @@ describe('Abstract pool test suite', () => { pool = new DynamicClusterPool( numberOfWorkers, numberOfWorkers * 2, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/cluster/testWorker.js' ) expect(pool.info).toStrictEqual({ type: PoolTypes.dynamic, @@ -324,7 +432,7 @@ describe('Abstract pool test suite', () => { it('Simulate worker not found', async () => { const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, - './tests/worker-files/cluster/testWorker.js', + './tests/worker-files/thread/testWorker.js', { errorHandler: e => console.error(e) } @@ -342,26 +450,41 @@ describe('Abstract pool test suite', () => { './tests/worker-files/cluster/testWorker.js' ) for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: 0, executing: 0, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, - elu: undefined + elu: { + idle: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + active: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + utilization: 0 + } }) } await pool.destroy() @@ -376,6 +499,7 @@ describe('Abstract pool test suite', () => { expect(workerNode.tasksQueue).toBeDefined() expect(workerNode.tasksQueue).toBeInstanceOf(Queue) expect(workerNode.tasksQueue.size).toBe(0) + expect(workerNode.tasksQueue.maxSize).toBe(0) } await pool.destroy() }) @@ -391,50 +515,80 @@ describe('Abstract pool test suite', () => { promises.add(pool.execute()) } for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: 0, executing: maxMultiplier, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, - elu: undefined + elu: { + idle: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + active: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + utilization: 0 + } }) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: maxMultiplier, executing: 0, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, - elu: undefined + elu: { + idle: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + active: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + utilization: 0 + } }) } await pool.destroy() @@ -453,57 +607,85 @@ describe('Abstract pool test suite', () => { } await Promise.all(promises) for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: expect.any(Number), executing: 0, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, - elu: undefined + elu: { + idle: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + active: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + utilization: 0 + } }) - expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0) - expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( - maxMultiplier - ) + expect(workerNode.usage.tasks.executed).toBeGreaterThan(0) + expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(maxMultiplier) } pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: 0, executing: 0, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, - elu: undefined + elu: { + idle: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + active: { + aggregate: 0, + average: 0, + median: 0, + history: expect.any(CircularArray) + }, + utilization: 0 + } }) - expect(workerNode.workerUsage.runTime.history.length).toBe(0) - expect(workerNode.workerUsage.waitTime.history.length).toBe(0) + expect(workerNode.usage.runTime.history.length).toBe(0) + expect(workerNode.usage.waitTime.history.length).toBe(0) } await pool.destroy() }) @@ -595,6 +777,6 @@ describe('Abstract pool test suite', () => { const result2 = await pool.execute(data, 'factorial') expect(result2).toBe(3628800) const result3 = await pool.execute(data, 'fibonacci') - expect(result3).toBe(89) + expect(result3).toBe(55) }) })