| 1 | import { randomInt } from 'node:crypto' |
| 2 | import os from 'node:os' |
| 3 | |
| 4 | import { expect } from 'expect' |
| 5 | |
| 6 | import { KillBehaviors } from '../lib/index.cjs' |
| 7 | import { |
| 8 | availableParallelism, |
| 9 | average, |
| 10 | DEFAULT_TASK_NAME, |
| 11 | EMPTY_FUNCTION, |
| 12 | exponentialDelay, |
| 13 | isAsyncFunction, |
| 14 | isKillBehavior, |
| 15 | isPlainObject, |
| 16 | max, |
| 17 | median, |
| 18 | min, |
| 19 | // once, |
| 20 | round, |
| 21 | secureRandom, |
| 22 | sleep |
| 23 | } from '../lib/utils.cjs' |
| 24 | |
| 25 | describe('Utils test suite', () => { |
| 26 | it('Verify DEFAULT_TASK_NAME value', () => { |
| 27 | expect(DEFAULT_TASK_NAME).toBe('default') |
| 28 | }) |
| 29 | |
| 30 | it('Verify EMPTY_FUNCTION value', () => { |
| 31 | expect(EMPTY_FUNCTION).toStrictEqual(expect.any(Function)) |
| 32 | }) |
| 33 | |
| 34 | it('Verify availableParallelism() behavior', () => { |
| 35 | const parallelism = availableParallelism() |
| 36 | expect(typeof parallelism === 'number').toBe(true) |
| 37 | expect(Number.isSafeInteger(parallelism)).toBe(true) |
| 38 | let expectedParallelism = 1 |
| 39 | try { |
| 40 | // eslint-disable-next-line n/no-unsupported-features/node-builtins |
| 41 | expectedParallelism = os.availableParallelism() |
| 42 | } catch { |
| 43 | expectedParallelism = os.cpus().length |
| 44 | } |
| 45 | expect(parallelism).toBe(expectedParallelism) |
| 46 | }) |
| 47 | |
| 48 | it('Verify sleep() behavior', async () => { |
| 49 | const start = performance.now() |
| 50 | const sleepMs = 1000 |
| 51 | await sleep(sleepMs) |
| 52 | const elapsed = performance.now() - start |
| 53 | expect(elapsed).toBeGreaterThanOrEqual(sleepMs - 1) |
| 54 | }) |
| 55 | |
| 56 | it('Verify exponentialDelay() behavior', () => { |
| 57 | const delay = exponentialDelay(randomInt(1000)) |
| 58 | expect(typeof delay === 'number').toBe(true) |
| 59 | expect(delay).toBeGreaterThanOrEqual(Number.MIN_VALUE) |
| 60 | expect(delay).toBeLessThanOrEqual(Number.MAX_VALUE) |
| 61 | }) |
| 62 | |
| 63 | it('Verify average() computation', () => { |
| 64 | expect(average([])).toBe(0) |
| 65 | expect(average([0.08])).toBe(0.08) |
| 66 | expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe( |
| 67 | 3.1642857142857146 |
| 68 | ) |
| 69 | expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe( |
| 70 | 2.8533333333333335 |
| 71 | ) |
| 72 | }) |
| 73 | |
| 74 | it('Verify median() computation', () => { |
| 75 | expect(median([])).toBe(0) |
| 76 | expect(median([0.08])).toBe(0.08) |
| 77 | expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05) |
| 78 | expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535) |
| 79 | }) |
| 80 | |
| 81 | it('Verify round() behavior', () => { |
| 82 | expect(round(0)).toBe(0) |
| 83 | expect(round(0.5, 0)).toBe(1) |
| 84 | expect(round(0.5)).toBe(0.5) |
| 85 | expect(round(-0.5, 0)).toBe(-1) |
| 86 | expect(round(-0.5)).toBe(-0.5) |
| 87 | expect(round(1.005)).toBe(1.01) |
| 88 | expect(round(2.175)).toBe(2.18) |
| 89 | expect(round(5.015)).toBe(5.02) |
| 90 | expect(round(-1.005)).toBe(-1.01) |
| 91 | expect(round(-2.175)).toBe(-2.18) |
| 92 | expect(round(-5.015)).toBe(-5.02) |
| 93 | }) |
| 94 | |
| 95 | it('Verify isPlainObject() behavior', () => { |
| 96 | expect(isPlainObject(null)).toBe(false) |
| 97 | expect(isPlainObject(undefined)).toBe(false) |
| 98 | expect(isPlainObject(true)).toBe(false) |
| 99 | expect(isPlainObject(false)).toBe(false) |
| 100 | expect(isPlainObject(0)).toBe(false) |
| 101 | expect(isPlainObject('')).toBe(false) |
| 102 | expect(isPlainObject([])).toBe(false) |
| 103 | expect(isPlainObject(() => {})).toBe(false) |
| 104 | expect(isPlainObject(new Date())).toBe(false) |
| 105 | expect(isPlainObject(new RegExp())).toBe(false) |
| 106 | expect(isPlainObject(new Error())).toBe(false) |
| 107 | expect(isPlainObject(new Map())).toBe(false) |
| 108 | expect(isPlainObject(new Set())).toBe(false) |
| 109 | expect(isPlainObject(new WeakMap())).toBe(false) |
| 110 | expect(isPlainObject(new WeakSet())).toBe(false) |
| 111 | expect(isPlainObject(new Int8Array())).toBe(false) |
| 112 | expect(isPlainObject(new Uint8Array())).toBe(false) |
| 113 | expect(isPlainObject(new Uint8ClampedArray())).toBe(false) |
| 114 | expect(isPlainObject(new Int16Array())).toBe(false) |
| 115 | expect(isPlainObject(new Uint16Array())).toBe(false) |
| 116 | expect(isPlainObject(new Int32Array())).toBe(false) |
| 117 | expect(isPlainObject(new Uint32Array())).toBe(false) |
| 118 | expect(isPlainObject(new Float32Array())).toBe(false) |
| 119 | expect(isPlainObject(new Float64Array())).toBe(false) |
| 120 | expect(isPlainObject(new BigInt64Array())).toBe(false) |
| 121 | expect(isPlainObject(new BigUint64Array())).toBe(false) |
| 122 | expect(isPlainObject(new Promise(() => {}))).toBe(false) |
| 123 | expect(isPlainObject(new WeakRef({}))).toBe(false) |
| 124 | expect(isPlainObject(new FinalizationRegistry(() => {}))).toBe(false) |
| 125 | expect(isPlainObject(new ArrayBuffer())).toBe(false) |
| 126 | expect(isPlainObject(new SharedArrayBuffer())).toBe(false) |
| 127 | expect(isPlainObject(new DataView(new ArrayBuffer()))).toBe(false) |
| 128 | expect(isPlainObject({})).toBe(true) |
| 129 | expect(isPlainObject({ a: 1 })).toBe(true) |
| 130 | }) |
| 131 | |
| 132 | it('Verify isKillBehavior() behavior', () => { |
| 133 | expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.SOFT)).toBe(true) |
| 134 | expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.HARD)).toBe(false) |
| 135 | expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.HARD)).toBe(true) |
| 136 | expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.SOFT)).toBe(false) |
| 137 | expect(isKillBehavior(KillBehaviors.SOFT)).toBe(false) |
| 138 | expect(isKillBehavior(KillBehaviors.HARD)).toBe(false) |
| 139 | expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false) |
| 140 | expect(isKillBehavior(KillBehaviors.HARD, undefined)).toBe(false) |
| 141 | expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false) |
| 142 | }) |
| 143 | |
| 144 | it('Verify isAsyncFunction() behavior', () => { |
| 145 | expect(isAsyncFunction(null)).toBe(false) |
| 146 | expect(isAsyncFunction(undefined)).toBe(false) |
| 147 | expect(isAsyncFunction(true)).toBe(false) |
| 148 | expect(isAsyncFunction(false)).toBe(false) |
| 149 | expect(isAsyncFunction(0)).toBe(false) |
| 150 | expect(isAsyncFunction('')).toBe(false) |
| 151 | expect(isAsyncFunction([])).toBe(false) |
| 152 | expect(isAsyncFunction(new Date())).toBe(false) |
| 153 | // eslint-disable-next-line prefer-regex-literals |
| 154 | expect(isAsyncFunction(/[a-z]/i)).toBe(false) |
| 155 | expect(isAsyncFunction(new Error())).toBe(false) |
| 156 | expect(isAsyncFunction(new Map())).toBe(false) |
| 157 | expect(isAsyncFunction(new Set())).toBe(false) |
| 158 | expect(isAsyncFunction(new WeakMap())).toBe(false) |
| 159 | expect(isAsyncFunction(new WeakSet())).toBe(false) |
| 160 | expect(isAsyncFunction(new Int8Array())).toBe(false) |
| 161 | expect(isAsyncFunction(new Uint8Array())).toBe(false) |
| 162 | expect(isAsyncFunction(new Uint8ClampedArray())).toBe(false) |
| 163 | expect(isAsyncFunction(new Int16Array())).toBe(false) |
| 164 | expect(isAsyncFunction(new Uint16Array())).toBe(false) |
| 165 | expect(isAsyncFunction(new Int32Array())).toBe(false) |
| 166 | expect(isAsyncFunction(new Uint32Array())).toBe(false) |
| 167 | expect(isAsyncFunction(new Float32Array())).toBe(false) |
| 168 | expect(isAsyncFunction(new Float64Array())).toBe(false) |
| 169 | expect(isAsyncFunction(new BigInt64Array())).toBe(false) |
| 170 | expect(isAsyncFunction(new BigUint64Array())).toBe(false) |
| 171 | expect(isAsyncFunction(new Promise(() => {}))).toBe(false) |
| 172 | expect(isAsyncFunction(new WeakRef({}))).toBe(false) |
| 173 | expect(isAsyncFunction(new FinalizationRegistry(() => {}))).toBe(false) |
| 174 | expect(isAsyncFunction(new ArrayBuffer(16))).toBe(false) |
| 175 | expect(isAsyncFunction(new SharedArrayBuffer(16))).toBe(false) |
| 176 | expect(isAsyncFunction(new DataView(new ArrayBuffer(16)))).toBe(false) |
| 177 | expect(isAsyncFunction({})).toBe(false) |
| 178 | expect(isAsyncFunction({ a: 1 })).toBe(false) |
| 179 | expect(isAsyncFunction(() => {})).toBe(false) |
| 180 | expect(isAsyncFunction(function () {})).toBe(false) |
| 181 | expect(isAsyncFunction(function named () {})).toBe(false) |
| 182 | expect(isAsyncFunction(async () => {})).toBe(true) |
| 183 | expect(isAsyncFunction(async function () {})).toBe(true) |
| 184 | expect(isAsyncFunction(async function named () {})).toBe(true) |
| 185 | class TestClass { |
| 186 | testSync () {} |
| 187 | async testAsync () {} |
| 188 | testArrowSync = () => {} |
| 189 | testArrowAsync = async () => {} |
| 190 | static testStaticSync () {} |
| 191 | static async testStaticAsync () {} |
| 192 | } |
| 193 | const testClass = new TestClass() |
| 194 | expect(isAsyncFunction(testClass.testSync)).toBe(false) |
| 195 | expect(isAsyncFunction(testClass.testAsync)).toBe(true) |
| 196 | expect(isAsyncFunction(testClass.testArrowSync)).toBe(false) |
| 197 | expect(isAsyncFunction(testClass.testArrowAsync)).toBe(true) |
| 198 | expect(isAsyncFunction(TestClass.testStaticSync)).toBe(false) |
| 199 | expect(isAsyncFunction(TestClass.testStaticAsync)).toBe(true) |
| 200 | }) |
| 201 | |
| 202 | it('Verify secureRandom() behavior', () => { |
| 203 | const randomNumber = secureRandom() |
| 204 | expect(typeof randomNumber === 'number').toBe(true) |
| 205 | expect(randomNumber).toBeGreaterThanOrEqual(0) |
| 206 | expect(randomNumber).toBeLessThan(1) |
| 207 | }) |
| 208 | |
| 209 | it('Verify min() behavior', () => { |
| 210 | expect(min()).toBe(Number.POSITIVE_INFINITY) |
| 211 | expect(min(1, 2)).toBe(1) |
| 212 | expect(min(2, 1)).toBe(1) |
| 213 | expect(min(1, 1)).toBe(1) |
| 214 | }) |
| 215 | |
| 216 | it('Verify max() behavior', () => { |
| 217 | expect(max()).toBe(Number.NEGATIVE_INFINITY) |
| 218 | expect(max(1, 2)).toBe(2) |
| 219 | expect(max(2, 1)).toBe(2) |
| 220 | expect(max(1, 1)).toBe(1) |
| 221 | }) |
| 222 | |
| 223 | // it('Verify once() behavior', () => { |
| 224 | // let called = 0 |
| 225 | // const fn = () => ++called |
| 226 | // const onceFn = once(fn, this) |
| 227 | // const result1 = onceFn() |
| 228 | // expect(called).toBe(1) |
| 229 | // expect(result1).toBe(1) |
| 230 | // const result2 = onceFn() |
| 231 | // expect(called).toBe(1) |
| 232 | // expect(result2).toBe(1) |
| 233 | // const result3 = onceFn() |
| 234 | // expect(called).toBe(1) |
| 235 | // expect(result3).toBe(1) |
| 236 | // }) |
| 237 | }) |