From bfc75ccaf49d915d2b7e73c92360787b3245321a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 19 Sep 2023 23:27:47 +0200 Subject: [PATCH] test: refactor pool tests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 6 +- src/pools/utils.ts | 50 +++++++++- src/utils.ts | 51 +--------- .../{abstract => }/abstract-pool.test.js | 14 +-- tests/pools/utils.test.js | 95 +++++++++++++++++++ .../pools/{abstract => }/worker-node.test.js | 10 +- tests/utils.test.js | 94 +----------------- 7 files changed, 160 insertions(+), 160 deletions(-) rename tests/pools/{abstract => }/abstract-pool.test.js (99%) create mode 100644 tests/pools/utils.test.js rename tests/pools/{abstract => }/worker-node.test.js (96%) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index ce59c992..52f9c0e3 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -16,8 +16,7 @@ import { max, median, min, - round, - updateMeasurementStatistics + round } from '../utils' import { KillBehaviors } from '../worker/worker-options' import type { TaskFunction } from '../worker/task-functions' @@ -51,7 +50,8 @@ import { WorkerNode } from './worker-node' import { checkFilePath, checkValidTasksQueueOptions, - checkValidWorkerChoiceStrategy + checkValidWorkerChoiceStrategy, + updateMeasurementStatistics } from './utils' /** diff --git a/src/pools/utils.ts b/src/pools/utils.ts index e534ead2..e08b31aa 100644 --- a/src/pools/utils.ts +++ b/src/pools/utils.ts @@ -1,11 +1,12 @@ import { existsSync } from 'node:fs' -import { isPlainObject } from '../utils' +import { average, isPlainObject, max, median, min } from '../utils' import { + type MeasurementStatisticsRequirements, WorkerChoiceStrategies, type WorkerChoiceStrategy } from './selection-strategies/selection-strategies-types' import type { TasksQueueOptions } from './pool' -import type { IWorker } from './worker' +import type { IWorker, MeasurementStatistics } from './worker' export const checkFilePath = (filePath: string): void => { if ( @@ -91,6 +92,7 @@ export const checkValidTasksQueueOptions = ( ) } } + export const checkWorkerNodeArguments = ( worker: Worker, tasksQueueBackPressureSize: number @@ -114,3 +116,47 @@ export const checkWorkerNodeArguments = ( ) } } + +/** + * Updates the given measurement statistics. + * + * @param measurementStatistics - The measurement statistics to update. + * @param measurementRequirements - The measurement statistics requirements. + * @param measurementValue - The measurement value. + * @param numberOfMeasurements - The number of measurements. + * @internal + */ +export const updateMeasurementStatistics = ( + measurementStatistics: MeasurementStatistics, + measurementRequirements: MeasurementStatisticsRequirements, + measurementValue: number +): void => { + if (measurementRequirements.aggregate) { + measurementStatistics.aggregate = + (measurementStatistics.aggregate ?? 0) + measurementValue + measurementStatistics.minimum = min( + measurementValue, + measurementStatistics.minimum ?? Infinity + ) + measurementStatistics.maximum = max( + measurementValue, + measurementStatistics.maximum ?? -Infinity + ) + if ( + (measurementRequirements.average || measurementRequirements.median) && + measurementValue != null + ) { + measurementStatistics.history.push(measurementValue) + if (measurementRequirements.average) { + measurementStatistics.average = average(measurementStatistics.history) + } else if (measurementStatistics.average != null) { + delete measurementStatistics.average + } + if (measurementRequirements.median) { + measurementStatistics.median = median(measurementStatistics.history) + } else if (measurementStatistics.median != null) { + delete measurementStatistics.median + } + } + } +} diff --git a/src/utils.ts b/src/utils.ts index 400a92b1..d7ce5a21 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,12 +7,7 @@ import type { WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types' import type { KillBehavior } from './worker/worker-options' -import { - type IWorker, - type MeasurementStatistics, - type WorkerType, - WorkerTypes -} from './pools/worker' +import { type IWorker, type WorkerType, WorkerTypes } from './pools/worker' /** * Default task name. @@ -224,50 +219,6 @@ export const isAsyncFunction = ( return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction' } -/** - * Updates the given measurement statistics. - * - * @param measurementStatistics - The measurement statistics to update. - * @param measurementRequirements - The measurement statistics requirements. - * @param measurementValue - The measurement value. - * @param numberOfMeasurements - The number of measurements. - * @internal - */ -export const updateMeasurementStatistics = ( - measurementStatistics: MeasurementStatistics, - measurementRequirements: MeasurementStatisticsRequirements, - measurementValue: number -): void => { - if (measurementRequirements.aggregate) { - measurementStatistics.aggregate = - (measurementStatistics.aggregate ?? 0) + measurementValue - measurementStatistics.minimum = min( - measurementValue, - measurementStatistics.minimum ?? Infinity - ) - measurementStatistics.maximum = max( - measurementValue, - measurementStatistics.maximum ?? -Infinity - ) - if ( - (measurementRequirements.average || measurementRequirements.median) && - measurementValue != null - ) { - measurementStatistics.history.push(measurementValue) - if (measurementRequirements.average) { - measurementStatistics.average = average(measurementStatistics.history) - } else if (measurementStatistics.average != null) { - delete measurementStatistics.average - } - if (measurementRequirements.median) { - measurementStatistics.median = median(measurementStatistics.history) - } else if (measurementStatistics.median != null) { - delete measurementStatistics.median - } - } - } -} - /** * Generates a cryptographically secure random number in the [0,1[ range * diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract-pool.test.js similarity index 99% rename from tests/pools/abstract/abstract-pool.test.js rename to tests/pools/abstract-pool.test.js index f6a54d5b..3860a0eb 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract-pool.test.js @@ -10,13 +10,13 @@ const { PoolTypes, WorkerChoiceStrategies, WorkerTypes -} = require('../../../lib') -const { CircularArray } = require('../../../lib/circular-array') -const { Deque } = require('../../../lib/deque') -const { DEFAULT_TASK_NAME } = require('../../../lib/utils') -const { version } = require('../../../package.json') -const { waitPoolEvents } = require('../../test-utils') -const { WorkerNode } = require('../../../lib/pools/worker-node') +} = require('../../lib') +const { CircularArray } = require('../../lib/circular-array') +const { Deque } = require('../../lib/deque') +const { DEFAULT_TASK_NAME } = require('../../lib/utils') +const { version } = require('../../package.json') +const { waitPoolEvents } = require('../test-utils') +const { WorkerNode } = require('../../lib/pools/worker-node') describe('Abstract pool test suite', () => { const numberOfWorkers = 2 diff --git a/tests/pools/utils.test.js b/tests/pools/utils.test.js new file mode 100644 index 00000000..23ef2e2f --- /dev/null +++ b/tests/pools/utils.test.js @@ -0,0 +1,95 @@ +const { expect } = require('expect') +const { + DEFAULT_CIRCULAR_ARRAY_SIZE, + CircularArray +} = require('../../lib/circular-array') +const { updateMeasurementStatistics } = require('../../lib/pools/utils') + +describe('Pool utils test suite', () => { + it('Verify updateMeasurementStatistics() behavior', () => { + const measurementStatistics = { + history: new CircularArray() + } + updateMeasurementStatistics( + measurementStatistics, + { aggregate: true, average: false, median: false }, + 0.01 + ) + expect(measurementStatistics).toStrictEqual({ + aggregate: 0.01, + maximum: 0.01, + minimum: 0.01, + history: new CircularArray() + }) + updateMeasurementStatistics( + measurementStatistics, + { aggregate: true, average: false, median: false }, + 0.02 + ) + expect(measurementStatistics).toStrictEqual({ + aggregate: 0.03, + maximum: 0.02, + minimum: 0.01, + history: new CircularArray() + }) + updateMeasurementStatistics( + measurementStatistics, + { aggregate: true, average: true, median: false }, + 0.001 + ) + expect(measurementStatistics).toStrictEqual({ + aggregate: 0.031, + maximum: 0.02, + minimum: 0.001, + average: 0.001, + history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001) + }) + updateMeasurementStatistics( + measurementStatistics, + { aggregate: true, average: true, median: false }, + 0.003 + ) + expect(measurementStatistics).toStrictEqual({ + aggregate: 0.034, + maximum: 0.02, + minimum: 0.001, + average: 0.002, + history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001, 0.003) + }) + updateMeasurementStatistics( + measurementStatistics, + { aggregate: true, average: false, median: true }, + 0.006 + ) + expect(measurementStatistics).toStrictEqual({ + aggregate: 0.04, + maximum: 0.02, + minimum: 0.001, + median: 0.003, + history: new CircularArray( + DEFAULT_CIRCULAR_ARRAY_SIZE, + 0.001, + 0.003, + 0.006 + ) + }) + updateMeasurementStatistics( + measurementStatistics, + { aggregate: true, average: true, median: false }, + 0.01 + ) + expect(measurementStatistics).toStrictEqual({ + aggregate: 0.05, + maximum: 0.02, + minimum: 0.001, + average: 0.005, + history: new CircularArray( + DEFAULT_CIRCULAR_ARRAY_SIZE, + 0.001, + 0.003, + 0.006, + 0.01 + ) + }) + }) +}) diff --git a/tests/pools/abstract/worker-node.test.js b/tests/pools/worker-node.test.js similarity index 96% rename from tests/pools/abstract/worker-node.test.js rename to tests/pools/worker-node.test.js index ee469eda..bfbd5d8d 100644 --- a/tests/pools/abstract/worker-node.test.js +++ b/tests/pools/worker-node.test.js @@ -1,11 +1,11 @@ const { MessageChannel, Worker } = require('node:worker_threads') const cluster = require('node:cluster') const { expect } = require('expect') -const { WorkerNode } = require('../../../lib/pools/worker-node') -const { WorkerTypes } = require('../../../lib') -const { CircularArray } = require('../../../lib/circular-array') -const { Deque } = require('../../../lib/deque') -const { DEFAULT_TASK_NAME } = require('../../../lib/utils') +const { WorkerNode } = require('../../lib/pools/worker-node') +const { WorkerTypes } = require('../../lib') +const { CircularArray } = require('../../lib/circular-array') +const { Deque } = require('../../lib/deque') +const { DEFAULT_TASK_NAME } = require('../../lib/utils') describe('Worker node test suite', () => { const threadWorker = new Worker('./tests/worker-files/thread/testWorker.js') diff --git a/tests/utils.test.js b/tests/utils.test.js index 5041f102..061c1648 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -3,10 +3,6 @@ const { Worker } = require('node:worker_threads') const cluster = require('node:cluster') const os = require('node:os') const { expect } = require('expect') -const { - CircularArray, - DEFAULT_CIRCULAR_ARRAY_SIZE -} = require('../lib/circular-array') const { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, DEFAULT_TASK_NAME, @@ -25,8 +21,7 @@ const { min, round, secureRandom, - sleep, - updateMeasurementStatistics + sleep } = require('../lib/utils') const { KillBehaviors, WorkerTypes } = require('../lib') @@ -220,93 +215,6 @@ describe('Utils test suite', () => { expect(isAsyncFunction(async function named () {})).toBe(true) }) - it('Verify updateMeasurementStatistics() behavior', () => { - const measurementStatistics = { - history: new CircularArray() - } - updateMeasurementStatistics( - measurementStatistics, - { aggregate: true, average: false, median: false }, - 0.01 - ) - expect(measurementStatistics).toStrictEqual({ - aggregate: 0.01, - maximum: 0.01, - minimum: 0.01, - history: new CircularArray() - }) - updateMeasurementStatistics( - measurementStatistics, - { aggregate: true, average: false, median: false }, - 0.02 - ) - expect(measurementStatistics).toStrictEqual({ - aggregate: 0.03, - maximum: 0.02, - minimum: 0.01, - history: new CircularArray() - }) - updateMeasurementStatistics( - measurementStatistics, - { aggregate: true, average: true, median: false }, - 0.001 - ) - expect(measurementStatistics).toStrictEqual({ - aggregate: 0.031, - maximum: 0.02, - minimum: 0.001, - average: 0.001, - history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001) - }) - updateMeasurementStatistics( - measurementStatistics, - { aggregate: true, average: true, median: false }, - 0.003 - ) - expect(measurementStatistics).toStrictEqual({ - aggregate: 0.034, - maximum: 0.02, - minimum: 0.001, - average: 0.002, - history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001, 0.003) - }) - updateMeasurementStatistics( - measurementStatistics, - { aggregate: true, average: false, median: true }, - 0.006 - ) - expect(measurementStatistics).toStrictEqual({ - aggregate: 0.04, - maximum: 0.02, - minimum: 0.001, - median: 0.003, - history: new CircularArray( - DEFAULT_CIRCULAR_ARRAY_SIZE, - 0.001, - 0.003, - 0.006 - ) - }) - updateMeasurementStatistics( - measurementStatistics, - { aggregate: true, average: true, median: false }, - 0.01 - ) - expect(measurementStatistics).toStrictEqual({ - aggregate: 0.05, - maximum: 0.02, - minimum: 0.001, - average: 0.005, - history: new CircularArray( - DEFAULT_CIRCULAR_ARRAY_SIZE, - 0.001, - 0.003, - 0.006, - 0.01 - ) - }) - }) - it('Verify secureRandom() behavior', () => { const randomNumber = secureRandom() expect(typeof randomNumber === 'number').toBe(true) -- 2.34.1