fix: properly handle dynamic pool with zero minimum size
[poolifier.git] / tests / utils.test.mjs
index 4b4e1e136119c0249894ba03f31d6245201ca3e0..5e089e291f07877506ff12a3373ca407ee2b5c51 100644 (file)
@@ -9,8 +9,9 @@ import {
   EMPTY_FUNCTION,
   availableParallelism,
   average,
+  buildWorkerChoiceStrategyOptions,
   exponentialDelay,
-  getDefaultInternalWorkerChoiceStrategyOptions,
+  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,18 +41,6 @@ describe('Utils test suite', () => {
     expect(EMPTY_FUNCTION).toStrictEqual(expect.any(Function))
   })
 
-  it('Verify getDefaultInternalWorkerChoiceStrategyOptions() values', () => {
-    const poolMaxSize = 10
-    expect(
-      getDefaultInternalWorkerChoiceStrategyOptions(poolMaxSize)
-    ).toStrictEqual({
-      retries: poolMaxSize,
-      runTime: { median: false },
-      waitTime: { median: false },
-      elu: { median: false }
-    })
-  })
-
   it('Verify DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS values', () => {
     expect(DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS).toStrictEqual({
       aggregate: false,
@@ -243,6 +237,34 @@ 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)
+    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()', () => {
   //   let called = 0
   //   const fn = () => ++called