test: fix empty pool test
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index 86506f671f29848f2a8f56f25943f708f886de03..038868c2d1502ce721561b23c469bd845e69a8f1 100644 (file)
@@ -1,18 +1,17 @@
 const { expect } = require('expect')
 const {
+  DynamicClusterPool,
   DynamicThreadPool,
   FixedClusterPool,
   FixedThreadPool,
   PoolEvents,
   WorkerChoiceStrategies
-} = require('../../../lib/index')
+} = require('../../../lib')
 const { CircularArray } = require('../../../lib/circular-array')
+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 = []
@@ -73,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'
       )
     )
   })
@@ -131,7 +130,7 @@ describe('Abstract pool test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify that pool options are valid', async () => {
+  it('Verify that pool options are validated', async () => {
     expect(
       () =>
         new FixedThreadPool(
@@ -155,7 +154,91 @@ describe('Abstract pool test suite', () => {
     ).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
   })
 
-  it('Simulate worker not found at getWorkerTasksUsage()', async () => {
+  it('Verify that worker choice strategy options can be set', async () => {
+    const pool = new FixedThreadPool(
+      numberOfWorkers,
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
+    )
+    expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+      medRunTime: false
+    })
+    for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+      .workerChoiceStrategies) {
+      expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false })
+    }
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
+    ).toBe(true)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
+    pool.setWorkerChoiceStrategyOptions({ medRunTime: true })
+    expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+      medRunTime: true
+    })
+    for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+      .workerChoiceStrategies) {
+      expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: true })
+    }
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
+    ).toBe(false)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(true)
+    pool.setWorkerChoiceStrategyOptions({ medRunTime: false })
+    expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+      medRunTime: false
+    })
+    for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+      .workerChoiceStrategies) {
+      expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false })
+    }
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
+    ).toBe(true)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
+    await pool.destroy()
+  })
+
+  it('Verify that tasks queue can be enabled/disabled', async () => {
+    const pool = new FixedThreadPool(
+      numberOfWorkers,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    expect(pool.opts.enableTasksQueue).toBe(false)
+    expect(pool.opts.tasksQueueOptions).toBeUndefined()
+    pool.enableTasksQueue(true)
+    expect(pool.opts.enableTasksQueue).toBe(true)
+    expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 })
+    pool.enableTasksQueue(true, { concurrency: 2 })
+    expect(pool.opts.enableTasksQueue).toBe(true)
+    expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 })
+    pool.enableTasksQueue(false)
+    expect(pool.opts.enableTasksQueue).toBe(false)
+    expect(pool.opts.tasksQueueOptions).toBeUndefined()
+    await pool.destroy()
+  })
+
+  it('Verify that tasks queue options can be set', async () => {
+    const pool = new FixedThreadPool(
+      numberOfWorkers,
+      './tests/worker-files/thread/testWorker.js',
+      { enableTasksQueue: true }
+    )
+    expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 })
+    pool.setTasksQueueOptions({ concurrency: 2 })
+    expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 })
+    expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError(
+      "Invalid worker tasks concurrency '0'"
+    )
+    await pool.destroy()
+  })
+
+  it('Simulate worker not found', async () => {
     const pool = new StubPoolWithRemoveAllWorker(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js',
@@ -167,9 +250,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()
   })
 
@@ -199,8 +279,8 @@ describe('Abstract pool test suite', () => {
     )
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksQueue).toBeDefined()
-      expect(workerNode.tasksQueue).toBeInstanceOf(Array)
-      expect(workerNode.tasksQueue.length).toBe(0)
+      expect(workerNode.tasksQueue).toBeInstanceOf(Queue)
+      expect(workerNode.tasksQueue.size).toBe(0)
     }
     await pool.destroy()
   })
@@ -313,4 +393,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)
+  })
 })