Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/express-hybrid...
[poolifier.git] / tests / utils.test.js
... / ...
CommitLineData
1const { randomInt } = require('node:crypto')
2const { Worker } = require('node:worker_threads')
3const cluster = require('node:cluster')
4const os = require('node:os')
5const { expect } = require('expect')
6const {
7 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
8 DEFAULT_TASK_NAME,
9 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
10 EMPTY_FUNCTION,
11 availableParallelism,
12 average,
13 exponentialDelay,
14 getWorkerType,
15 getWorkerId,
16 isAsyncFunction,
17 isKillBehavior,
18 isPlainObject,
19 max,
20 median,
21 min,
22 round,
23 secureRandom,
24 sleep
25} = require('../lib/utils')
26const { KillBehaviors, WorkerTypes } = require('../lib')
27
28describe('Utils test suite', () => {
29 it('Verify DEFAULT_TASK_NAME value', () => {
30 expect(DEFAULT_TASK_NAME).toBe('default')
31 })
32
33 it('Verify EMPTY_FUNCTION value', () => {
34 expect(EMPTY_FUNCTION).toStrictEqual(expect.any(Function))
35 })
36
37 it('Verify DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS values', () => {
38 expect(DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS).toStrictEqual({
39 retries: 6,
40 runTime: { median: false },
41 waitTime: { median: false },
42 elu: { median: false }
43 })
44 })
45
46 it('Verify DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS values', () => {
47 expect(DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS).toStrictEqual({
48 aggregate: false,
49 average: false,
50 median: false
51 })
52 })
53
54 it('Verify availableParallelism() behavior', () => {
55 const parallelism = availableParallelism()
56 expect(typeof parallelism === 'number').toBe(true)
57 expect(Number.isSafeInteger(parallelism)).toBe(true)
58 let expectedParallelism = 1
59 try {
60 expectedParallelism = os.availableParallelism()
61 } catch {
62 expectedParallelism = os.cpus().length
63 }
64 expect(parallelism).toBe(expectedParallelism)
65 })
66
67 it('Verify getWorkerType() behavior', () => {
68 expect(
69 getWorkerType(new Worker('./tests/worker-files/thread/testWorker.js'))
70 ).toBe(WorkerTypes.thread)
71 expect(getWorkerType(cluster.fork())).toBe(WorkerTypes.cluster)
72 })
73
74 it('Verify getWorkerId() behavior', () => {
75 const threadWorker = new Worker('./tests/worker-files/thread/testWorker.js')
76 const clusterWorker = cluster.fork()
77 expect(getWorkerId(threadWorker)).toBe(threadWorker.threadId)
78 expect(getWorkerId(clusterWorker)).toBe(clusterWorker.id)
79 })
80
81 it('Verify sleep() behavior', async () => {
82 const start = performance.now()
83 await sleep(1000)
84 const elapsed = performance.now() - start
85 expect(elapsed).toBeGreaterThanOrEqual(999)
86 })
87
88 it('Verify exponentialDelay() behavior', () => {
89 const delay = exponentialDelay(randomInt(1000))
90 expect(typeof delay === 'number').toBe(true)
91 expect(delay).toBeGreaterThanOrEqual(Number.MIN_VALUE)
92 expect(delay).toBeLessThanOrEqual(Number.MAX_VALUE)
93 })
94
95 it('Verify average() computation', () => {
96 expect(average([])).toBe(0)
97 expect(average([0.08])).toBe(0.08)
98 expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(
99 3.1642857142857146
100 )
101 expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(
102 2.8533333333333335
103 )
104 })
105
106 it('Verify median() computation', () => {
107 expect(median([])).toBe(0)
108 expect(median([0.08])).toBe(0.08)
109 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05)
110 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
111 })
112
113 it('Verify round() behavior', () => {
114 expect(round(0)).toBe(0)
115 expect(round(0.5, 0)).toBe(1)
116 expect(round(0.5)).toBe(0.5)
117 expect(round(-0.5, 0)).toBe(-1)
118 expect(round(-0.5)).toBe(-0.5)
119 expect(round(1.005)).toBe(1.01)
120 expect(round(2.175)).toBe(2.18)
121 expect(round(5.015)).toBe(5.02)
122 expect(round(-1.005)).toBe(-1.01)
123 expect(round(-2.175)).toBe(-2.18)
124 expect(round(-5.015)).toBe(-5.02)
125 })
126
127 it('Verify isPlainObject() behavior', () => {
128 expect(isPlainObject(null)).toBe(false)
129 expect(isPlainObject(undefined)).toBe(false)
130 expect(isPlainObject(true)).toBe(false)
131 expect(isPlainObject(false)).toBe(false)
132 expect(isPlainObject(0)).toBe(false)
133 expect(isPlainObject('')).toBe(false)
134 expect(isPlainObject([])).toBe(false)
135 expect(isPlainObject(() => {})).toBe(false)
136 expect(isPlainObject(new Date())).toBe(false)
137 expect(isPlainObject(new RegExp())).toBe(false)
138 expect(isPlainObject(new Error())).toBe(false)
139 expect(isPlainObject(new Map())).toBe(false)
140 expect(isPlainObject(new Set())).toBe(false)
141 expect(isPlainObject(new WeakMap())).toBe(false)
142 expect(isPlainObject(new WeakSet())).toBe(false)
143 expect(isPlainObject(new Int8Array())).toBe(false)
144 expect(isPlainObject(new Uint8Array())).toBe(false)
145 expect(isPlainObject(new Uint8ClampedArray())).toBe(false)
146 expect(isPlainObject(new Int16Array())).toBe(false)
147 expect(isPlainObject(new Uint16Array())).toBe(false)
148 expect(isPlainObject(new Int32Array())).toBe(false)
149 expect(isPlainObject(new Uint32Array())).toBe(false)
150 expect(isPlainObject(new Float32Array())).toBe(false)
151 expect(isPlainObject(new Float64Array())).toBe(false)
152 expect(isPlainObject(new BigInt64Array())).toBe(false)
153 expect(isPlainObject(new BigUint64Array())).toBe(false)
154 expect(isPlainObject(new Promise(() => {}))).toBe(false)
155 expect(isPlainObject(new WeakRef({}))).toBe(false)
156 expect(isPlainObject(new FinalizationRegistry(() => {}))).toBe(false)
157 expect(isPlainObject(new ArrayBuffer())).toBe(false)
158 expect(isPlainObject(new SharedArrayBuffer())).toBe(false)
159 expect(isPlainObject(new DataView(new ArrayBuffer()))).toBe(false)
160 expect(isPlainObject({})).toBe(true)
161 expect(isPlainObject({ a: 1 })).toBe(true)
162 })
163
164 it('Verify isKillBehavior() behavior', () => {
165 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.SOFT)).toBe(true)
166 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.HARD)).toBe(false)
167 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.HARD)).toBe(true)
168 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.SOFT)).toBe(false)
169 expect(isKillBehavior(KillBehaviors.SOFT)).toBe(false)
170 expect(isKillBehavior(KillBehaviors.HARD)).toBe(false)
171 expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false)
172 expect(isKillBehavior(KillBehaviors.HARD, undefined)).toBe(false)
173 expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
174 })
175
176 it('Verify isAsyncFunction() behavior', () => {
177 expect(isAsyncFunction(null)).toBe(false)
178 expect(isAsyncFunction(undefined)).toBe(false)
179 expect(isAsyncFunction(true)).toBe(false)
180 expect(isAsyncFunction(false)).toBe(false)
181 expect(isAsyncFunction(0)).toBe(false)
182 expect(isAsyncFunction('')).toBe(false)
183 expect(isAsyncFunction([])).toBe(false)
184 expect(isAsyncFunction(new Date())).toBe(false)
185 expect(isAsyncFunction(new RegExp())).toBe(false)
186 expect(isAsyncFunction(new Error())).toBe(false)
187 expect(isAsyncFunction(new Map())).toBe(false)
188 expect(isAsyncFunction(new Set())).toBe(false)
189 expect(isAsyncFunction(new WeakMap())).toBe(false)
190 expect(isAsyncFunction(new WeakSet())).toBe(false)
191 expect(isAsyncFunction(new Int8Array())).toBe(false)
192 expect(isAsyncFunction(new Uint8Array())).toBe(false)
193 expect(isAsyncFunction(new Uint8ClampedArray())).toBe(false)
194 expect(isAsyncFunction(new Int16Array())).toBe(false)
195 expect(isAsyncFunction(new Uint16Array())).toBe(false)
196 expect(isAsyncFunction(new Int32Array())).toBe(false)
197 expect(isAsyncFunction(new Uint32Array())).toBe(false)
198 expect(isAsyncFunction(new Float32Array())).toBe(false)
199 expect(isAsyncFunction(new Float64Array())).toBe(false)
200 expect(isAsyncFunction(new BigInt64Array())).toBe(false)
201 expect(isAsyncFunction(new BigUint64Array())).toBe(false)
202 expect(isAsyncFunction(new Promise(() => {}))).toBe(false)
203 expect(isAsyncFunction(new WeakRef({}))).toBe(false)
204 expect(isAsyncFunction(new FinalizationRegistry(() => {}))).toBe(false)
205 expect(isAsyncFunction(new ArrayBuffer())).toBe(false)
206 expect(isAsyncFunction(new SharedArrayBuffer())).toBe(false)
207 expect(isAsyncFunction(new DataView(new ArrayBuffer()))).toBe(false)
208 expect(isAsyncFunction({})).toBe(false)
209 expect(isAsyncFunction({ a: 1 })).toBe(false)
210 expect(isAsyncFunction(() => {})).toBe(false)
211 expect(isAsyncFunction(function () {})).toBe(false)
212 expect(isAsyncFunction(function named () {})).toBe(false)
213 expect(isAsyncFunction(async () => {})).toBe(true)
214 expect(isAsyncFunction(async function () {})).toBe(true)
215 expect(isAsyncFunction(async function named () {})).toBe(true)
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
225 it('Verify min() behavior', () => {
226 expect(min()).toBe(Infinity)
227 expect(min(1, 2)).toBe(1)
228 expect(min(2, 1)).toBe(1)
229 expect(min(1, 1)).toBe(1)
230 })
231
232 it('Verify max() behavior', () => {
233 expect(max()).toBe(-Infinity)
234 expect(max(1, 2)).toBe(2)
235 expect(max(2, 1)).toBe(2)
236 expect(max(1, 1)).toBe(1)
237 })
238})