test: add missing pool destroy() calls
[poolifier.git] / tests / pools / thread / fixed.test.js
index 8cd337ee6fa1ceb7109e42a7e55255f76690392c..09363b84e450f5aa201c2e3428b5003af9653ceb 100644 (file)
@@ -2,6 +2,7 @@ const { expect } = require('expect')
 const { FixedThreadPool, PoolEvents } = require('../../../lib')
 const { TaskFunctions } = require('../../test-types')
 const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils')
+const { DEFAULT_TASK_NAME } = require('../../../lib/utils')
 
 describe('Fixed thread pool test suite', () => {
   const numberOfThreads = 6
@@ -79,7 +80,7 @@ describe('Fixed thread pool test suite', () => {
   })
 
   it("Verify that 'ready' event is emitted", async () => {
-    const pool1 = new FixedThreadPool(
+    const pool = new FixedThreadPool(
       numberOfThreads,
       './tests/worker-files/thread/testWorker.js',
       {
@@ -87,9 +88,10 @@ describe('Fixed thread pool test suite', () => {
       }
     )
     let poolReady = 0
-    pool1.emitter.on(PoolEvents.ready, () => ++poolReady)
-    await waitPoolEvents(pool1, PoolEvents.ready, 1)
+    pool.emitter.on(PoolEvents.ready, () => ++poolReady)
+    await waitPoolEvents(pool, PoolEvents.ready, 1)
     expect(poolReady).toBe(1)
+    await pool.destroy()
   })
 
   it("Verify that 'busy' event is emitted", async () => {
@@ -124,7 +126,9 @@ describe('Fixed thread pool test suite', () => {
       expect(workerNode.usage.tasks.maxQueued).toBe(
         maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
       )
+      expect(workerNode.usage.tasks.stolen).toBe(0)
     }
+    expect(queuePool.info.executedTasks).toBe(0)
     expect(queuePool.info.executingTasks).toBe(
       numberOfThreads * queuePool.opts.tasksQueueOptions.concurrency
     )
@@ -137,6 +141,7 @@ describe('Fixed thread pool test suite', () => {
         (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
     )
     expect(queuePool.info.backPressure).toBe(false)
+    expect(queuePool.info.stolenTasks).toBe(0)
     await Promise.all(promises)
     for (const workerNode of queuePool.workerNodes) {
       expect(workerNode.usage.tasks.executing).toBeGreaterThanOrEqual(0)
@@ -148,7 +153,17 @@ describe('Fixed thread pool test suite', () => {
       expect(workerNode.usage.tasks.maxQueued).toBe(
         maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
       )
+      expect(workerNode.usage.tasks.stolen).toBeGreaterThanOrEqual(0)
+      expect(workerNode.usage.tasks.stolen).toBeLessThanOrEqual(
+        numberOfThreads * maxMultiplier
+      )
     }
+    expect(queuePool.info.executedTasks).toBe(numberOfThreads * maxMultiplier)
+    expect(queuePool.info.backPressure).toBe(false)
+    expect(queuePool.info.stolenTasks).toBeGreaterThanOrEqual(0)
+    expect(queuePool.info.stolenTasks).toBeLessThanOrEqual(
+      numberOfThreads * maxMultiplier
+    )
   })
 
   it('Verify that is possible to have a worker that return undefined', async () => {
@@ -206,7 +221,7 @@ describe('Fixed thread pool test suite', () => {
     expect(typeof inError.message === 'string').toBe(true)
     expect(inError.message).toBe('Error Message from ThreadWorker')
     expect(taskError).toStrictEqual({
-      name: 'default',
+      name: DEFAULT_TASK_NAME,
       message: new Error('Error Message from ThreadWorker'),
       data
     })
@@ -235,7 +250,7 @@ describe('Fixed thread pool test suite', () => {
     expect(typeof inError.message === 'string').toBe(true)
     expect(inError.message).toBe('Error Message from ThreadWorker:async')
     expect(taskError).toStrictEqual({
-      name: 'default',
+      name: DEFAULT_TASK_NAME,
       message: new Error('Error Message from ThreadWorker:async'),
       data
     })
@@ -267,31 +282,31 @@ describe('Fixed thread pool test suite', () => {
 
   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, {
+    let pool = new FixedThreadPool(numberOfThreads, workerFilePath)
+    expect(pool.opts.workerOptions).toBeUndefined()
+    await pool.destroy()
+    pool = new FixedThreadPool(numberOfThreads, workerFilePath, {
       workerOptions: {
         env: { TEST: 'test' },
         name: 'test'
       }
     })
-    expect(pool1.opts.workerOptions).toStrictEqual({
+    expect(pool.opts.workerOptions).toStrictEqual({
       env: { TEST: 'test' },
       name: 'test'
     })
-    await pool1.destroy()
+    await pool.destroy()
   })
 
   it('Should work even without opts in input', async () => {
-    const pool1 = new FixedThreadPool(
+    const pool = new FixedThreadPool(
       numberOfThreads,
       './tests/worker-files/thread/testWorker.js'
     )
-    const res = await pool1.execute()
+    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 that a pool with zero worker fails', async () => {