Improvements based on https://github.com/pioardi/poolifier/issues/6
[poolifier.git] / tests / dynamic.test.js
index 7b0865bb9f951c9c038f2c0c80f6bf6440e81656..e8be74ace413c874a008dbd846c2ec98811d46cb 100644 (file)
@@ -1,9 +1,9 @@
 const expect = require('expect')
 const DynamicThreadPool = require('../lib/dynamic')
 const min = 1
-const max = 10
+const max = 3
 const pool = new DynamicThreadPool(min, max,
-  './tests/testWorker.js',
+  './tests/workers/testWorker.js',
   { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') })
 
 describe('Dynamic thread pool test suite ', () => {
@@ -16,7 +16,9 @@ describe('Dynamic thread pool test suite ', () => {
   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 closedThreads = 0
-    for (let i = 0; i < (max * 3); i++) {
+    let fullPool = 0
+    pool.emitter.on('FullPool', () => fullPool++)
+    for (let i = 0; i < (max * 2); i++) {
       promises.push(pool.execute({ test: 'test' }))
     }
     expect(pool.workers.length).toBe(max)
@@ -25,7 +27,8 @@ describe('Dynamic thread pool test suite ', () => {
         closedThreads++
       })
     })
-    await new Promise(resolve => setTimeout(resolve, 1000 * 2))
+    expect(fullPool > 1).toBeTruthy()
+    await new Promise(resolve => setTimeout(resolve, 2000))
     expect(closedThreads).toBe(max - min)
   })
 
@@ -36,8 +39,7 @@ describe('Dynamic thread pool test suite ', () => {
         closedThreads++
       })
     })
-    pool.destroy()
-    await new Promise(resolve => setTimeout(resolve, 1000))
+    await pool.destroy()
     expect(closedThreads).toBe(min)
   })
 
@@ -54,7 +56,7 @@ describe('Dynamic thread pool test suite ', () => {
   })
 
   it('Should work even without opts in input', async () => {
-    const pool1 = new DynamicThreadPool(1, 1, './tests/testWorker.js')
+    const pool1 = new DynamicThreadPool(1, 1, './tests/workers/testWorker.js')
     const res = await pool1.execute({ test: 'test' })
     expect(res).toBeFalsy()
   })