Merge branch 'master' of github.com:poolifier/poolifier into waittime
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index 366d4965960f6eddfb17856331fab8542b44fce3..02baefaf93b5639867d8bb6e852dce39c25dfa57 100644 (file)
@@ -1,5 +1,6 @@
 const { expect } = require('expect')
 const {
+  DynamicClusterPool,
   DynamicThreadPool,
   FixedClusterPool,
   FixedThreadPool,
@@ -11,9 +12,6 @@ const { Queue } = require('../../../lib/queue')
 
 describe('Abstract pool test suite', () => {
   const numberOfWorkers = 1
-  const workerNotFoundInPoolError = new Error(
-    'Worker could not be found in the pool worker nodes'
-  )
   class StubPoolWithRemoveAllWorker extends FixedThreadPool {
     removeAllWorker () {
       this.workerNodes = []
@@ -74,7 +72,7 @@ describe('Abstract pool test suite', () => {
         new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
     ).toThrowError(
       new TypeError(
-        'Cannot instantiate a pool with a non integer number of workers'
+        'Cannot instantiate a pool with a non safe integer number of workers'
       )
     )
   })
@@ -104,8 +102,11 @@ describe('Abstract pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js',
       {
-        workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED,
-        workerChoiceStrategyOptions: { medRunTime: true },
+        workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED,
+        workerChoiceStrategyOptions: {
+          medRunTime: true,
+          weights: { 0: 300 }
+        },
         enableEvents: false,
         enableTasksQueue: true,
         tasksQueueOptions: { concurrency: 2 },
@@ -120,10 +121,11 @@ describe('Abstract pool test suite', () => {
     expect(pool.opts.enableTasksQueue).toBe(true)
     expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 })
     expect(pool.opts.workerChoiceStrategy).toBe(
-      WorkerChoiceStrategies.LESS_USED
+      WorkerChoiceStrategies.LEAST_USED
     )
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
-      medRunTime: true
+      medRunTime: true,
+      weights: { 0: 300 }
     })
     expect(pool.opts.messageHandler).toStrictEqual(testHandler)
     expect(pool.opts.errorHandler).toStrictEqual(testHandler)
@@ -154,6 +156,18 @@ describe('Abstract pool test suite', () => {
           }
         )
     ).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
+    expect(
+      () =>
+        new FixedThreadPool(
+          numberOfWorkers,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            workerChoiceStrategyOptions: { weights: {} }
+          }
+        )
+    ).toThrowError(
+      'Invalid worker choice strategy options: must have a weight for each worker node'
+    )
   })
 
   it('Verify that worker choice strategy options can be set', async () => {
@@ -240,7 +254,7 @@ describe('Abstract pool test suite', () => {
     await pool.destroy()
   })
 
-  it('Simulate worker not found at getWorkerTasksUsage()', async () => {
+  it('Simulate worker not found', async () => {
     const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
@@ -252,9 +266,6 @@ describe('Abstract pool test suite', () => {
     // Simulate worker not found.
     pool.removeAllWorker()
     expect(pool.workerNodes.length).toBe(0)
-    expect(() => pool.getWorkerTasksUsage()).toThrowError(
-      workerNotFoundInPoolError
-    )
     await pool.destroy()
   })
 
@@ -398,4 +409,21 @@ describe('Abstract pool test suite', () => {
     expect(poolBusy).toBe(numberOfWorkers + 1)
     await pool.destroy()
   })
+
+  it('Verify that multiple tasks worker is working', async () => {
+    const pool = new DynamicClusterPool(
+      numberOfWorkers,
+      numberOfWorkers * 2,
+      './tests/worker-files/cluster/testMultiTasksWorker.js'
+    )
+    const data = { n: 10 }
+    const result0 = await pool.execute(data)
+    expect(result0).toBe(false)
+    const result1 = await pool.execute(data, 'jsonIntegerSerialization')
+    expect(result1).toBe(false)
+    const result2 = await pool.execute(data, 'factorial')
+    expect(result2).toBe(3628800)
+    const result3 = await pool.execute(data, 'fibonacci')
+    expect(result3).toBe(89)
+  })
 })