test: improve UTs
[poolifier.git] / tests / pools / thread / dynamic.test.js
index a7f537349ed2b9d2e0a96ece08f3c1c5e2dd4964..e30099dd4f890097e5ee705662273ee1e04ce97e 100644 (file)
-const expect = require('expect')
-const { DynamicThreadPool } = require('../../../lib/index')
-const TestUtils = require('../../test-utils')
-const min = 1
-const max = 3
-const pool = new DynamicThreadPool(
-  min,
-  max,
-  './tests/worker-files/thread/testWorker.js',
-  {
-    errorHandler: e => console.error(e)
-  }
-)
+const { expect } = require('expect')
+const { DynamicThreadPool, PoolEvents } = require('../../../lib')
+const { TaskFunctions } = require('../../test-types')
+const { sleep, waitWorkerEvents } = require('../../test-utils')
 
 describe('Dynamic thread pool test suite', () => {
+  const min = 1
+  const max = 3
+  const pool = new DynamicThreadPool(
+    min,
+    max,
+    './tests/worker-files/thread/testWorker.js',
+    {
+      errorHandler: (e) => console.error(e)
+    }
+  )
+
   it('Verify that the function is executed in a worker thread', async () => {
-    const result = await pool.execute({ test: 'test' })
-    expect(result).toBeDefined()
-    expect(result).toBeFalsy()
+    let result = await pool.execute({
+      function: TaskFunctions.fibonacci
+    })
+    expect(result).toBe(75025)
+    result = await pool.execute({
+      function: TaskFunctions.factorial
+    })
+    expect(result).toBe(9.33262154439441e157)
   })
 
   it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
-    const promises = []
-    let fullPool = 0
-    pool.emitter.on('FullPool', () => fullPool++)
+    let poolBusy = 0
+    pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
     for (let i = 0; i < max * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      pool.execute()
     }
-    expect(pool.workers.length).toBe(max)
-    expect(fullPool > 1).toBeTruthy()
-    const res = await TestUtils.waitExits(pool, max - min)
-    expect(res).toBe(max - min)
+    expect(pool.workerNodes.length).toBeLessThanOrEqual(max)
+    expect(pool.workerNodes.length).toBeGreaterThan(min)
+    expect(poolBusy).toBe(1)
+    const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min)
+    expect(numberOfExitEvents).toBe(max - min)
   })
 
   it('Verify scale thread up and down is working', async () => {
-    expect(pool.workers.length).toBe(min)
-    for (let i = 0; i < max * 10; i++) {
-      pool.execute({ test: 'test' })
+    expect(pool.workerNodes.length).toBe(min)
+    for (let i = 0; i < max * 2; i++) {
+      pool.execute()
     }
-    expect(pool.workers.length).toBe(max)
-    await TestUtils.waitExits(pool, max - min)
-    expect(pool.workers.length).toBe(min)
-    for (let i = 0; i < max * 10; i++) {
-      pool.execute({ test: 'test' })
+    expect(pool.workerNodes.length).toBe(max)
+    await waitWorkerEvents(pool, 'exit', max - min)
+    expect(pool.workerNodes.length).toBe(min)
+    for (let i = 0; i < max * 2; i++) {
+      pool.execute()
     }
-    expect(pool.workers.length).toBe(max)
-    await TestUtils.waitExits(pool, max - min)
-    expect(pool.workers.length).toBe(min)
+    expect(pool.workerNodes.length).toBe(max)
+    await waitWorkerEvents(pool, 'exit', max - min)
+    expect(pool.workerNodes.length).toBe(min)
   })
 
   it('Shutdown test', async () => {
-    let closedThreads = 0
-    pool.workers.forEach(w => {
-      w.on('exit', () => {
-        closedThreads++
-      })
-    })
+    const exitPromise = waitWorkerEvents(pool, 'exit', min)
+    let poolDestroy = 0
+    pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
     await pool.destroy()
-    expect(closedThreads).toBe(min)
+    const numberOfExitEvents = await exitPromise
+    expect(pool.started).toBe(false)
+    expect(pool.workerNodes.length).toBe(0)
+    expect(numberOfExitEvents).toBe(min)
+    expect(poolDestroy).toBe(1)
   })
 
-  it('Validations test', () => {
-    expect(() => {
-      const pool1 = new DynamicThreadPool()
-    }).toThrowError(
-      new Error('Please specify a file with a worker implementation')
+  it('Validation of inputs test', () => {
+    expect(() => new DynamicThreadPool(min)).toThrowError(
+      'Please specify a file with a worker implementation'
     )
   })
 
   it('Should work even without opts in input', async () => {
-    const pool1 = new DynamicThreadPool(
-      1,
-      1,
+    const pool = new DynamicThreadPool(
+      min,
+      max,
       './tests/worker-files/thread/testWorker.js'
     )
-    const res = await pool1.execute({ test: 'test' })
-    expect(res).toBeFalsy()
+    const res = await pool.execute()
+    expect(res).toStrictEqual({ ok: 1 })
     // We need to clean up the resources after our test
-    await pool1.destroy()
+    await pool.destroy()
   })
 
-  it('Verify scale thread up and down is working when long running task is used:hard', async () => {
+  it('Verify scale thread up and down is working when long executing task is used:hard', async () => {
     const longRunningPool = new DynamicThreadPool(
       min,
       max,
       './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
       {
-        errorHandler: e => console.error(e),
-        onlineHandler: () => console.log('worker is online')
+        errorHandler: (e) => console.error(e),
+        onlineHandler: () => console.info('long executing worker is online'),
+        exitHandler: () => console.info('long executing worker exited')
       }
     )
-    expect(longRunningPool.workers.length).toBe(min)
-    for (let i = 0; i < max * 10; i++) {
-      longRunningPool.execute({ test: 'test' })
+    expect(longRunningPool.workerNodes.length).toBe(min)
+    for (let i = 0; i < max * 2; i++) {
+      longRunningPool.execute()
     }
-    expect(longRunningPool.workers.length).toBe(max)
-    await TestUtils.waitExits(longRunningPool, max - min)
-    expect(longRunningPool.workers.length).toBe(min)
+    expect(longRunningPool.workerNodes.length).toBe(max)
+    await waitWorkerEvents(longRunningPool, 'exit', max - min)
+    expect(longRunningPool.workerNodes.length).toBe(min)
+    expect(
+      longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy
+      ).nextWorkerNodeKey
+    ).toBeLessThan(longRunningPool.workerNodes.length)
     // We need to clean up the resources after our test
     await longRunningPool.destroy()
   })
 
-  it('Verify scale thread up and down is working when long running task is used:soft', async () => {
+  it('Verify scale thread up and down is working when long executing task is used:soft', async () => {
     const longRunningPool = new DynamicThreadPool(
       min,
       max,
       './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
       {
-        errorHandler: e => console.error(e),
-        onlineHandler: () => console.log('worker is online')
+        errorHandler: (e) => console.error(e),
+        onlineHandler: () => console.info('long executing worker is online'),
+        exitHandler: () => console.info('long executing worker exited')
       }
     )
-    expect(longRunningPool.workers.length).toBe(min)
-    for (let i = 0; i < max * 10; i++) {
-      longRunningPool.execute({ test: 'test' })
+    expect(longRunningPool.workerNodes.length).toBe(min)
+    for (let i = 0; i < max * 2; i++) {
+      longRunningPool.execute()
     }
-    expect(longRunningPool.workers.length).toBe(max)
-    await TestUtils.sleep(1500)
-    // Here we expect the workers to be at the max size since that the task is still running
-    expect(longRunningPool.workers.length).toBe(max)
+    expect(longRunningPool.workerNodes.length).toBe(max)
+    await sleep(1000)
+    // Here we expect the workerNodes to be at the max size since the task is still executing
+    expect(longRunningPool.workerNodes.length).toBe(max)
     // We need to clean up the resources after our test
     await longRunningPool.destroy()
   })
+
+  it('Verify that a pool with zero worker can be instantiated', async () => {
+    const pool = new DynamicThreadPool(
+      0,
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    expect(pool).toBeInstanceOf(DynamicThreadPool)
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
 })