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