1 import cluster, { Worker as ClusterWorker } from 'node:cluster'
2 import { Worker as ThreadWorker } from 'node:worker_threads'
4 import { expect } from 'expect'
8 DEFAULT_CIRCULAR_ARRAY_SIZE
9 } from '../../lib/circular-array.cjs'
10 import { WorkerTypes } from '../../lib/index.cjs'
13 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
14 getDefaultTasksQueueOptions,
17 updateMeasurementStatistics
18 } from '../../lib/pools/utils.cjs'
20 describe('Pool utils test suite', () => {
21 it('Verify DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS values', () => {
22 expect(DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS).toStrictEqual({
29 it('Verify getDefaultTasksQueueOptions() behavior', () => {
31 expect(getDefaultTasksQueueOptions(poolMaxSize)).toStrictEqual({
33 size: Math.pow(poolMaxSize, 2),
35 tasksStealingOnBackPressure: false,
36 tasksFinishedTimeout: 2000
40 it('Verify updateMeasurementStatistics() behavior', () => {
41 const measurementStatistics = {
42 history: new CircularArray()
44 updateMeasurementStatistics(
45 measurementStatistics,
46 { aggregate: true, average: false, median: false },
49 expect(measurementStatistics).toStrictEqual({
53 history: new CircularArray()
55 updateMeasurementStatistics(
56 measurementStatistics,
57 { aggregate: true, average: false, median: false },
60 expect(measurementStatistics).toStrictEqual({
64 history: new CircularArray()
66 updateMeasurementStatistics(
67 measurementStatistics,
68 { aggregate: true, average: true, median: false },
71 expect(measurementStatistics).toStrictEqual({
76 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001)
78 updateMeasurementStatistics(
79 measurementStatistics,
80 { aggregate: true, average: true, median: false },
83 expect(measurementStatistics).toStrictEqual({
88 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001, 0.003)
90 updateMeasurementStatistics(
91 measurementStatistics,
92 { aggregate: true, average: false, median: true },
95 expect(measurementStatistics).toStrictEqual({
100 history: new CircularArray(
101 DEFAULT_CIRCULAR_ARRAY_SIZE,
107 updateMeasurementStatistics(
108 measurementStatistics,
109 { aggregate: true, average: true, median: false },
112 expect(measurementStatistics).toStrictEqual({
117 history: new CircularArray(
118 DEFAULT_CIRCULAR_ARRAY_SIZE,
127 it('Verify createWorker() behavior', () => {
131 './tests/worker-files/thread/testWorker.mjs',
134 ).toBeInstanceOf(ThreadWorker)
138 './tests/worker-files/cluster/testWorker.mjs',
141 ).toBeInstanceOf(ClusterWorker)
144 it('Verify getWorkerType() behavior', () => {
147 new ThreadWorker('./tests/worker-files/thread/testWorker.mjs')
149 ).toBe(WorkerTypes.thread)
150 expect(getWorkerType(cluster.fork())).toBe(WorkerTypes.cluster)
153 it('Verify getWorkerId() behavior', () => {
154 const threadWorker = new ThreadWorker(
155 './tests/worker-files/thread/testWorker.mjs'
157 const clusterWorker = cluster.fork()
158 expect(getWorkerId(threadWorker)).toBe(threadWorker.threadId)
159 expect(getWorkerId(clusterWorker)).toBe(clusterWorker.id)