build(deps-dev): apply updates
[poolifier.git] / tests / utils.test.mjs
index 5e089e291f07877506ff12a3373ca407ee2b5c51..25b4e5ae0cfd21cc84e5fb523748e84e44a95e0b 100644 (file)
@@ -1,19 +1,15 @@
-import { Worker } from 'node:worker_threads'
-import cluster from 'node:cluster'
-import os from 'node:os'
 import { randomInt } from 'node:crypto'
+import os from 'node:os'
+
 import { expect } from 'expect'
+
+import { KillBehaviors } from '../lib/index.cjs'
 import {
-  DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-  DEFAULT_TASK_NAME,
-  EMPTY_FUNCTION,
   availableParallelism,
   average,
-  buildWorkerChoiceStrategyOptions,
+  DEFAULT_TASK_NAME,
+  EMPTY_FUNCTION,
   exponentialDelay,
-  getWorkerChoiceStrategyRetries,
-  getWorkerId,
-  getWorkerType,
   isAsyncFunction,
   isKillBehavior,
   isPlainObject,
@@ -23,14 +19,8 @@ import {
   // once,
   round,
   secureRandom,
-  sleep
+  sleep,
 } from '../lib/utils.cjs'
-import {
-  FixedClusterPool,
-  FixedThreadPool,
-  KillBehaviors,
-  WorkerTypes
-} from '../lib/index.cjs'
 
 describe('Utils test suite', () => {
   it('Verify DEFAULT_TASK_NAME value', () => {
@@ -41,20 +31,13 @@ describe('Utils test suite', () => {
     expect(EMPTY_FUNCTION).toStrictEqual(expect.any(Function))
   })
 
-  it('Verify DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS values', () => {
-    expect(DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS).toStrictEqual({
-      aggregate: false,
-      average: false,
-      median: false
-    })
-  })
-
   it('Verify availableParallelism() behavior', () => {
     const parallelism = availableParallelism()
     expect(typeof parallelism === 'number').toBe(true)
     expect(Number.isSafeInteger(parallelism)).toBe(true)
     let expectedParallelism = 1
     try {
+      // eslint-disable-next-line n/no-unsupported-features/node-builtins
       expectedParallelism = os.availableParallelism()
     } catch {
       expectedParallelism = os.cpus().length
@@ -62,22 +45,6 @@ describe('Utils test suite', () => {
     expect(parallelism).toBe(expectedParallelism)
   })
 
-  it('Verify getWorkerType() behavior', () => {
-    expect(
-      getWorkerType(new Worker('./tests/worker-files/thread/testWorker.mjs'))
-    ).toBe(WorkerTypes.thread)
-    expect(getWorkerType(cluster.fork())).toBe(WorkerTypes.cluster)
-  })
-
-  it('Verify getWorkerId() behavior', () => {
-    const threadWorker = new Worker(
-      './tests/worker-files/thread/testWorker.mjs'
-    )
-    const clusterWorker = cluster.fork()
-    expect(getWorkerId(threadWorker)).toBe(threadWorker.threadId)
-    expect(getWorkerId(clusterWorker)).toBe(clusterWorker.id)
-  })
-
   it('Verify sleep() behavior', async () => {
     const start = performance.now()
     const sleepMs = 1000
@@ -183,7 +150,7 @@ describe('Utils test suite', () => {
     expect(isAsyncFunction('')).toBe(false)
     expect(isAsyncFunction([])).toBe(false)
     expect(isAsyncFunction(new Date())).toBe(false)
-    expect(isAsyncFunction(new RegExp())).toBe(false)
+    expect(isAsyncFunction(/[a-z]/i)).toBe(false)
     expect(isAsyncFunction(new Error())).toBe(false)
     expect(isAsyncFunction(new Map())).toBe(false)
     expect(isAsyncFunction(new Set())).toBe(false)
@@ -203,9 +170,9 @@ describe('Utils test suite', () => {
     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(new ArrayBuffer(16))).toBe(false)
+    expect(isAsyncFunction(new SharedArrayBuffer(16))).toBe(false)
+    expect(isAsyncFunction(new DataView(new ArrayBuffer(16)))).toBe(false)
     expect(isAsyncFunction({})).toBe(false)
     expect(isAsyncFunction({ a: 1 })).toBe(false)
     expect(isAsyncFunction(() => {})).toBe(false)
@@ -214,6 +181,21 @@ describe('Utils test suite', () => {
     expect(isAsyncFunction(async () => {})).toBe(true)
     expect(isAsyncFunction(async function () {})).toBe(true)
     expect(isAsyncFunction(async function named () {})).toBe(true)
+    class TestClass {
+      testSync () {}
+      async testAsync () {}
+      testArrowSync = () => {}
+      testArrowAsync = async () => {}
+      static testStaticSync () {}
+      static async testStaticAsync () {}
+    }
+    const testClass = new TestClass()
+    expect(isAsyncFunction(testClass.testSync)).toBe(false)
+    expect(isAsyncFunction(testClass.testAsync)).toBe(true)
+    expect(isAsyncFunction(testClass.testArrowSync)).toBe(false)
+    expect(isAsyncFunction(testClass.testArrowAsync)).toBe(true)
+    expect(isAsyncFunction(TestClass.testStaticSync)).toBe(false)
+    expect(isAsyncFunction(TestClass.testStaticAsync)).toBe(true)
   })
 
   it('Verify secureRandom() behavior', () => {
@@ -224,48 +206,20 @@ describe('Utils test suite', () => {
   })
 
   it('Verify min() behavior', () => {
-    expect(min()).toBe(Infinity)
+    expect(min()).toBe(Number.POSITIVE_INFINITY)
     expect(min(1, 2)).toBe(1)
     expect(min(2, 1)).toBe(1)
     expect(min(1, 1)).toBe(1)
   })
 
   it('Verify max() behavior', () => {
-    expect(max()).toBe(-Infinity)
+    expect(max()).toBe(Number.NEGATIVE_INFINITY)
     expect(max(1, 2)).toBe(2)
     expect(max(2, 1)).toBe(2)
     expect(max(1, 1)).toBe(1)
   })
 
-  it('Verify getWorkerChoiceStrategyRetries() behavior', async () => {
-    const numberOfThreads = 4
-    const pool = new FixedThreadPool(
-      numberOfThreads,
-      './tests/worker-files/thread/testWorker.mjs'
-    )
-    expect(getWorkerChoiceStrategyRetries(pool)).toBe(pool.info.maxSize * 2)
-    await pool.destroy()
-  })
-
-  it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => {
-    const numberOfWorkers = 4
-    const pool = new FixedClusterPool(
-      numberOfWorkers,
-      './tests/worker-files/cluster/testWorker.cjs'
-    )
-    expect(buildWorkerChoiceStrategyOptions(pool)).toStrictEqual({
-      runTime: { median: false },
-      waitTime: { median: false },
-      elu: { median: false },
-      weights: expect.objectContaining({
-        0: expect.any(Number),
-        [pool.info.maxSize - 1]: expect.any(Number)
-      })
-    })
-    await pool.destroy()
-  })
-
-  // it('Verify once()', () => {
+  // it('Verify once() behavior', () => {
   //   let called = 0
   //   const fn = () => ++called
   //   const onceFn = once(fn, this)