Rename folder
[poolifier.git] / tests / pools / thread / fixed.test.js
index b0cf9e576517dfe9179747ed734f38544ea2c107..ff945a700f6bf6efe85359b260499597c3497a1c 100644 (file)
@@ -1,33 +1,44 @@
 const expect = require('expect')
 const { FixedThreadPool } = require('../../../lib/index')
-const numThreads = 10
+const numberOfThreads = 10
+const maxTasks = 400
 const pool = new FixedThreadPool(
-  numThreads,
-  './tests/worker/thread/testWorker.js',
+  numberOfThreads,
+  './tests/worker-files/thread/testWorker.js',
   {
     errorHandler: e => console.error(e),
     onlineHandler: () => console.log('worker is online')
   }
 )
-const emptyPool = new FixedThreadPool(1, './tests/worker/thread/emptyWorker.js')
-const echoPool = new FixedThreadPool(1, './tests/worker/thread/echoWorker.js')
+const emptyPool = new FixedThreadPool(
+  1,
+  './tests/worker-files/thread/emptyWorker.js'
+)
+const echoPool = new FixedThreadPool(
+  1,
+  './tests/worker-files/thread/echoWorker.js'
+)
 const errorPool = new FixedThreadPool(
   1,
-  './tests/worker/thread/errorWorker.js',
+  './tests/worker-files/thread/errorWorker.js',
   {
     errorHandler: e => console.error(e),
     onlineHandler: () => console.log('worker is online')
   }
 )
-const asyncPool = new FixedThreadPool(1, './tests/worker/thread/asyncWorker.js')
+const asyncPool = new FixedThreadPool(
+  1,
+  './tests/worker-files/thread/asyncWorker.js',
+  { maxTasks: maxTasks }
+)
 
 describe('Fixed thread pool test suite ', () => {
   it('Choose worker round robin test', async () => {
     const results = new Set()
-    for (let i = 0; i < numThreads; i++) {
+    for (let i = 0; i < numberOfThreads; i++) {
       results.add(pool.chooseWorker().threadId)
     }
-    expect(results.size).toBe(numThreads)
+    expect(results.size).toBe(numberOfThreads)
   })
 
   it('Verify that the function is executed in a worker thread', async () => {
@@ -77,6 +88,11 @@ describe('Fixed thread pool test suite ', () => {
     expect(usedTime).toBeGreaterThanOrEqual(2000)
   })
 
+  it('Verify that maxTasks is set properly', async () => {
+    const worker = asyncPool.chooseWorker()
+    expect(worker.port2.getMaxListeners()).toBe(maxTasks)
+  })
+
   it('Shutdown test', async () => {
     let closedThreads = 0
     pool.workers.forEach(w => {
@@ -85,7 +101,7 @@ describe('Fixed thread pool test suite ', () => {
       })
     })
     await pool.destroy()
-    expect(closedThreads).toBe(numThreads)
+    expect(closedThreads).toBe(numberOfThreads)
   })
 
   it('Validations test', () => {
@@ -101,7 +117,10 @@ describe('Fixed thread pool test suite ', () => {
   })
 
   it('Should work even without opts in input', async () => {
-    const pool1 = new FixedThreadPool(1, './tests/worker/thread/testWorker.js')
+    const pool1 = new FixedThreadPool(
+      1,
+      './tests/worker-files/thread/testWorker.js'
+    )
     const res = await pool1.execute({ test: 'test' })
     expect(res).toBeFalsy()
   })