build: run CI on merge queue
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index ac776174b31180669196fa5448da3f5e0786dff9..80ebdb9e7e5a04b07f71f7878ed46318cbcd4fdb 100644 (file)
@@ -1,6 +1,7 @@
 const { expect } = require('expect')
 const {
   FixedClusterPool,
+  DynamicThreadPool,
   FixedThreadPool,
   WorkerChoiceStrategies
 } = require('../../../lib/index')
@@ -8,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
     }
@@ -24,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',
           {
@@ -59,7 +62,9 @@ describe('Abstract pool test suite', () => {
       () =>
         new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
     ).toThrowError(
-      new Error('Cannot instantiate a pool with a negative number of workers')
+      new RangeError(
+        'Cannot instantiate a pool with a negative number of workers'
+      )
     )
   })
 
@@ -68,13 +73,13 @@ describe('Abstract pool test suite', () => {
       () =>
         new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
     ).toThrowError(
-      new Error(
+      new TypeError(
         'Cannot instantiate a pool with a non integer number of workers'
       )
     )
   })
 
-  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 +93,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,11 +116,11 @@ 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', () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+  it('Simulate worker not found during increaseWorkerRunningTasks', async () => {
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js'
     )
@@ -124,11 +129,11 @@ describe('Abstract pool test suite', () => {
     expect(() => pool.increaseWorkerRunningTasks()).toThrowError(
       workerNotFoundInTasksUsageMapError
     )
-    pool.destroy()
+    await pool.destroy()
   })
 
-  it('Simulate worker not found during decreaseWorkerRunningTasks', () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+  it('Simulate worker not found during decreaseWorkerRunningTasks', async () => {
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
       {
@@ -140,11 +145,11 @@ describe('Abstract pool test suite', () => {
     expect(() => pool.decreaseWorkerRunningTasks()).toThrowError(
       workerNotFoundInTasksUsageMapError
     )
-    pool.destroy()
+    await pool.destroy()
   })
 
-  it('Simulate worker not found during stepWorkerRunTasks', () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+  it('Simulate worker not found during stepWorkerRunTasks', async () => {
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
       {
@@ -156,11 +161,11 @@ 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', () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+  it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', async () => {
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
       {
@@ -170,11 +175,11 @@ 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', () => {
-    const pool = new StubPoolWithWorkerTasksUsageMapClear(
+  it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', async () => {
+    const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
       {
@@ -187,10 +192,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 +207,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 +234,12 @@ 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(
+    const pool = new DynamicThreadPool(
+      numberOfWorkers,
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js'
     )
@@ -257,7 +263,7 @@ describe('Abstract pool test suite', () => {
       expect(tasksUsage.runTime).toBe(0)
       expect(tasksUsage.avgRunTime).toBe(0)
     }
-    pool.destroy()
+    await pool.destroy()
   })
 
   it("Verify that pool event emitter 'busy' event can register a callback", async () => {
@@ -275,6 +281,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()
   })
 })