build(deps-dev): apply updates
[poolifier.git] / tests / pools / utils.test.mjs
1 import cluster, { Worker as ClusterWorker } from 'node:cluster'
2 import { Worker as ThreadWorker } from 'node:worker_threads'
3
4 import { expect } from 'expect'
5
6 import {
7 CircularArray,
8 DEFAULT_CIRCULAR_ARRAY_SIZE
9 } from '../../lib/circular-array.cjs'
10 import { WorkerTypes } from '../../lib/index.cjs'
11 import {
12 createWorker,
13 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
14 getDefaultTasksQueueOptions,
15 getWorkerId,
16 getWorkerType,
17 updateMeasurementStatistics
18 } from '../../lib/pools/utils.cjs'
19
20 describe('Pool utils test suite', () => {
21 it('Verify DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS values', () => {
22 expect(DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS).toStrictEqual({
23 aggregate: false,
24 average: false,
25 median: false
26 })
27 })
28
29 it('Verify getDefaultTasksQueueOptions() behavior', () => {
30 const poolMaxSize = 4
31 expect(getDefaultTasksQueueOptions(poolMaxSize)).toStrictEqual({
32 concurrency: 1,
33 size: Math.pow(poolMaxSize, 2),
34 taskStealing: true,
35 tasksStealingOnBackPressure: false,
36 tasksFinishedTimeout: 2000
37 })
38 })
39
40 it('Verify updateMeasurementStatistics() behavior', () => {
41 const measurementStatistics = {
42 history: new CircularArray()
43 }
44 updateMeasurementStatistics(
45 measurementStatistics,
46 { aggregate: true, average: false, median: false },
47 0.01
48 )
49 expect(measurementStatistics).toStrictEqual({
50 aggregate: 0.01,
51 maximum: 0.01,
52 minimum: 0.01,
53 history: new CircularArray()
54 })
55 updateMeasurementStatistics(
56 measurementStatistics,
57 { aggregate: true, average: false, median: false },
58 0.02
59 )
60 expect(measurementStatistics).toStrictEqual({
61 aggregate: 0.03,
62 maximum: 0.02,
63 minimum: 0.01,
64 history: new CircularArray()
65 })
66 updateMeasurementStatistics(
67 measurementStatistics,
68 { aggregate: true, average: true, median: false },
69 0.001
70 )
71 expect(measurementStatistics).toStrictEqual({
72 aggregate: 0.031,
73 maximum: 0.02,
74 minimum: 0.001,
75 average: 0.001,
76 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001)
77 })
78 updateMeasurementStatistics(
79 measurementStatistics,
80 { aggregate: true, average: true, median: false },
81 0.003
82 )
83 expect(measurementStatistics).toStrictEqual({
84 aggregate: 0.034,
85 maximum: 0.02,
86 minimum: 0.001,
87 average: 0.002,
88 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001, 0.003)
89 })
90 updateMeasurementStatistics(
91 measurementStatistics,
92 { aggregate: true, average: false, median: true },
93 0.006
94 )
95 expect(measurementStatistics).toStrictEqual({
96 aggregate: 0.04,
97 maximum: 0.02,
98 minimum: 0.001,
99 median: 0.003,
100 history: new CircularArray(
101 DEFAULT_CIRCULAR_ARRAY_SIZE,
102 0.001,
103 0.003,
104 0.006
105 )
106 })
107 updateMeasurementStatistics(
108 measurementStatistics,
109 { aggregate: true, average: true, median: false },
110 0.01
111 )
112 expect(measurementStatistics).toStrictEqual({
113 aggregate: 0.05,
114 maximum: 0.02,
115 minimum: 0.001,
116 average: 0.005,
117 history: new CircularArray(
118 DEFAULT_CIRCULAR_ARRAY_SIZE,
119 0.001,
120 0.003,
121 0.006,
122 0.01
123 )
124 })
125 })
126
127 it('Verify createWorker() behavior', () => {
128 expect(
129 createWorker(
130 WorkerTypes.thread,
131 './tests/worker-files/thread/testWorker.mjs',
132 {}
133 )
134 ).toBeInstanceOf(ThreadWorker)
135 expect(
136 createWorker(
137 WorkerTypes.cluster,
138 './tests/worker-files/cluster/testWorker.mjs',
139 {}
140 )
141 ).toBeInstanceOf(ClusterWorker)
142 })
143
144 it('Verify getWorkerType() behavior', () => {
145 expect(
146 getWorkerType(
147 new ThreadWorker('./tests/worker-files/thread/testWorker.mjs')
148 )
149 ).toBe(WorkerTypes.thread)
150 expect(getWorkerType(cluster.fork())).toBe(WorkerTypes.cluster)
151 })
152
153 it('Verify getWorkerId() behavior', () => {
154 const threadWorker = new ThreadWorker(
155 './tests/worker-files/thread/testWorker.mjs'
156 )
157 const clusterWorker = cluster.fork()
158 expect(getWorkerId(threadWorker)).toBe(threadWorker.threadId)
159 expect(getWorkerId(clusterWorker)).toBe(clusterWorker.id)
160 })
161 })