docs: refine worker choice strategies documentation
[poolifier.git] / tests / utils.test.js
CommitLineData
aba955e1 1const { expect } = require('expect')
dc021bcc
JB
2const {
3 CircularArray,
4 DEFAULT_CIRCULAR_ARRAY_SIZE
5} = require('../lib/circular-array')
afe0d5bf
JB
6const {
7 availableParallelism,
dc021bcc 8 average,
78ac1a90 9 isAsyncFunction,
59317253 10 isKillBehavior,
afe0d5bf
JB
11 isPlainObject,
12 median,
a91f7b35 13 round,
970b38d6 14 secureRandom,
a91f7b35 15 updateMeasurementStatistics
afe0d5bf 16} = require('../lib/utils')
59317253 17const { KillBehaviors } = require('../lib/worker/worker-options')
aba955e1
JB
18
19describe('Utils test suite', () => {
afe0d5bf
JB
20 it('Verify availableParallelism() behavior', () => {
21 expect(typeof availableParallelism() === 'number').toBe(true)
51c90525
JB
22 expect(availableParallelism()).toBeGreaterThan(0)
23 expect(Number.isSafeInteger(availableParallelism())).toBe(true)
afe0d5bf
JB
24 })
25
dc021bcc
JB
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
bb615bd0 37 it('Verify median() computation', () => {
4a45e8d2 38 expect(median([])).toBe(0)
76845835
JB
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)
aba955e1
JB
42 })
43
afe0d5bf
JB
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
aba955e1
JB
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 })
47aacbaa
JB
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)
78ac1a90 103 expect(isKillBehavior(KillBehaviors.HARD, undefined)).toBe(false)
47aacbaa
JB
104 expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
105 })
78ac1a90
JB
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 })
a91f7b35
JB
148
149 it('Verify updateMeasurementStatistics() behavior', () => {
997bbcba
JB
150 const measurementStatistics = {
151 history: new CircularArray()
152 }
a91f7b35
JB
153 updateMeasurementStatistics(
154 measurementStatistics,
155 { aggregate: true, average: false, median: false },
dc021bcc 156 0.01
a91f7b35 157 )
997bbcba 158 expect(measurementStatistics).toStrictEqual({
a91f7b35
JB
159 aggregate: 0.01,
160 maximum: 0.01,
997bbcba 161 minimum: 0.01,
dc021bcc 162 history: new CircularArray()
a91f7b35
JB
163 })
164 updateMeasurementStatistics(
165 measurementStatistics,
166 { aggregate: true, average: false, median: false },
dc021bcc 167 0.02
a91f7b35 168 )
997bbcba 169 expect(measurementStatistics).toStrictEqual({
a91f7b35
JB
170 aggregate: 0.03,
171 maximum: 0.02,
997bbcba 172 minimum: 0.01,
dc021bcc 173 history: new CircularArray()
a91f7b35
JB
174 })
175 updateMeasurementStatistics(
176 measurementStatistics,
177 { aggregate: true, average: true, median: false },
dc021bcc 178 0.001
a91f7b35 179 )
997bbcba 180 expect(measurementStatistics).toStrictEqual({
a91f7b35
JB
181 aggregate: 0.031,
182 maximum: 0.02,
183 minimum: 0.001,
dc021bcc
JB
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)
a91f7b35
JB
198 })
199 })
970b38d6
JB
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 })
aba955e1 207})