Test: Verify that worker pool tasks usage are reset at worker choice strategy change...
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index ac776174b31180669196fa5448da3f5e0786dff9..e1c83b6fbfdf91caf6da8ffab5fd8532bd48da7b 100644 (file)
@@ -1,6 +1,7 @@
 const { expect } = require('expect')
 const {
   FixedClusterPool,
+  DynamicThreadPool,
   FixedThreadPool,
   WorkerChoiceStrategies
 } = require('../../../lib/index')
@@ -74,7 +75,7 @@ describe('Abstract pool test suite', () => {
     )
   })
 
-  it('Verify that pool options are checked', () => {
+  it('Verify that pool options are checked', async () => {
     let pool = new FixedThreadPool(
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js'
@@ -88,7 +89,7 @@ describe('Abstract pool test suite', () => {
     expect(pool.opts.errorHandler).toBeUndefined()
     expect(pool.opts.onlineHandler).toBeUndefined()
     expect(pool.opts.exitHandler).toBeUndefined()
-    pool.destroy()
+    await pool.destroy()
     const testHandler = () => console.log('test handler executed')
     pool = new FixedThreadPool(
       numberOfWorkers,
@@ -111,10 +112,10 @@ describe('Abstract pool test suite', () => {
     expect(pool.opts.errorHandler).toStrictEqual(testHandler)
     expect(pool.opts.onlineHandler).toStrictEqual(testHandler)
     expect(pool.opts.exitHandler).toStrictEqual(testHandler)
-    pool.destroy()
+    await pool.destroy()
   })
 
-  it('Simulate worker not found during increaseWorkerRunningTasks', () => {
+  it('Simulate worker not found during increaseWorkerRunningTasks', async () => {
     const pool = new StubPoolWithWorkerTasksUsageMapClear(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js'
@@ -124,10 +125,10 @@ describe('Abstract pool test suite', () => {
     expect(() => pool.increaseWorkerRunningTasks()).toThrowError(
       workerNotFoundInTasksUsageMapError
     )
-    pool.destroy()
+    await pool.destroy()
   })
 
-  it('Simulate worker not found during decreaseWorkerRunningTasks', () => {
+  it('Simulate worker not found during decreaseWorkerRunningTasks', async () => {
     const pool = new StubPoolWithWorkerTasksUsageMapClear(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
@@ -140,10 +141,10 @@ describe('Abstract pool test suite', () => {
     expect(() => pool.decreaseWorkerRunningTasks()).toThrowError(
       workerNotFoundInTasksUsageMapError
     )
-    pool.destroy()
+    await pool.destroy()
   })
 
-  it('Simulate worker not found during stepWorkerRunTasks', () => {
+  it('Simulate worker not found during stepWorkerRunTasks', async () => {
     const pool = new StubPoolWithWorkerTasksUsageMapClear(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
@@ -156,10 +157,10 @@ describe('Abstract pool test suite', () => {
     expect(() => pool.stepWorkerRunTasks()).toThrowError(
       workerNotFoundInTasksUsageMapError
     )
-    pool.destroy()
+    await pool.destroy()
   })
 
-  it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', () => {
+  it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', async () => {
     const pool = new StubPoolWithWorkerTasksUsageMapClear(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
@@ -170,10 +171,10 @@ describe('Abstract pool test suite', () => {
     // Simulate worker not found.
     pool.removeAllWorker()
     expect(() => pool.updateWorkerTasksRunTime()).not.toThrowError()
-    pool.destroy()
+    await pool.destroy()
   })
 
-  it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', () => {
+  it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', async () => {
     const pool = new StubPoolWithWorkerTasksUsageMapClear(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
@@ -187,10 +188,10 @@ describe('Abstract pool test suite', () => {
     expect(() => pool.updateWorkerTasksRunTime()).toThrowError(
       workerNotFoundInTasksUsageMapError
     )
-    pool.destroy()
+    await pool.destroy()
   })
 
-  it('Verify that worker pool tasks usage are initialized', () => {
+  it('Verify that worker pool tasks usage are initialized', async () => {
     const pool = new FixedClusterPool(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js'
@@ -202,7 +203,7 @@ describe('Abstract pool test suite', () => {
       expect(tasksUsage.runTime).toBe(0)
       expect(tasksUsage.avgRunTime).toBe(0)
     }
-    pool.destroy()
+    await pool.destroy()
   })
 
   it('Verify that worker pool tasks usage are computed', async () => {
@@ -229,11 +230,11 @@ describe('Abstract pool test suite', () => {
       expect(tasksUsage.runTime).toBeGreaterThanOrEqual(0)
       expect(tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0)
     }
-    pool.destroy()
+    await pool.destroy()
   })
 
   it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => {
-    const pool = new FixedThreadPool(
+    let pool = new FixedThreadPool(
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js'
     )
@@ -257,7 +258,33 @@ describe('Abstract pool test suite', () => {
       expect(tasksUsage.runTime).toBe(0)
       expect(tasksUsage.avgRunTime).toBe(0)
     }
-    pool.destroy()
+    await pool.destroy()
+    pool = new DynamicThreadPool(
+      numberOfWorkers,
+      numberOfWorkers,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    promises.length = 0
+    for (let i = 0; i < numberOfWorkers * 2; i++) {
+      promises.push(pool.execute())
+    }
+    await Promise.all(promises)
+    for (const tasksUsage of pool.workersTasksUsage.values()) {
+      expect(tasksUsage).toBeDefined()
+      expect(tasksUsage.run).toBe(numberOfWorkers * 2)
+      expect(tasksUsage.running).toBe(0)
+      expect(tasksUsage.runTime).toBeGreaterThanOrEqual(0)
+      expect(tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0)
+    }
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
+    for (const tasksUsage of pool.workersTasksUsage.values()) {
+      expect(tasksUsage).toBeDefined()
+      expect(tasksUsage.run).toBe(0)
+      expect(tasksUsage.running).toBe(0)
+      expect(tasksUsage.runTime).toBe(0)
+      expect(tasksUsage.avgRunTime).toBe(0)
+    }
+    await pool.destroy()
   })
 
   it("Verify that pool event emitter 'busy' event can register a callback", async () => {
@@ -275,6 +302,6 @@ describe('Abstract pool test suite', () => {
     // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
     // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
     expect(poolBusy).toBe(numberOfWorkers + 1)
-    pool.destroy()
+    await pool.destroy()
   })
 })