Tests enhancement (#172)
[poolifier.git] / tests / pools / thread / fixed.test.js
index e160f17c72b6d4db2e8111d7fed488f84a666610..2a7d005b3dcd6d88a53e4e53e1d2216c55483de4 100644 (file)
@@ -1,19 +1,26 @@
 const expect = require('expect')
 const { FixedThreadPool } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
 const numberOfThreads = 10
 const maxTasks = 400
 const pool = new FixedThreadPool(
   numberOfThreads,
-  './tests/worker/thread/testWorker.js',
+  './tests/worker-files/thread/testWorker.js',
   {
     errorHandler: e => console.error(e)
   }
 )
-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')
@@ -21,7 +28,7 @@ const errorPool = new FixedThreadPool(
 )
 const asyncPool = new FixedThreadPool(
   1,
-  './tests/worker/thread/asyncWorker.js',
+  './tests/worker-files/thread/asyncWorker.js',
   { maxTasks: maxTasks }
 )
 
@@ -87,30 +94,17 @@ describe('Fixed thread pool test suite ', () => {
   })
 
   it('Shutdown test', async () => {
-    let closedThreads = 0
-    pool.workers.forEach(w => {
-      w.on('exit', () => {
-        closedThreads++
-      })
-    })
+    const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
     await pool.destroy()
-    expect(closedThreads).toBe(numberOfThreads)
-  })
-
-  it('Validations test', () => {
-    let error
-    try {
-      const pool1 = new FixedThreadPool()
-      console.log(pool1)
-    } catch (e) {
-      error = e
-    }
-    expect(error).toBeTruthy()
-    expect(error.message).toBeTruthy()
+    const res = await exitPromise
+    expect(res).toBe(numberOfThreads)
   })
 
   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()
   })