6cfe99ae2f1198592d940bd0b1414d4fa038bb36
[poolifier.git] / tests / utils.test.js
1 const { randomInt } = require('crypto')
2 const { expect } = require('expect')
3 const {
4 CircularArray,
5 DEFAULT_CIRCULAR_ARRAY_SIZE
6 } = require('../lib/circular-array')
7 const {
8 availableParallelism,
9 average,
10 exponentialDelay,
11 isAsyncFunction,
12 isKillBehavior,
13 isPlainObject,
14 median,
15 round,
16 secureRandom,
17 sleep,
18 updateMeasurementStatistics
19 } = require('../lib/utils')
20 const { KillBehaviors } = require('../lib/worker/worker-options')
21
22 describe('Utils test suite', () => {
23 it('Verify availableParallelism() behavior', () => {
24 expect(typeof availableParallelism() === 'number').toBe(true)
25 expect(availableParallelism()).toBeGreaterThan(0)
26 expect(Number.isSafeInteger(availableParallelism())).toBe(true)
27 })
28
29 it.skip('Verify sleep() behavior', async () => {
30 const start = performance.now()
31 await sleep(1000)
32 const elapsed = performance.now() - start
33 expect(elapsed).toBeGreaterThanOrEqual(1000)
34 })
35
36 it('Verify exponentialDelay() behavior', () => {
37 const delay = exponentialDelay(randomInt(1000))
38 expect(typeof delay === 'number').toBe(true)
39 expect(delay).toBeGreaterThanOrEqual(Number.MIN_VALUE)
40 expect(delay).toBeLessThanOrEqual(Number.MAX_VALUE)
41 })
42
43 it('Verify average() computation', () => {
44 expect(average([])).toBe(0)
45 expect(average([0.08])).toBe(0.08)
46 expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(
47 3.1642857142857146
48 )
49 expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(
50 2.8533333333333335
51 )
52 })
53
54 it('Verify median() computation', () => {
55 expect(median([])).toBe(0)
56 expect(median([0.08])).toBe(0.08)
57 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05)
58 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
59 })
60
61 it('Verify round() behavior', () => {
62 expect(round(0)).toBe(0)
63 expect(round(0.5, 0)).toBe(1)
64 expect(round(0.5)).toBe(0.5)
65 expect(round(-0.5, 0)).toBe(-1)
66 expect(round(-0.5)).toBe(-0.5)
67 expect(round(1.005)).toBe(1.01)
68 expect(round(2.175)).toBe(2.18)
69 expect(round(5.015)).toBe(5.02)
70 expect(round(-1.005)).toBe(-1.01)
71 expect(round(-2.175)).toBe(-2.18)
72 expect(round(-5.015)).toBe(-5.02)
73 })
74
75 it('Verify isPlainObject() behavior', () => {
76 expect(isPlainObject(null)).toBe(false)
77 expect(isPlainObject(undefined)).toBe(false)
78 expect(isPlainObject(true)).toBe(false)
79 expect(isPlainObject(false)).toBe(false)
80 expect(isPlainObject(0)).toBe(false)
81 expect(isPlainObject('')).toBe(false)
82 expect(isPlainObject([])).toBe(false)
83 expect(isPlainObject(() => {})).toBe(false)
84 expect(isPlainObject(new Date())).toBe(false)
85 expect(isPlainObject(new RegExp())).toBe(false)
86 expect(isPlainObject(new Error())).toBe(false)
87 expect(isPlainObject(new Map())).toBe(false)
88 expect(isPlainObject(new Set())).toBe(false)
89 expect(isPlainObject(new WeakMap())).toBe(false)
90 expect(isPlainObject(new WeakSet())).toBe(false)
91 expect(isPlainObject(new Int8Array())).toBe(false)
92 expect(isPlainObject(new Uint8Array())).toBe(false)
93 expect(isPlainObject(new Uint8ClampedArray())).toBe(false)
94 expect(isPlainObject(new Int16Array())).toBe(false)
95 expect(isPlainObject(new Uint16Array())).toBe(false)
96 expect(isPlainObject(new Int32Array())).toBe(false)
97 expect(isPlainObject(new Uint32Array())).toBe(false)
98 expect(isPlainObject(new Float32Array())).toBe(false)
99 expect(isPlainObject(new Float64Array())).toBe(false)
100 expect(isPlainObject(new BigInt64Array())).toBe(false)
101 expect(isPlainObject(new BigUint64Array())).toBe(false)
102 expect(isPlainObject(new Promise(() => {}))).toBe(false)
103 expect(isPlainObject(new WeakRef({}))).toBe(false)
104 expect(isPlainObject(new FinalizationRegistry(() => {}))).toBe(false)
105 expect(isPlainObject(new ArrayBuffer())).toBe(false)
106 expect(isPlainObject(new SharedArrayBuffer())).toBe(false)
107 expect(isPlainObject(new DataView(new ArrayBuffer()))).toBe(false)
108 expect(isPlainObject({})).toBe(true)
109 expect(isPlainObject({ a: 1 })).toBe(true)
110 })
111
112 it('Verify isKillBehavior() behavior', () => {
113 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.SOFT)).toBe(true)
114 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.HARD)).toBe(false)
115 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.HARD)).toBe(true)
116 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.SOFT)).toBe(false)
117 expect(isKillBehavior(KillBehaviors.SOFT)).toBe(false)
118 expect(isKillBehavior(KillBehaviors.HARD)).toBe(false)
119 expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false)
120 expect(isKillBehavior(KillBehaviors.HARD, undefined)).toBe(false)
121 expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
122 })
123
124 it('Verify isAsyncFunction() behavior', () => {
125 expect(isAsyncFunction(null)).toBe(false)
126 expect(isAsyncFunction(undefined)).toBe(false)
127 expect(isAsyncFunction(true)).toBe(false)
128 expect(isAsyncFunction(false)).toBe(false)
129 expect(isAsyncFunction(0)).toBe(false)
130 expect(isAsyncFunction('')).toBe(false)
131 expect(isAsyncFunction([])).toBe(false)
132 expect(isAsyncFunction(new Date())).toBe(false)
133 expect(isAsyncFunction(new RegExp())).toBe(false)
134 expect(isAsyncFunction(new Error())).toBe(false)
135 expect(isAsyncFunction(new Map())).toBe(false)
136 expect(isAsyncFunction(new Set())).toBe(false)
137 expect(isAsyncFunction(new WeakMap())).toBe(false)
138 expect(isAsyncFunction(new WeakSet())).toBe(false)
139 expect(isAsyncFunction(new Int8Array())).toBe(false)
140 expect(isAsyncFunction(new Uint8Array())).toBe(false)
141 expect(isAsyncFunction(new Uint8ClampedArray())).toBe(false)
142 expect(isAsyncFunction(new Int16Array())).toBe(false)
143 expect(isAsyncFunction(new Uint16Array())).toBe(false)
144 expect(isAsyncFunction(new Int32Array())).toBe(false)
145 expect(isAsyncFunction(new Uint32Array())).toBe(false)
146 expect(isAsyncFunction(new Float32Array())).toBe(false)
147 expect(isAsyncFunction(new Float64Array())).toBe(false)
148 expect(isAsyncFunction(new BigInt64Array())).toBe(false)
149 expect(isAsyncFunction(new BigUint64Array())).toBe(false)
150 expect(isAsyncFunction(new Promise(() => {}))).toBe(false)
151 expect(isAsyncFunction(new WeakRef({}))).toBe(false)
152 expect(isAsyncFunction(new FinalizationRegistry(() => {}))).toBe(false)
153 expect(isAsyncFunction(new ArrayBuffer())).toBe(false)
154 expect(isAsyncFunction(new SharedArrayBuffer())).toBe(false)
155 expect(isAsyncFunction(new DataView(new ArrayBuffer()))).toBe(false)
156 expect(isAsyncFunction({})).toBe(false)
157 expect(isAsyncFunction({ a: 1 })).toBe(false)
158 expect(isAsyncFunction(() => {})).toBe(false)
159 expect(isAsyncFunction(function () {})).toBe(false)
160 expect(isAsyncFunction(function named () {})).toBe(false)
161 expect(isAsyncFunction(async () => {})).toBe(true)
162 expect(isAsyncFunction(async function () {})).toBe(true)
163 expect(isAsyncFunction(async function named () {})).toBe(true)
164 })
165
166 it('Verify updateMeasurementStatistics() behavior', () => {
167 const measurementStatistics = {
168 history: new CircularArray()
169 }
170 updateMeasurementStatistics(
171 measurementStatistics,
172 { aggregate: true, average: false, median: false },
173 0.01
174 )
175 expect(measurementStatistics).toStrictEqual({
176 aggregate: 0.01,
177 maximum: 0.01,
178 minimum: 0.01,
179 history: new CircularArray()
180 })
181 updateMeasurementStatistics(
182 measurementStatistics,
183 { aggregate: true, average: false, median: false },
184 0.02
185 )
186 expect(measurementStatistics).toStrictEqual({
187 aggregate: 0.03,
188 maximum: 0.02,
189 minimum: 0.01,
190 history: new CircularArray()
191 })
192 updateMeasurementStatistics(
193 measurementStatistics,
194 { aggregate: true, average: true, median: false },
195 0.001
196 )
197 expect(measurementStatistics).toStrictEqual({
198 aggregate: 0.031,
199 maximum: 0.02,
200 minimum: 0.001,
201 average: 0.001,
202 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001)
203 })
204 updateMeasurementStatistics(
205 measurementStatistics,
206 { aggregate: true, average: true, median: false },
207 0.003
208 )
209 expect(measurementStatistics).toStrictEqual({
210 aggregate: 0.034,
211 maximum: 0.02,
212 minimum: 0.001,
213 average: 0.002,
214 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001, 0.003)
215 })
216 })
217
218 it('Verify secureRandom() behavior', () => {
219 const randomNumber = secureRandom()
220 expect(typeof randomNumber === 'number').toBe(true)
221 expect(randomNumber).toBeGreaterThanOrEqual(0)
222 expect(randomNumber).toBeLessThan(1)
223 })
224 })