perf: use a single map to store pool workers and their related data
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
index d9a01684e9b5b54c85675d3eb9f7573df0b1ea0d..c55b971edb8a9ed3e47ccae1db4b0deff206b08b 100644 (file)
@@ -2,7 +2,8 @@ const { expect } = require('expect')
 const {
   WorkerChoiceStrategies,
   DynamicThreadPool,
-  FixedThreadPool
+  FixedThreadPool,
+  FixedClusterPool
 } = require('../../../lib/index')
 
 describe('Selection strategies test suite', () => {
@@ -41,7 +42,7 @@ describe('Selection strategies test suite', () => {
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     expect(
-      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
     ).toBe(0)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -125,15 +126,38 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
+    let pool = new FixedClusterPool(
+      max,
+      './tests/worker-files/cluster/testWorker.js'
+    )
+    let results = new Set()
+    for (let i = 0; i < max; i++) {
+      results.add(pool.chooseWorker().id)
+    }
+    expect(results.size).toBe(max)
+    await pool.destroy()
+    pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js')
+    results = new Set()
+    for (let i = 0; i < max; i++) {
+      results.add(pool.chooseWorker().threadId)
+    }
+    expect(results.size).toBe(max)
+    await pool.destroy()
+  })
+
   it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
     let pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
       { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
+    ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
-      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
     ).toBe(0)
     await pool.destroy()
     pool = new DynamicThreadPool(
@@ -142,10 +166,14 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js',
       { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .workerChoiceStrategy.nextWorkerId
+    ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .workerChoiceStrategy.nextWorkerIndex
+        .workerChoiceStrategy.nextWorkerId
     ).toBe(0)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -396,12 +424,7 @@ describe('Selection strategies test suite', () => {
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
     expect(
-      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .previousWorkerIndex
-    ).toBe(0)
-    expect(
-      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .currentWorkerIndex
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
     ).toBe(0)
     expect(
       pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
@@ -502,17 +525,19 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     expect(
-      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .workersTaskRunTime
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
     ).toBeUndefined()
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .previousWorkerIndex
-    ).toBe(0)
+        .defaultWorkerWeight
+    ).toBeUndefined()
     expect(
       pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .currentWorkerIndex
+        .workersTaskRunTime
+    ).toBeUndefined()
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
     ).toBe(0)
     expect(
       pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
@@ -535,16 +560,20 @@ describe('Selection strategies test suite', () => {
     )
     expect(
       pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .workerChoiceStrategy.workersTaskRunTime
+        .workerChoiceStrategy.currentWorkerId
     ).toBeUndefined()
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .workerChoiceStrategy.previousWorkerIndex
-    ).toBe(0)
+        .workerChoiceStrategy.defaultWorkerWeight
+    ).toBeUndefined()
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .workerChoiceStrategy.workersTaskRunTime
+    ).toBeUndefined()
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
-        .workerChoiceStrategy.currentWorkerIndex
+        .workerChoiceStrategy.currentWorkerId
     ).toBe(0)
     expect(
       pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()