fix: fix continuous tasks stealing on idle start at worker node idling
[poolifier.git] / tests / pools / abstract-pool.test.mjs
index 041c62a2b1277c179d689dbe182d6b382fc55cef..04e15c09f40b437d44cb019f52c64e25e3801a21 100644 (file)
@@ -38,7 +38,16 @@ describe('Abstract pool test suite', () => {
     restore()
   })
 
-  it('Simulate pool creation from a non main thread/process', () => {
+  it('Verify that pool can be created and destroyed', async () => {
+    const pool = new FixedThreadPool(
+      numberOfWorkers,
+      './tests/worker-files/thread/testWorker.mjs'
+    )
+    expect(pool).toBeInstanceOf(FixedThreadPool)
+    await pool.destroy()
+  })
+
+  it('Verify that pool cannot be created from a non main thread/process', () => {
     expect(
       () =>
         new StubPoolWithIsMain(
@@ -48,7 +57,7 @@ describe('Abstract pool test suite', () => {
             errorHandler: e => console.error(e)
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new Error(
         'Cannot start a pool from a worker with the same type as the pool'
       )
@@ -60,30 +69,19 @@ describe('Abstract pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.mjs'
     )
-    expect(pool.starting).toBe(false)
     expect(pool.started).toBe(true)
+    expect(pool.starting).toBe(false)
+    expect(pool.destroying).toBe(false)
     await pool.destroy()
   })
 
   it('Verify that filePath is checked', () => {
-    const expectedError = new Error(
-      'Please specify a file with a worker implementation'
-    )
-    expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError(
-      expectedError
-    )
-    expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError(
-      expectedError
-    )
-    expect(() => new FixedThreadPool(numberOfWorkers, 0)).toThrowError(
-      expectedError
-    )
-    expect(() => new FixedThreadPool(numberOfWorkers, true)).toThrowError(
-      expectedError
+    expect(() => new FixedThreadPool(numberOfWorkers)).toThrow(
+      new Error("Cannot find the worker file 'undefined'")
     )
     expect(
       () => new FixedThreadPool(numberOfWorkers, './dummyWorker.ts')
-    ).toThrowError(new Error("Cannot find the worker file './dummyWorker.ts'"))
+    ).toThrow(new Error("Cannot find the worker file './dummyWorker.ts'"))
   })
 
   it('Verify that numberOfWorkers is checked', () => {
@@ -93,7 +91,7 @@ describe('Abstract pool test suite', () => {
           undefined,
           './tests/worker-files/thread/testWorker.mjs'
         )
-    ).toThrowError(
+    ).toThrow(
       new Error(
         'Cannot instantiate a pool without specifying the number of workers'
       )
@@ -104,7 +102,7 @@ describe('Abstract pool test suite', () => {
     expect(
       () =>
         new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
-    ).toThrowError(
+    ).toThrow(
       new RangeError(
         'Cannot instantiate a pool with a negative number of workers'
       )
@@ -115,7 +113,7 @@ describe('Abstract pool test suite', () => {
     expect(
       () =>
         new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.mjs')
-    ).toThrowError(
+    ).toThrow(
       new TypeError(
         'Cannot instantiate a pool with a non safe integer number of workers'
       )
@@ -130,7 +128,7 @@ describe('Abstract pool test suite', () => {
           undefined,
           './tests/worker-files/cluster/testWorker.js'
         )
-    ).toThrowError(
+    ).toThrow(
       new TypeError(
         'Cannot instantiate a dynamic pool without specifying the maximum pool size'
       )
@@ -142,7 +140,7 @@ describe('Abstract pool test suite', () => {
           1,
           './tests/worker-files/thread/testWorker.mjs'
         )
-    ).toThrowError(
+    ).toThrow(
       new TypeError(
         'Cannot instantiate a pool with a non safe integer number of workers'
       )
@@ -154,7 +152,7 @@ describe('Abstract pool test suite', () => {
           0.5,
           './tests/worker-files/cluster/testWorker.js'
         )
-    ).toThrowError(
+    ).toThrow(
       new TypeError(
         'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
       )
@@ -166,7 +164,7 @@ describe('Abstract pool test suite', () => {
           1,
           './tests/worker-files/thread/testWorker.mjs'
         )
-    ).toThrowError(
+    ).toThrow(
       new RangeError(
         'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
       )
@@ -178,7 +176,7 @@ describe('Abstract pool test suite', () => {
           0,
           './tests/worker-files/thread/testWorker.mjs'
         )
-    ).toThrowError(
+    ).toThrow(
       new RangeError(
         'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
       )
@@ -190,7 +188,7 @@ describe('Abstract pool test suite', () => {
           1,
           './tests/worker-files/cluster/testWorker.js'
         )
-    ).toThrowError(
+    ).toThrow(
       new RangeError(
         'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
       )
@@ -307,9 +305,7 @@ describe('Abstract pool test suite', () => {
             workerChoiceStrategy: 'invalidStrategy'
           }
         )
-    ).toThrowError(
-      new Error("Invalid worker choice strategy 'invalidStrategy'")
-    )
+    ).toThrow(new Error("Invalid worker choice strategy 'invalidStrategy'"))
     expect(
       () =>
         new FixedThreadPool(
@@ -321,7 +317,7 @@ describe('Abstract pool test suite', () => {
             }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new TypeError(
         'Invalid worker choice strategy options: retries must be an integer'
       )
@@ -337,7 +333,7 @@ describe('Abstract pool test suite', () => {
             }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new RangeError(
         "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero"
       )
@@ -351,7 +347,7 @@ describe('Abstract pool test suite', () => {
             workerChoiceStrategyOptions: { weights: {} }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new Error(
         'Invalid worker choice strategy options: must have a weight for each worker node'
       )
@@ -365,7 +361,7 @@ describe('Abstract pool test suite', () => {
             workerChoiceStrategyOptions: { measurement: 'invalidMeasurement' }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new Error(
         "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'"
       )
@@ -380,7 +376,7 @@ describe('Abstract pool test suite', () => {
             tasksQueueOptions: 'invalidTasksQueueOptions'
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new TypeError('Invalid tasks queue options: must be a plain object')
     )
     expect(
@@ -393,7 +389,7 @@ describe('Abstract pool test suite', () => {
             tasksQueueOptions: { concurrency: 0 }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new RangeError(
         'Invalid worker node tasks concurrency: 0 is a negative integer or zero'
       )
@@ -408,7 +404,7 @@ describe('Abstract pool test suite', () => {
             tasksQueueOptions: { concurrency: -1 }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new RangeError(
         'Invalid worker node tasks concurrency: -1 is a negative integer or zero'
       )
@@ -423,7 +419,7 @@ describe('Abstract pool test suite', () => {
             tasksQueueOptions: { concurrency: 0.2 }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new TypeError('Invalid worker node tasks concurrency: must be an integer')
     )
     expect(
@@ -436,7 +432,7 @@ describe('Abstract pool test suite', () => {
             tasksQueueOptions: { size: 0 }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new RangeError(
         'Invalid worker node tasks queue size: 0 is a negative integer or zero'
       )
@@ -451,7 +447,7 @@ describe('Abstract pool test suite', () => {
             tasksQueueOptions: { size: -1 }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new RangeError(
         'Invalid worker node tasks queue size: -1 is a negative integer or zero'
       )
@@ -466,7 +462,7 @@ describe('Abstract pool test suite', () => {
             tasksQueueOptions: { size: 0.2 }
           }
         )
-    ).toThrowError(
+    ).toThrow(
       new TypeError('Invalid worker node tasks queue size: must be an integer')
     )
   })
@@ -607,7 +603,7 @@ describe('Abstract pool test suite', () => {
     })
     expect(() =>
       pool.setWorkerChoiceStrategyOptions('invalidWorkerChoiceStrategyOptions')
-    ).toThrowError(
+    ).toThrow(
       new TypeError(
         'Invalid worker choice strategy options: must be a plain object'
       )
@@ -616,28 +612,24 @@ describe('Abstract pool test suite', () => {
       pool.setWorkerChoiceStrategyOptions({
         retries: 'invalidChoiceRetries'
       })
-    ).toThrowError(
+    ).toThrow(
       new TypeError(
         'Invalid worker choice strategy options: retries must be an integer'
       )
     )
-    expect(() =>
-      pool.setWorkerChoiceStrategyOptions({ retries: -1 })
-    ).toThrowError(
+    expect(() => pool.setWorkerChoiceStrategyOptions({ retries: -1 })).toThrow(
       new RangeError(
         "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero"
       )
     )
-    expect(() =>
-      pool.setWorkerChoiceStrategyOptions({ weights: {} })
-    ).toThrowError(
+    expect(() => pool.setWorkerChoiceStrategyOptions({ weights: {} })).toThrow(
       new Error(
         'Invalid worker choice strategy options: must have a weight for each worker node'
       )
     )
     expect(() =>
       pool.setWorkerChoiceStrategyOptions({ measurement: 'invalidMeasurement' })
-    ).toThrowError(
+    ).toThrow(
       new Error(
         "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'"
       )
@@ -652,10 +644,6 @@ describe('Abstract pool test suite', () => {
     )
     expect(pool.opts.enableTasksQueue).toBe(false)
     expect(pool.opts.tasksQueueOptions).toBeUndefined()
-    for (const workerNode of pool.workerNodes) {
-      expect(workerNode.onEmptyQueue).toBeUndefined()
-      expect(workerNode.onBackPressure).toBeUndefined()
-    }
     pool.enableTasksQueue(true)
     expect(pool.opts.enableTasksQueue).toBe(true)
     expect(pool.opts.tasksQueueOptions).toStrictEqual({
@@ -664,10 +652,6 @@ describe('Abstract pool test suite', () => {
       taskStealing: true,
       tasksStealingOnBackPressure: true
     })
-    for (const workerNode of pool.workerNodes) {
-      expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
-      expect(workerNode.onBackPressure).toBeInstanceOf(Function)
-    }
     pool.enableTasksQueue(true, { concurrency: 2 })
     expect(pool.opts.enableTasksQueue).toBe(true)
     expect(pool.opts.tasksQueueOptions).toStrictEqual({
@@ -676,17 +660,9 @@ describe('Abstract pool test suite', () => {
       taskStealing: true,
       tasksStealingOnBackPressure: true
     })
-    for (const workerNode of pool.workerNodes) {
-      expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
-      expect(workerNode.onBackPressure).toBeInstanceOf(Function)
-    }
     pool.enableTasksQueue(false)
     expect(pool.opts.enableTasksQueue).toBe(false)
     expect(pool.opts.tasksQueueOptions).toBeUndefined()
-    for (const workerNode of pool.workerNodes) {
-      expect(workerNode.onEmptyQueue).toBeUndefined()
-      expect(workerNode.onBackPressure).toBeUndefined()
-    }
     await pool.destroy()
   })
 
@@ -706,8 +682,6 @@ describe('Abstract pool test suite', () => {
       expect(workerNode.tasksQueueBackPressureSize).toBe(
         pool.opts.tasksQueueOptions.size
       )
-      expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
-      expect(workerNode.onBackPressure).toBeInstanceOf(Function)
     }
     pool.setTasksQueueOptions({
       concurrency: 2,
@@ -725,8 +699,6 @@ describe('Abstract pool test suite', () => {
       expect(workerNode.tasksQueueBackPressureSize).toBe(
         pool.opts.tasksQueueOptions.size
       )
-      expect(workerNode.onEmptyQueue).toBeUndefined()
-      expect(workerNode.onBackPressure).toBeUndefined()
     }
     pool.setTasksQueueOptions({
       concurrency: 1,
@@ -743,38 +715,34 @@ describe('Abstract pool test suite', () => {
       expect(workerNode.tasksQueueBackPressureSize).toBe(
         pool.opts.tasksQueueOptions.size
       )
-      expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
-      expect(workerNode.onBackPressure).toBeInstanceOf(Function)
     }
-    expect(() =>
-      pool.setTasksQueueOptions('invalidTasksQueueOptions')
-    ).toThrowError(
+    expect(() => pool.setTasksQueueOptions('invalidTasksQueueOptions')).toThrow(
       new TypeError('Invalid tasks queue options: must be a plain object')
     )
-    expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError(
+    expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrow(
       new RangeError(
         'Invalid worker node tasks concurrency: 0 is a negative integer or zero'
       )
     )
-    expect(() => pool.setTasksQueueOptions({ concurrency: -1 })).toThrowError(
+    expect(() => pool.setTasksQueueOptions({ concurrency: -1 })).toThrow(
       new RangeError(
         'Invalid worker node tasks concurrency: -1 is a negative integer or zero'
       )
     )
-    expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError(
+    expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrow(
       new TypeError('Invalid worker node tasks concurrency: must be an integer')
     )
-    expect(() => pool.setTasksQueueOptions({ size: 0 })).toThrowError(
+    expect(() => pool.setTasksQueueOptions({ size: 0 })).toThrow(
       new RangeError(
         'Invalid worker node tasks queue size: 0 is a negative integer or zero'
       )
     )
-    expect(() => pool.setTasksQueueOptions({ size: -1 })).toThrowError(
+    expect(() => pool.setTasksQueueOptions({ size: -1 })).toThrow(
       new RangeError(
         'Invalid worker node tasks queue size: -1 is a negative integer or zero'
       )
     )
-    expect(() => pool.setTasksQueueOptions({ size: 0.2 })).toThrowError(
+    expect(() => pool.setTasksQueueOptions({ size: 0.2 })).toThrow(
       new TypeError('Invalid worker node tasks queue size: must be an integer')
     )
     await pool.destroy()
@@ -839,6 +807,7 @@ describe('Abstract pool test suite', () => {
           executing: 0,
           queued: 0,
           maxQueued: 0,
+          sequentiallyStolen: 0,
           stolen: 0,
           failed: 0
         },
@@ -919,6 +888,24 @@ describe('Abstract pool test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify that pool statuses are checked at start or destroy', async () => {
+    const pool = new FixedThreadPool(
+      numberOfWorkers,
+      './tests/worker-files/thread/testWorker.mjs'
+    )
+    expect(pool.info.started).toBe(true)
+    expect(pool.info.ready).toBe(true)
+    expect(() => pool.start()).toThrow(
+      new Error('Cannot start an already started pool')
+    )
+    await pool.destroy()
+    expect(pool.info.started).toBe(false)
+    expect(pool.info.ready).toBe(false)
+    await expect(pool.destroy()).rejects.toThrow(
+      new Error('Cannot destroy an already destroyed pool')
+    )
+  })
+
   it('Verify that pool can be started after initialization', async () => {
     const pool = new FixedClusterPool(
       numberOfWorkers,
@@ -929,13 +916,16 @@ describe('Abstract pool test suite', () => {
     )
     expect(pool.info.started).toBe(false)
     expect(pool.info.ready).toBe(false)
+    expect(pool.readyEventEmitted).toBe(false)
     expect(pool.workerNodes).toStrictEqual([])
-    await expect(pool.execute()).rejects.toThrowError(
+    await expect(pool.execute()).rejects.toThrow(
       new Error('Cannot execute a task on not started pool')
     )
     pool.start()
     expect(pool.info.started).toBe(true)
     expect(pool.info.ready).toBe(true)
+    await waitPoolEvents(pool, PoolEvents.ready, 1)
+    expect(pool.readyEventEmitted).toBe(true)
     expect(pool.workerNodes.length).toBe(numberOfWorkers)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode).toBeInstanceOf(WorkerNode)
@@ -948,20 +938,20 @@ describe('Abstract pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js'
     )
-    await expect(pool.execute(undefined, 0)).rejects.toThrowError(
+    await expect(pool.execute(undefined, 0)).rejects.toThrow(
       new TypeError('name argument must be a string')
     )
-    await expect(pool.execute(undefined, '')).rejects.toThrowError(
+    await expect(pool.execute(undefined, '')).rejects.toThrow(
       new TypeError('name argument must not be an empty string')
     )
-    await expect(pool.execute(undefined, undefined, {})).rejects.toThrowError(
+    await expect(pool.execute(undefined, undefined, {})).rejects.toThrow(
       new TypeError('transferList argument must be an array')
     )
     await expect(pool.execute(undefined, 'unknown')).rejects.toBe(
       "Task function 'unknown' not found"
     )
     await pool.destroy()
-    await expect(pool.execute()).rejects.toThrowError(
+    await expect(pool.execute()).rejects.toThrow(
       new Error('Cannot execute a task on not started pool')
     )
   })
@@ -983,6 +973,7 @@ describe('Abstract pool test suite', () => {
           executing: maxMultiplier,
           queued: 0,
           maxQueued: 0,
+          sequentiallyStolen: 0,
           stolen: 0,
           failed: 0
         },
@@ -1010,6 +1001,7 @@ describe('Abstract pool test suite', () => {
           executing: 0,
           queued: 0,
           maxQueued: 0,
+          sequentiallyStolen: 0,
           stolen: 0,
           failed: 0
         },
@@ -1051,6 +1043,7 @@ describe('Abstract pool test suite', () => {
           executing: 0,
           queued: 0,
           maxQueued: 0,
+          sequentiallyStolen: 0,
           stolen: 0,
           failed: 0
         },
@@ -1086,6 +1079,7 @@ describe('Abstract pool test suite', () => {
           executing: 0,
           queued: 0,
           maxQueued: 0,
+          sequentiallyStolen: 0,
           stolen: 0,
           failed: 0
         },
@@ -1312,18 +1306,18 @@ describe('Abstract pool test suite', () => {
     await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1)
     await expect(
       dynamicThreadPool.addTaskFunction(0, () => {})
-    ).rejects.toThrowError(new TypeError('name argument must be a string'))
+    ).rejects.toThrow(new TypeError('name argument must be a string'))
     await expect(
       dynamicThreadPool.addTaskFunction('', () => {})
-    ).rejects.toThrowError(
+    ).rejects.toThrow(
       new TypeError('name argument must not be an empty string')
     )
-    await expect(
-      dynamicThreadPool.addTaskFunction('test', 0)
-    ).rejects.toThrowError(new TypeError('fn argument must be a function'))
-    await expect(
-      dynamicThreadPool.addTaskFunction('test', '')
-    ).rejects.toThrowError(new TypeError('fn argument must be a function'))
+    await expect(dynamicThreadPool.addTaskFunction('test', 0)).rejects.toThrow(
+      new TypeError('fn argument must be a function')
+    )
+    await expect(dynamicThreadPool.addTaskFunction('test', '')).rejects.toThrow(
+      new TypeError('fn argument must be a function')
+    )
     expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
       DEFAULT_TASK_NAME,
       'test'
@@ -1353,6 +1347,7 @@ describe('Abstract pool test suite', () => {
           executing: 0,
           queued: 0,
           stolen: 0,
+          sequentiallyStolen: 0,
           failed: 0
         },
         runTime: {
@@ -1385,9 +1380,7 @@ describe('Abstract pool test suite', () => {
       DEFAULT_TASK_NAME,
       'test'
     ])
-    await expect(
-      dynamicThreadPool.removeTaskFunction('test')
-    ).rejects.toThrowError(
+    await expect(dynamicThreadPool.removeTaskFunction('test')).rejects.toThrow(
       new Error('Cannot remove a task function not handled on the pool side')
     )
     const echoTaskFunction = data => {
@@ -1450,25 +1443,24 @@ describe('Abstract pool test suite', () => {
       './tests/worker-files/thread/testMultipleTaskFunctionsWorker.mjs'
     )
     await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1)
-    await expect(
-      dynamicThreadPool.setDefaultTaskFunction(0)
-    ).rejects.toThrowError(
+    const workerId = dynamicThreadPool.workerNodes[0].info.id
+    await expect(dynamicThreadPool.setDefaultTaskFunction(0)).rejects.toThrow(
       new Error(
-        "Task function operation 'default' failed on worker 31 with error: 'TypeError: name parameter is not a string'"
+        `Task function operation 'default' failed on worker ${workerId} with error: 'TypeError: name parameter is not a string'`
       )
     )
     await expect(
       dynamicThreadPool.setDefaultTaskFunction(DEFAULT_TASK_NAME)
-    ).rejects.toThrowError(
+    ).rejects.toThrow(
       new Error(
-        "Task function operation 'default' failed on worker 31 with error: 'Error: Cannot set the default task function reserved name as the default task function'"
+        `Task function operation 'default' failed on worker ${workerId} with error: 'Error: Cannot set the default task function reserved name as the default task function'`
       )
     )
     await expect(
       dynamicThreadPool.setDefaultTaskFunction('unknown')
-    ).rejects.toThrowError(
+    ).rejects.toThrow(
       new Error(
-        "Task function operation 'default' failed on worker 31 with error: 'Error: Cannot set the default task function to a non-existing task function'"
+        `Task function operation 'default' failed on worker ${workerId} with error: 'Error: Cannot set the default task function to a non-existing task function'`
       )
     )
     expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
@@ -1495,6 +1487,7 @@ describe('Abstract pool test suite', () => {
       'jsonIntegerSerialization',
       'factorial'
     ])
+    await dynamicThreadPool.destroy()
   })
 
   it('Verify that multiple task functions worker is working', async () => {
@@ -1529,6 +1522,7 @@ describe('Abstract pool test suite', () => {
             executing: 0,
             failed: 0,
             queued: 0,
+            sequentiallyStolen: 0,
             stolen: 0
           },
           runTime: {