Implement PR reviews
[poolifier.git] / tests / pools / cluster / fixed.test.js
index 7e319ac686ea3fc63e36e40682e1c4ade38ab7db..51501df0350dde87d73be94cc4ddebb7be66cee2 100644 (file)
@@ -1,12 +1,12 @@
 const expect = require('expect')
 const { FixedClusterPool } = require('../../../lib/index')
-const numWorkers = 10
+const numberOfWorkers = 10
+const maxTasks = 500
 const pool = new FixedClusterPool(
-  numWorkers,
+  numberOfWorkers,
   './tests/worker/cluster/testWorker.js',
   {
-    errorHandler: e => console.error(e),
-    onlineHandler: () => console.log('worker is online')
+    errorHandler: e => console.error(e)
   }
 )
 const emptyPool = new FixedClusterPool(
@@ -18,8 +18,7 @@ const errorPool = new FixedClusterPool(
   1,
   './tests/worker/cluster/errorWorker.js',
   {
-    errorHandler: e => console.error(e),
-    onlineHandler: () => console.log('worker is online')
+    errorHandler: e => console.error(e)
   }
 )
 
@@ -27,22 +26,24 @@ const asyncErrorPool = new FixedClusterPool(
   1,
   './tests/worker/cluster/asyncErrorWorker.js',
   {
-    errorHandler: e => console.error(e),
     onlineHandler: () => console.log('worker is online')
   }
 )
 const asyncPool = new FixedClusterPool(
   1,
-  './tests/worker/cluster/asyncWorker.js'
+  './tests/worker/cluster/asyncWorker.js',
+  {
+    maxTasks: maxTasks
+  }
 )
 
 describe('Fixed cluster pool test suite ', () => {
   it('Choose worker round robin test', async () => {
     const results = new Set()
-    for (let i = 0; i < numWorkers; i++) {
+    for (let i = 0; i < numberOfWorkers; i++) {
       results.add(pool.chooseWorker().id)
     }
-    expect(results.size).toBe(numWorkers)
+    expect(results.size).toBe(numberOfWorkers)
   })
 
   it('Verify that the function is executed in a worker cluster', async () => {
@@ -105,6 +106,11 @@ describe('Fixed cluster pool test suite ', () => {
     expect(usedTime).toBeGreaterThanOrEqual(2000)
   })
 
+  it('Verify that maxTasks is set properly', async () => {
+    const worker = asyncPool.chooseWorker()
+    expect(worker.getMaxListeners()).toBe(maxTasks)
+  })
+
   it('Shutdown test', async () => {
     let closedWorkers = 0
     pool.workers.forEach(w => {
@@ -112,9 +118,9 @@ describe('Fixed cluster pool test suite ', () => {
         closedWorkers++
       })
     })
-    pool.destroy()
+    await pool.destroy()
     await new Promise(resolve => setTimeout(resolve, 200))
-    expect(closedWorkers).toBe(numWorkers)
+    expect(closedWorkers).toBe(numberOfWorkers)
   })
 
   it('Validations test', () => {