Tests: Verify that worker pool tasks usage are reset at worker choice strategy change
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index 6a4da63d8a479b28410464bf9073118b28444fe4..ac776174b31180669196fa5448da3f5e0786dff9 100644 (file)
@@ -4,22 +4,23 @@ const {
   FixedThreadPool,
   WorkerChoiceStrategies
 } = require('../../../lib/index')
-const numberOfWorkers = 1
-const workerNotFoundInTasksUsageMapError = new Error(
-  'Worker could not be found in worker tasks usage map'
-)
-class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool {
-  removeAllWorker () {
-    this.workersTasksUsage.clear()
+
+describe('Abstract pool test suite', () => {
+  const numberOfWorkers = 1
+  const workerNotFoundInTasksUsageMapError = new Error(
+    'Worker could not be found in worker tasks usage map'
+  )
+  class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool {
+    removeAllWorker () {
+      this.workersTasksUsage.clear()
+    }
   }
-}
-class StubPoolWithIsMainMethod extends FixedThreadPool {
-  isMain () {
-    return false
+  class StubPoolWithIsMainMethod extends FixedThreadPool {
+    isMain () {
+      return false
+    }
   }
-}
 
-describe('Abstract pool test suite', () => {
   it('Simulate pool creation from a non main thread/process', () => {
     expect(
       () =>
@@ -211,7 +212,7 @@ describe('Abstract pool test suite', () => {
     )
     const promises = []
     for (let i = 0; i < numberOfWorkers * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     for (const tasksUsage of pool.workersTasksUsage.values()) {
       expect(tasksUsage).toBeDefined()
@@ -231,6 +232,34 @@ describe('Abstract pool test suite', () => {
     pool.destroy()
   })
 
+  it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => {
+    const pool = new FixedThreadPool(
+      numberOfWorkers,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    const promises = []
+    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)
+    }
+    pool.destroy()
+  })
+
   it("Verify that pool event emitter 'busy' event can register a callback", async () => {
     const pool = new FixedThreadPool(
       numberOfWorkers,
@@ -240,7 +269,7 @@ describe('Abstract pool test suite', () => {
     let poolBusy = 0
     pool.emitter.on('busy', () => poolBusy++)
     for (let i = 0; i < numberOfWorkers * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     await Promise.all(promises)
     // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.