X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Futils.test.mjs;h=c6cc06d31487f2b492850203fb64ceda79d2ae87;hb=53a0019f59b1d5f10be6acbaca2e6a970dd1fc6c;hp=610b92c3ce62cba912ab9273b14d5cb2077f9884;hpb=c63a35a04c190989be80f9218d97e0aca739475e;p=poolifier.git diff --git a/tests/pools/utils.test.mjs b/tests/pools/utils.test.mjs index 610b92c3..c6cc06d3 100644 --- a/tests/pools/utils.test.mjs +++ b/tests/pools/utils.test.mjs @@ -1,18 +1,37 @@ +import cluster, { Worker as ClusterWorker } from 'node:cluster' import { Worker as ThreadWorker } from 'node:worker_threads' -import { Worker as ClusterWorker } from 'node:cluster' + import { expect } from 'expect' + import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from '../../lib/circular-array.cjs' import { + FixedClusterPool, + FixedThreadPool, + WorkerTypes +} from '../../lib/index.cjs' +import { + buildWorkerChoiceStrategyOptions, createWorker, + DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, getDefaultTasksQueueOptions, + getWorkerChoiceStrategyRetries, + getWorkerId, + getWorkerType, updateMeasurementStatistics } from '../../lib/pools/utils.cjs' -import { WorkerTypes } from '../../lib/index.cjs' describe('Pool utils test suite', () => { + it('Verify DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS values', () => { + expect(DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS).toStrictEqual({ + aggregate: false, + average: false, + median: false + }) + }) + it('Verify getDefaultTasksQueueOptions() behavior', () => { const poolMaxSize = 4 expect(getDefaultTasksQueueOptions(poolMaxSize)).toStrictEqual({ @@ -24,6 +43,61 @@ describe('Pool utils test suite', () => { }) }) + it('Verify getWorkerChoiceStrategyRetries() behavior', async () => { + const numberOfThreads = 4 + const pool = new FixedThreadPool( + numberOfThreads, + './tests/worker-files/thread/testWorker.mjs' + ) + expect(getWorkerChoiceStrategyRetries(pool)).toBe(pool.info.maxSize * 2) + const workerChoiceStrategyOptions = { + runTime: { median: true }, + waitTime: { median: true }, + elu: { median: true }, + weights: { + 0: 100, + 1: 100 + } + } + expect( + getWorkerChoiceStrategyRetries(pool, workerChoiceStrategyOptions) + ).toBe( + pool.info.maxSize + + Object.keys(workerChoiceStrategyOptions.weights).length + ) + await pool.destroy() + }) + + it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => { + const numberOfWorkers = 4 + const pool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/testWorker.cjs' + ) + expect(buildWorkerChoiceStrategyOptions(pool)).toStrictEqual({ + runTime: { median: false }, + waitTime: { median: false }, + elu: { median: false }, + weights: expect.objectContaining({ + 0: expect.any(Number), + [pool.info.maxSize - 1]: expect.any(Number) + }) + }) + const workerChoiceStrategyOptions = { + runTime: { median: true }, + waitTime: { median: true }, + elu: { median: true }, + weights: { + 0: 100, + 1: 100 + } + } + expect( + buildWorkerChoiceStrategyOptions(pool, workerChoiceStrategyOptions) + ).toStrictEqual(workerChoiceStrategyOptions) + await pool.destroy() + }) + it('Verify updateMeasurementStatistics() behavior', () => { const measurementStatistics = { history: new CircularArray() @@ -127,4 +201,22 @@ describe('Pool utils test suite', () => { ) ).toBeInstanceOf(ClusterWorker) }) + + it('Verify getWorkerType() behavior', () => { + expect( + getWorkerType( + new ThreadWorker('./tests/worker-files/thread/testWorker.mjs') + ) + ).toBe(WorkerTypes.thread) + expect(getWorkerType(cluster.fork())).toBe(WorkerTypes.cluster) + }) + + it('Verify getWorkerId() behavior', () => { + const threadWorker = new ThreadWorker( + './tests/worker-files/thread/testWorker.mjs' + ) + const clusterWorker = cluster.fork() + expect(getWorkerId(threadWorker)).toBe(threadWorker.threadId) + expect(getWorkerId(clusterWorker)).toBe(clusterWorker.id) + }) })