test: improve availableParallelism() test
[poolifier.git] / tests / utils.test.js
index c08efbc1981a7acd6084398c0208d9b5d4c6c1fd..f5a5366de86bbd212f456d5a142ec9d9dd1bd5aa 100644 (file)
@@ -1,10 +1,42 @@
 const { expect } = require('expect')
-const { isPlainObject, median } = require('../lib/utils')
+const { CircularArray } = require('../lib/circular-array')
+const {
+  availableParallelism,
+  isAsyncFunction,
+  isKillBehavior,
+  isPlainObject,
+  median,
+  round,
+  updateMeasurementStatistics
+} = require('../lib/utils')
+const { KillBehaviors } = require('../lib/worker/worker-options')
 
 describe('Utils test suite', () => {
-  it('Verify median computation', () => {
-    const array = [0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03]
-    expect(median(array)).toBe(3.05)
+  it('Verify availableParallelism() behavior', () => {
+    expect(typeof availableParallelism() === 'number').toBe(true)
+    expect(availableParallelism()).toBeGreaterThan(0)
+    expect(Number.isSafeInteger(availableParallelism())).toBe(true)
+  })
+
+  it('Verify median() computation', () => {
+    expect(median([])).toBe(0)
+    expect(median([0.08])).toBe(0.08)
+    expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05)
+    expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
+  })
+
+  it('Verify round() behavior', () => {
+    expect(round(0)).toBe(0)
+    expect(round(0.5, 0)).toBe(1)
+    expect(round(0.5)).toBe(0.5)
+    expect(round(-0.5, 0)).toBe(-1)
+    expect(round(-0.5)).toBe(-0.5)
+    expect(round(1.005)).toBe(1.01)
+    expect(round(2.175)).toBe(2.18)
+    expect(round(5.015)).toBe(5.02)
+    expect(round(-1.005)).toBe(-1.01)
+    expect(round(-2.175)).toBe(-2.18)
+    expect(round(-5.015)).toBe(-5.02)
   })
 
   it('Verify isPlainObject() behavior', () => {
@@ -43,4 +75,101 @@ describe('Utils test suite', () => {
     expect(isPlainObject({})).toBe(true)
     expect(isPlainObject({ a: 1 })).toBe(true)
   })
+
+  it('Verify isKillBehavior() behavior', () => {
+    expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.SOFT)).toBe(true)
+    expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.HARD)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.HARD)).toBe(true)
+    expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.SOFT)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.SOFT)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.HARD)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.HARD, undefined)).toBe(false)
+    expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
+  })
+
+  it('Verify isAsyncFunction() behavior', () => {
+    expect(isAsyncFunction(null)).toBe(false)
+    expect(isAsyncFunction(undefined)).toBe(false)
+    expect(isAsyncFunction(true)).toBe(false)
+    expect(isAsyncFunction(false)).toBe(false)
+    expect(isAsyncFunction(0)).toBe(false)
+    expect(isAsyncFunction('')).toBe(false)
+    expect(isAsyncFunction([])).toBe(false)
+    expect(isAsyncFunction(new Date())).toBe(false)
+    expect(isAsyncFunction(new RegExp())).toBe(false)
+    expect(isAsyncFunction(new Error())).toBe(false)
+    expect(isAsyncFunction(new Map())).toBe(false)
+    expect(isAsyncFunction(new Set())).toBe(false)
+    expect(isAsyncFunction(new WeakMap())).toBe(false)
+    expect(isAsyncFunction(new WeakSet())).toBe(false)
+    expect(isAsyncFunction(new Int8Array())).toBe(false)
+    expect(isAsyncFunction(new Uint8Array())).toBe(false)
+    expect(isAsyncFunction(new Uint8ClampedArray())).toBe(false)
+    expect(isAsyncFunction(new Int16Array())).toBe(false)
+    expect(isAsyncFunction(new Uint16Array())).toBe(false)
+    expect(isAsyncFunction(new Int32Array())).toBe(false)
+    expect(isAsyncFunction(new Uint32Array())).toBe(false)
+    expect(isAsyncFunction(new Float32Array())).toBe(false)
+    expect(isAsyncFunction(new Float64Array())).toBe(false)
+    expect(isAsyncFunction(new BigInt64Array())).toBe(false)
+    expect(isAsyncFunction(new BigUint64Array())).toBe(false)
+    expect(isAsyncFunction(new Promise(() => {}))).toBe(false)
+    expect(isAsyncFunction(new WeakRef({}))).toBe(false)
+    expect(isAsyncFunction(new FinalizationRegistry(() => {}))).toBe(false)
+    expect(isAsyncFunction(new ArrayBuffer())).toBe(false)
+    expect(isAsyncFunction(new SharedArrayBuffer())).toBe(false)
+    expect(isAsyncFunction(new DataView(new ArrayBuffer()))).toBe(false)
+    expect(isAsyncFunction({})).toBe(false)
+    expect(isAsyncFunction({ a: 1 })).toBe(false)
+    expect(isAsyncFunction(() => {})).toBe(false)
+    expect(isAsyncFunction(function () {})).toBe(false)
+    expect(isAsyncFunction(function named () {})).toBe(false)
+    expect(isAsyncFunction(async () => {})).toBe(true)
+    expect(isAsyncFunction(async function () {})).toBe(true)
+    expect(isAsyncFunction(async function named () {})).toBe(true)
+  })
+
+  it('Verify updateMeasurementStatistics() behavior', () => {
+    const measurementStatistics = {
+      history: new CircularArray()
+    }
+    updateMeasurementStatistics(
+      measurementStatistics,
+      { aggregate: true, average: false, median: false },
+      0.01,
+      1
+    )
+    expect(measurementStatistics).toStrictEqual({
+      aggregate: 0.01,
+      maximum: 0.01,
+      minimum: 0.01,
+      history: expect.any(CircularArray)
+    })
+    updateMeasurementStatistics(
+      measurementStatistics,
+      { aggregate: true, average: false, median: false },
+      0.02,
+      2
+    )
+    expect(measurementStatistics).toStrictEqual({
+      aggregate: 0.03,
+      maximum: 0.02,
+      minimum: 0.01,
+      history: expect.any(CircularArray)
+    })
+    updateMeasurementStatistics(
+      measurementStatistics,
+      { aggregate: true, average: true, median: false },
+      0.001,
+      3
+    )
+    expect(measurementStatistics).toStrictEqual({
+      aggregate: 0.031,
+      maximum: 0.02,
+      minimum: 0.001,
+      average: 0.010333333333333333,
+      history: expect.any(CircularArray)
+    })
+  })
 })