Refine attributes scope in dynamic pool code
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index e1c83b6fbfdf91caf6da8ffab5fd8532bd48da7b..273de9a038f0a02eb0e4d554bd717107e06394f3 100644 (file)
@@ -9,14 +9,16 @@ const {
 describe('Abstract pool test suite', () => {
   const numberOfWorkers = 1
   const workerNotFoundInTasksUsageMapError = new Error(
-    'Worker could not be found in worker tasks usage map'
+    'Worker could not be found in workers tasks usage map'
   )
-  class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool {
+  class StubPoolWithRemoveAllWorker extends FixedThreadPool {
     removeAllWorker () {
+      this.workers = []
       this.workersTasksUsage.clear()
+      this.promiseMap.clear()
     }
   }
-  class StubPoolWithIsMainMethod extends FixedThreadPool {
+  class StubPoolWithIsMain extends FixedThreadPool {
     isMain () {
       return false
     }
@@ -25,7 +27,7 @@ describe('Abstract pool test suite', () => {
   it('Simulate pool creation from a non main thread/process', () => {
     expect(
       () =>
-        new StubPoolWithIsMainMethod(
+        new StubPoolWithIsMain(
           numberOfWorkers,
           './tests/worker-files/thread/testWorker.js',
           {
@@ -116,7 +118,7 @@ describe('Abstract pool test suite', () => {
   })
 
   it('Simulate worker not found during increaseWorkerRunningTasks', async () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js'
     )
@@ -129,7 +131,7 @@ describe('Abstract pool test suite', () => {
   })
 
   it('Simulate worker not found during decreaseWorkerRunningTasks', async () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
       {
@@ -145,7 +147,7 @@ describe('Abstract pool test suite', () => {
   })
 
   it('Simulate worker not found during stepWorkerRunTasks', async () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
       {
@@ -161,7 +163,7 @@ describe('Abstract pool test suite', () => {
   })
 
   it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', async () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
       {
@@ -175,7 +177,7 @@ describe('Abstract pool test suite', () => {
   })
 
   it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', async () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
       {
@@ -234,37 +236,12 @@ describe('Abstract pool test suite', () => {
   })
 
   it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => {
-    let 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)
-    }
-    await pool.destroy()
-    pool = new DynamicThreadPool(
+    const pool = new DynamicThreadPool(
       numberOfWorkers,
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js'
     )
-    promises.length = 0
+    const promises = []
     for (let i = 0; i < numberOfWorkers * 2; i++) {
       promises.push(pool.execute())
     }