build(deps): bump poolifier
[poolifier.git] / tests / utils.test.mjs
index 0ae80de24bfa9c2badff5f5127a8e3137c50e6a9..5f54f6fc8ea4e0ebe7d7f6ebc27f0efb8a5ea218 100644 (file)
@@ -6,11 +6,12 @@ import { expect } from 'expect'
 import {
   DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
   DEFAULT_TASK_NAME,
-  DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
   EMPTY_FUNCTION,
   availableParallelism,
   average,
+  buildWorkerChoiceStrategyOptions,
   exponentialDelay,
+  getWorkerChoiceStrategyRetries,
   getWorkerId,
   getWorkerType,
   isAsyncFunction,
@@ -23,8 +24,13 @@ import {
   round,
   secureRandom,
   sleep
-} from '../lib/utils.js'
-import { KillBehaviors, WorkerTypes } from '../lib/index.js'
+} from '../lib/utils.cjs'
+import {
+  FixedClusterPool,
+  FixedThreadPool,
+  KillBehaviors,
+  WorkerTypes
+} from '../lib/index.cjs'
 
 describe('Utils test suite', () => {
   it('Verify DEFAULT_TASK_NAME value', () => {
@@ -35,15 +41,6 @@ describe('Utils test suite', () => {
     expect(EMPTY_FUNCTION).toStrictEqual(expect.any(Function))
   })
 
-  it('Verify DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS values', () => {
-    expect(DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS).toStrictEqual({
-      retries: 6,
-      runTime: { median: false },
-      waitTime: { median: false },
-      elu: { median: false }
-    })
-  })
-
   it('Verify DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS values', () => {
     expect(DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS).toStrictEqual({
       aggregate: false,
@@ -83,9 +80,10 @@ describe('Utils test suite', () => {
 
   it('Verify sleep() behavior', async () => {
     const start = performance.now()
-    await sleep(1000)
+    const sleepMs = 1000
+    await sleep(sleepMs)
     const elapsed = performance.now() - start
-    expect(elapsed).toBeGreaterThanOrEqual(1000)
+    expect(elapsed).toBeGreaterThanOrEqual(sleepMs - 1)
   })
 
   it('Verify exponentialDelay() behavior', () => {
@@ -239,6 +237,61 @@ describe('Utils test suite', () => {
     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)
+    const workerChoiceStrategyOptions = {
+      runTime: { median: true },
+      waitTime: { median: true },
+      elu: { median: true },
+      weights: {
+        0: 100,
+        1: 100
+      }
+    }
+    expect(
+      getWorkerChoiceStrategyRetries(pool, workerChoiceStrategyOptions)
+    ).toBe(
+      pool.info.maxSize +
+        Object.keys(workerChoiceStrategyOptions.weights).length
+    )
+    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)
+      })
+    })
+    const workerChoiceStrategyOptions = {
+      runTime: { median: true },
+      waitTime: { median: true },
+      elu: { median: true },
+      weights: {
+        0: 100,
+        1: 100
+      }
+    }
+    expect(
+      buildWorkerChoiceStrategyOptions(pool, workerChoiceStrategyOptions)
+    ).toStrictEqual(workerChoiceStrategyOptions)
+    await pool.destroy()
+  })
+
   // it('Verify once()', () => {
   //   let called = 0
   //   const fn = () => ++called