fix: fix back pressure detection
[poolifier.git] / tests / pools / thread / fixed.test.js
index 01622f1622c87237e1dc501313ff14ef348cc2dd..aeebc4a7a623a1151f37f41aaf7f56ddcfcadbc1 100644 (file)
@@ -1,21 +1,33 @@
 const { expect } = require('expect')
-const { FixedThreadPool } = require('../../../lib/index')
-const { WorkerFunctions } = require('../../test-types')
-const TestUtils = require('../../test-utils')
+const { FixedThreadPool, PoolEvents } = require('../../../lib')
+const { TaskFunctions } = require('../../test-types')
+const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils')
 
 describe('Fixed thread pool test suite', () => {
   const numberOfThreads = 6
+  const tasksConcurrency = 2
   const pool = new FixedThreadPool(
     numberOfThreads,
     './tests/worker-files/thread/testWorker.js',
     {
-      errorHandler: e => console.error(e)
+      errorHandler: (e) => console.error(e)
+    }
+  )
+  const queuePool = new FixedThreadPool(
+    numberOfThreads,
+    './tests/worker-files/thread/testWorker.js',
+    {
+      enableTasksQueue: true,
+      tasksQueueOptions: {
+        concurrency: tasksConcurrency
+      },
+      errorHandler: (e) => console.error(e)
     }
   )
   const emptyPool = new FixedThreadPool(
     numberOfThreads,
     './tests/worker-files/thread/emptyWorker.js',
-    { exitHandler: () => console.log('empty pool worker exited') }
+    { exitHandler: () => console.info('empty pool worker exited') }
   )
   const echoPool = new FixedThreadPool(
     numberOfThreads,
@@ -25,14 +37,14 @@ describe('Fixed thread pool test suite', () => {
     numberOfThreads,
     './tests/worker-files/thread/errorWorker.js',
     {
-      errorHandler: e => console.error(e)
+      errorHandler: (e) => console.error(e)
     }
   )
   const asyncErrorPool = new FixedThreadPool(
     numberOfThreads,
     './tests/worker-files/thread/asyncErrorWorker.js',
     {
-      errorHandler: e => console.error(e)
+      errorHandler: (e) => console.error(e)
     }
   )
   const asyncPool = new FixedThreadPool(
@@ -47,44 +59,93 @@ describe('Fixed thread pool test suite', () => {
     await errorPool.destroy()
     await asyncErrorPool.destroy()
     await emptyPool.destroy()
-  })
-
-  it('Choose worker round robin test', async () => {
-    const results = new Set()
-    for (let i = 0; i < numberOfThreads; i++) {
-      results.add(pool.chooseWorker().threadId)
-    }
-    expect(results.size).toBe(numberOfThreads)
+    await queuePool.destroy()
   })
 
   it('Verify that the function is executed in a worker thread', async () => {
     let result = await pool.execute({
-      function: WorkerFunctions.fibonacci
+      function: TaskFunctions.fibonacci
     })
-    expect(result).toBe(false)
+    expect(result).toBe(75025)
     result = await pool.execute({
-      function: WorkerFunctions.factorial
+      function: TaskFunctions.factorial
     })
-    expect(result).toBe(false)
+    expect(result).toBe(9.33262154439441e157)
   })
 
-  it('Verify that is possible to invoke the execute method without input', async () => {
+  it('Verify that is possible to invoke the execute() method without input', async () => {
     const result = await pool.execute()
-    expect(result).toBe(false)
+    expect(result).toStrictEqual({ ok: 1 })
   })
 
-  it('Verify that busy event is emitted', async () => {
-    const promises = []
+  it("Verify that 'ready' event is emitted", async () => {
+    const pool1 = new FixedThreadPool(
+      numberOfThreads,
+      './tests/worker-files/thread/testWorker.js',
+      {
+        errorHandler: (e) => console.error(e)
+      }
+    )
+    let poolReady = 0
+    pool1.emitter.on(PoolEvents.ready, () => ++poolReady)
+    await waitPoolEvents(pool1, 'ready', 1)
+    expect(poolReady).toBe(1)
+  })
+
+  it("Verify that 'busy' event is emitted", () => {
     let poolBusy = 0
-    pool.emitter.on('busy', () => poolBusy++)
+    pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
     for (let i = 0; i < numberOfThreads * 2; i++) {
-      promises.push(pool.execute())
+      pool.execute()
     }
     // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
     // So in total numberOfThreads + 1 times for a loop submitting up to numberOfThreads * 2 tasks to the fixed pool.
     expect(poolBusy).toBe(numberOfThreads + 1)
   })
 
+  it('Verify that tasks queuing is working', async () => {
+    const promises = new Set()
+    const maxMultiplier = 3 // Must be greater than tasksConcurrency
+    for (let i = 0; i < numberOfThreads * maxMultiplier; i++) {
+      promises.add(queuePool.execute())
+    }
+    expect(promises.size).toBe(numberOfThreads * maxMultiplier)
+    for (const workerNode of queuePool.workerNodes) {
+      expect(workerNode.usage.tasks.executing).toBeGreaterThanOrEqual(0)
+      expect(workerNode.usage.tasks.executing).toBeLessThanOrEqual(
+        queuePool.opts.tasksQueueOptions.concurrency
+      )
+      expect(workerNode.usage.tasks.executed).toBe(0)
+      expect(workerNode.usage.tasks.queued).toBe(
+        maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
+      )
+      expect(workerNode.usage.tasks.maxQueued).toBe(
+        maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
+      )
+    }
+    expect(queuePool.info.executingTasks).toBe(
+      numberOfThreads * queuePool.opts.tasksQueueOptions.concurrency
+    )
+    expect(queuePool.info.queuedTasks).toBe(
+      numberOfThreads *
+        (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
+    )
+    expect(queuePool.info.maxQueuedTasks).toBe(
+      numberOfThreads *
+        (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
+    )
+    expect(queuePool.info.backPressure).toBe(false)
+    await Promise.all(promises)
+    for (const workerNode of queuePool.workerNodes) {
+      expect(workerNode.usage.tasks.executing).toBe(0)
+      expect(workerNode.usage.tasks.executed).toBe(maxMultiplier)
+      expect(workerNode.usage.tasks.queued).toBe(0)
+      expect(workerNode.usage.tasks.maxQueued).toBe(
+        maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
+      )
+    }
+  })
+
   it('Verify that is possible to have a worker that return undefined', async () => {
     const result = await emptyPool.execute()
     expect(result).toBeUndefined()
@@ -96,8 +157,38 @@ describe('Fixed thread pool test suite', () => {
     expect(result).toStrictEqual(data)
   })
 
+  it('Verify that transferable objects are sent to the worker correctly', async () => {
+    let error
+    let result
+    try {
+      result = await pool.execute(undefined, undefined, [
+        new ArrayBuffer(16),
+        new MessageChannel().port1
+      ])
+    } catch (e) {
+      error = e
+    }
+    expect(result).toStrictEqual({ ok: 1 })
+    expect(error).toBeUndefined()
+    try {
+      result = await pool.execute(undefined, undefined, [
+        new SharedArrayBuffer(16)
+      ])
+    } catch (e) {
+      error = e
+    }
+    expect(result).toStrictEqual({ ok: 1 })
+    expect(error).toStrictEqual(
+      new TypeError('Found invalid object in transferList')
+    )
+  })
+
   it('Verify that error handling is working properly:sync', async () => {
     const data = { f: 10 }
+    let taskError
+    errorPool.emitter.on(PoolEvents.taskError, (e) => {
+      taskError = e
+    })
     let inError
     try {
       await errorPool.execute(data)
@@ -109,10 +200,24 @@ describe('Fixed thread pool test suite', () => {
     expect(inError.message).toBeDefined()
     expect(typeof inError.message === 'string').toBe(true)
     expect(inError.message).toBe('Error Message from ThreadWorker')
+    expect(taskError).toStrictEqual({
+      name: 'default',
+      message: new Error('Error Message from ThreadWorker'),
+      data
+    })
+    expect(
+      errorPool.workerNodes.some(
+        (workerNode) => workerNode.usage.tasks.failed === 1
+      )
+    ).toBe(true)
   })
 
   it('Verify that error handling is working properly:async', async () => {
     const data = { f: 10 }
+    let taskError
+    asyncErrorPool.emitter.on(PoolEvents.taskError, (e) => {
+      taskError = e
+    })
     let inError
     try {
       await asyncErrorPool.execute(data)
@@ -124,22 +229,53 @@ describe('Fixed thread pool test suite', () => {
     expect(inError.message).toBeDefined()
     expect(typeof inError.message === 'string').toBe(true)
     expect(inError.message).toBe('Error Message from ThreadWorker:async')
+    expect(taskError).toStrictEqual({
+      name: 'default',
+      message: new Error('Error Message from ThreadWorker:async'),
+      data
+    })
+    expect(
+      asyncErrorPool.workerNodes.some(
+        (workerNode) => workerNode.usage.tasks.failed === 1
+      )
+    ).toBe(true)
   })
 
   it('Verify that async function is working properly', async () => {
     const data = { f: 10 }
-    const startTime = Date.now()
+    const startTime = performance.now()
     const result = await asyncPool.execute(data)
-    const usedTime = Date.now() - startTime
+    const usedTime = performance.now() - startTime
     expect(result).toStrictEqual(data)
     expect(usedTime).toBeGreaterThanOrEqual(2000)
   })
 
   it('Shutdown test', async () => {
-    const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
+    const exitPromise = waitWorkerEvents(pool, 'exit', numberOfThreads)
+    let poolDestroy = 0
+    pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
     await pool.destroy()
     const numberOfExitEvents = await exitPromise
     expect(numberOfExitEvents).toBe(numberOfThreads)
+    expect(poolDestroy).toBe(1)
+  })
+
+  it('Verify that thread pool options are checked', async () => {
+    const workerFilePath = './tests/worker-files/thread/testWorker.js'
+    let pool1 = new FixedThreadPool(numberOfThreads, workerFilePath)
+    expect(pool1.opts.workerOptions).toBeUndefined()
+    await pool1.destroy()
+    pool1 = new FixedThreadPool(numberOfThreads, workerFilePath, {
+      workerOptions: {
+        env: { TEST: 'test' },
+        name: 'test'
+      }
+    })
+    expect(pool1.opts.workerOptions).toStrictEqual({
+      env: { TEST: 'test' },
+      name: 'test'
+    })
+    await pool1.destroy()
   })
 
   it('Should work even without opts in input', async () => {
@@ -148,7 +284,7 @@ describe('Fixed thread pool test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     const res = await pool1.execute()
-    expect(res).toBe(false)
+    expect(res).toStrictEqual({ ok: 1 })
     // We need to clean up the resources after our test
     await pool1.destroy()
   })
@@ -156,6 +292,6 @@ describe('Fixed thread pool test suite', () => {
   it('Verify that a pool with zero worker fails', async () => {
     expect(
       () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
-    ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))
+    ).toThrowError('Cannot instantiate a fixed pool with zero worker')
   })
 })