test: add least ELU tests
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
index a1eeaeaf8b4e977ea9e8c962a2d74b25c42da711..9f1d3a09f42f74f8742e320eec6d6cca2f26dbd0 100644 (file)
@@ -6,6 +6,7 @@ const {
   FixedClusterPool
 } = require('../../../lib')
 const { CircularArray } = require('../../../lib/circular-array')
+const TestUtils = require('../../test-utils')
 
 describe('Selection strategies test suite', () => {
   const min = 0
@@ -447,7 +448,7 @@ describe('Selection strategies test suite', () => {
       runTime: true,
       avgRunTime: false,
       medRunTime: false,
-      waitTime: false,
+      waitTime: true,
       avgWaitTime: false,
       medWaitTime: false,
       elu: false
@@ -465,7 +466,7 @@ describe('Selection strategies test suite', () => {
       runTime: true,
       avgRunTime: false,
       medRunTime: false,
-      waitTime: false,
+      waitTime: true,
       avgWaitTime: false,
       medWaitTime: false,
       elu: false
@@ -502,7 +503,7 @@ describe('Selection strategies test suite', () => {
           history: expect.any(CircularArray)
         },
         waitTime: {
-          aggregation: 0,
+          aggregation: expect.any(Number),
           average: 0,
           median: 0,
           history: expect.any(CircularArray)
@@ -516,6 +517,9 @@ describe('Selection strategies test suite', () => {
       expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThanOrEqual(
         0
       )
+      expect(
+        workerNode.workerUsage.waitTime.aggregation
+      ).toBeGreaterThanOrEqual(0)
     }
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -550,7 +554,7 @@ describe('Selection strategies test suite', () => {
           history: expect.any(CircularArray)
         },
         waitTime: {
-          aggregation: 0,
+          aggregation: expect.any(Number),
           average: 0,
           median: 0,
           history: expect.any(CircularArray)
@@ -562,6 +566,7 @@ describe('Selection strategies test suite', () => {
         max * maxMultiplier
       )
       expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0)
+      expect(workerNode.workerUsage.waitTime.aggregation).toBeGreaterThan(0)
     }
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -607,6 +612,63 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify LEAST_ELU strategy can be run in a fixed pool', async () => {
+    const pool = new FixedThreadPool(
+      max,
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_ELU }
+    )
+    // TODO: Create a better test to cover `LeastEluWorkerChoiceStrategy#choose`
+    const maxMultiplier = 2
+    for (let i = 0; i < max * maxMultiplier; i++) {
+      await pool.execute()
+      if (i !== max * maxMultiplier - 1) await TestUtils.sleep(500)
+    }
+    for (const workerNode of pool.workerNodes) {
+      const expectedWorkerUsage = {
+        tasks: {
+          executed: expect.any(Number),
+          executing: 0,
+          queued: 0,
+          failed: 0
+        },
+        runTime: {
+          aggregation: 0,
+          average: 0,
+          median: 0,
+          history: expect.any(CircularArray)
+        },
+        waitTime: {
+          aggregation: 0,
+          average: 0,
+          median: 0,
+          history: expect.any(CircularArray)
+        }
+      }
+      if (workerNode.workerUsage.elu === undefined) {
+        expect(workerNode.workerUsage).toStrictEqual({
+          ...expectedWorkerUsage,
+          elu: undefined
+        })
+      } else {
+        expect(workerNode.workerUsage).toStrictEqual({
+          ...expectedWorkerUsage,
+          elu: {
+            active: expect.any(Number),
+            idle: 0,
+            utilization: 1
+          }
+        })
+      }
+      expect(workerNode.workerUsage.tasks.executed).toBeGreaterThanOrEqual(0)
+      expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
+        max * maxMultiplier
+      )
+    }
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
+
   it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
     const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
     let pool = new FixedThreadPool(