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