Tests enhancement (#172)
authorAlessandro Pio Ardizio <alessandroardizio94@gmail.com>
Wed, 17 Feb 2021 09:54:49 +0000 (10:54 +0100)
committerGitHub <noreply@github.com>
Wed, 17 Feb 2021 09:54:49 +0000 (10:54 +0100)
* Increase coverage and remove deprecated methods

* Add utility test function instead of use setTimeout to wait workers scale down

* Decrease a little bit the required nyc percentages

* Add a TestUtils class.

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
* More sleep usage.

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
* Run npm format.

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
* Remove deprecated methods and update changelog, increase require coverage

* Check coverage change

Co-authored-by: Jérôme Benoit <jerome.benoit@sap.com>
16 files changed:
.nycrc.json [new file with mode: 0644]
CHANGELOG.md
benchmarks/bench.js
package.json
src/pools/abstract-pool.ts
tests/pools/cluster/dynamic.test.js
tests/pools/cluster/fixed.test.js
tests/pools/thread/dynamic.test.js
tests/pools/thread/fixed.test.js
tests/test-utils.js [new file with mode: 0644]
tests/worker-files/cluster/echoWorker.js
tests/worker-files/cluster/longRunningWorkerHardBehavior.js [moved from tests/worker/cluster/longRunningWorkerHardBehavior.js with 100% similarity]
tests/worker-files/cluster/longRunningWorkerSoftBehavior.js [moved from tests/worker/cluster/longRunningWorkerSoftBehavior.js with 100% similarity]
tests/worker-files/thread/echoWorker.js
tests/worker-files/thread/longRunningWorkerHardBehavior.js [moved from tests/worker/thread/longRunningWorkerHardBehavior.js with 100% similarity]
tests/worker-files/thread/longRunningWorkerSoftBehavior.js [moved from tests/worker/thread/longRunningWorkerSoftBehavior.js with 100% similarity]

diff --git a/.nycrc.json b/.nycrc.json
new file mode 100644 (file)
index 0000000..0d2fa01
--- /dev/null
@@ -0,0 +1,7 @@
+{
+  "check-coverage": true,
+  "lines": 90,
+  "statements": 90,
+  "functions": 89,
+  "branches": 85
+}
index d23919efeb0da43d7e260a0564390347f62c898d..4915f5e87589bf8e0822d0c5466baae28dc665b5 100644 (file)
@@ -38,10 +38,10 @@ const { DynamicThreadPool } = require('poolifier')
 For cluster and thread pools, you can now only send and receive serializable `JSON` data.  
 _This is not a limitation by poolifier but NodeJS._
 
-#### Public properties renaming
+### Public methods removed
 
-- Thread Pool's `numWorkers` is now `numberOfWorkers`
-- Thread Pool's `nextWorker` is now `nextWorkerIndex`
+`numWorkers` method removed
+`nextWorker` method removed
 
 #### Internal (protected) properties and methods renaming
 
index f8d933813a8b393692a124bbece9243aec244681..2ad7f22c5e47ec7827366ed3c95b21a3402c06cf 100644 (file)
@@ -11,13 +11,13 @@ const LIST_FORMATTER = new Intl.ListFormat('en-US', {
   type: 'conjunction'
 })
 
-// wait some seconds before start, my pools need to load threads !!!
+// Wait some seconds before start, pools need to load threads !!!
 setTimeout(async () => {
   test()
 }, 3000)
 
 async function test () {
-  // add tests
+  // Add tests
   suite
     .add('Pioardi:Static:ThreadPool', async function () {
       await fixedThreadTest()
@@ -31,7 +31,7 @@ async function test () {
     .add('Pioardi:Dynamic:ClusterPool', async function () {
       await dynamicClusterTest()
     })
-    // add listeners
+    // Add listeners
     .on('cycle', function (event) {
       console.log(event.target.toString())
     })
index 22e5a63017cb4adbe3902697fe64d944fa335161..ceef04b142b5045f4f7ad618ad2626c1547a483c 100644 (file)
@@ -13,8 +13,8 @@
     "test:debug": "npm run build && mocha -r source-map-support/register --inspect --exit --timeout 20000 'tests/**/*.test.js'",
     "test:prod": "npm run build:prod && nyc mocha -r source-map-support/register --exit --timeout 20000 'tests/**/*.test.js'",
     "sonar": "sonar-scanner",
-    "coverage": "nyc report --reporter=lcov --check-coverage --lines 80",
-    "coverage:html": "nyc report --reporter=html --check-coverage --lines 80",
+    "coverage": "nyc report --reporter=lcov",
+    "coverage:html": "nyc report --reporter=html",
     "format": "prettier --loglevel silent --write .; prettierx --write .",
     "lint": "eslint .",
     "lint:fix": "eslint . --fix"
index 5ef2eed9f39ed7dd83f0744e75d89725b490c7f3..4a6ba72f4b7523bd2d8870dd1108c6225814ff16 100644 (file)
@@ -155,27 +155,6 @@ export abstract class AbstractPool<
     this.emitter = new PoolEmitter()
   }
 
-  /**
-   * Number of workers that this pool should manage.
-   *
-   * @returns Number of workers that this pool manages.
-   * @deprecated Only here for backward compatibility.
-   */
-  // eslint-disable-next-line spellcheck/spell-checker
-  public get numWorkers (): number {
-    return this.numberOfWorkers
-  }
-
-  /**
-   * Index for the next worker.
-   *
-   * @returns Index for the next worker.
-   * @deprecated Only here for backward compatibility.
-   */
-  public get nextWorker (): number {
-    return this.nextWorkerIndex
-  }
-
   /**
    * Perform the task specified in the constructor with the data parameter.
    *
index 01b720720ac4761a768645b329d3cdb00a93283e..fbeb77cf3555c76ce1a93b53b926cee8f505ce91 100644 (file)
@@ -1,5 +1,6 @@
 const expect = require('expect')
 const { DynamicClusterPool } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
 const min = 1
 const max = 3
 const pool = new DynamicClusterPool(
@@ -20,7 +21,6 @@ describe('Dynamic cluster 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 closedWorkers = 0
     let fullPool = 0
     pool.emitter.on('FullPool', () => fullPool++)
     for (let i = 0; i < max * 2; i++) {
@@ -28,14 +28,9 @@ describe('Dynamic cluster pool test suite ', () => {
     }
     expect(pool.workers.length).toBeLessThanOrEqual(max)
     expect(pool.workers.length).toBeGreaterThan(min)
-    pool.workers.forEach(w => {
-      w.on('exit', () => {
-        closedWorkers++
-      })
-    })
     expect(fullPool > 1).toBeTruthy()
-    await new Promise(resolve => setTimeout(resolve, 5000))
-    expect(closedWorkers).toBe(max - min)
+    const numberOfExitEvents = await TestUtils.waitExits(pool, max - min)
+    expect(numberOfExitEvents).toBe(max - min)
   })
 
   it('Verify scale worker up and down is working', async () => {
@@ -44,37 +39,21 @@ describe('Dynamic cluster pool test suite ', () => {
       pool.execute({ test: 'test' })
     }
     expect(pool.workers.length).toBeGreaterThan(min)
-    await new Promise(resolve => setTimeout(resolve, 3000))
+    await TestUtils.waitExits(pool, max - min)
     expect(pool.workers.length).toBe(min)
     for (let i = 0; i < max * 10; i++) {
       pool.execute({ test: 'test' })
     }
     expect(pool.workers.length).toBeGreaterThan(min)
-    await new Promise(resolve => setTimeout(resolve, 3000))
+    await TestUtils.waitExits(pool, max - min)
     expect(pool.workers.length).toBe(min)
   })
-  it('Shutdown test', async () => {
-    let closedWorkers = 0
-    pool.workers.forEach(w => {
-      w.on('exit', () => {
-        closedWorkers++
-      })
-    })
-    pool.destroy()
-    await new Promise(resolve => setTimeout(resolve, 2000))
-    expect(closedWorkers).toBe(min)
-  })
 
-  it('Validations test', () => {
-    let error
-    try {
-      const pool1 = new DynamicClusterPool()
-      console.log(pool1)
-    } catch (e) {
-      error = e
-    }
-    expect(error).toBeTruthy()
-    expect(error.message).toBeTruthy()
+  it('Shutdown test', async () => {
+    const exitPromise = TestUtils.waitExits(pool, min)
+    await pool.destroy()
+    const res = await exitPromise
+    expect(res).toBe(min)
   })
 
   it('Should work even without opts in input', async () => {
@@ -91,14 +70,14 @@ describe('Dynamic cluster pool test suite ', () => {
     const longRunningPool = new DynamicClusterPool(
       min,
       max,
-      './tests/worker/cluster/longRunningWorkerHardBehavior.js'
+      './tests/worker-files/cluster/longRunningWorkerHardBehavior.js'
     )
     expect(longRunningPool.workers.length).toBe(min)
     for (let i = 0; i < max * 10; i++) {
       longRunningPool.execute({ test: 'test' })
     }
     expect(longRunningPool.workers.length).toBe(max)
-    await new Promise(resolve => setTimeout(resolve, 3000))
+    await TestUtils.waitExits(longRunningPool, max - min)
     // Here we expect the workers to be at the max size since that the task is still running
     expect(longRunningPool.workers.length).toBe(min)
   })
@@ -107,14 +86,14 @@ describe('Dynamic cluster pool test suite ', () => {
     const longRunningPool = new DynamicClusterPool(
       min,
       max,
-      './tests/worker/cluster/longRunningWorkerSoftBehavior.js'
+      './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js'
     )
     expect(longRunningPool.workers.length).toBe(min)
     for (let i = 0; i < max * 10; i++) {
       longRunningPool.execute({ test: 'test' })
     }
     expect(longRunningPool.workers.length).toBe(max)
-    await new Promise(resolve => setTimeout(resolve, 3000))
+    await TestUtils.sleep(1500)
     // Here we expect the workers to be at the max size since that the task is still running
     expect(longRunningPool.workers.length).toBe(max)
   })
index f5300eccf424c39028e340296b18d1d0492f45f4..08476e093d4b7051040ab84879152a869b254749 100644 (file)
@@ -1,5 +1,6 @@
 const expect = require('expect')
 const { FixedClusterPool } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
 const numberOfWorkers = 10
 const maxTasks = 500
 const pool = new FixedClusterPool(
@@ -115,27 +116,10 @@ describe('Fixed cluster pool test suite ', () => {
   })
 
   it('Shutdown test', async () => {
-    let closedWorkers = 0
-    pool.workers.forEach(w => {
-      w.on('exit', () => {
-        closedWorkers++
-      })
-    })
+    const exitPromise = TestUtils.waitExits(pool, numberOfWorkers)
     await pool.destroy()
-    await new Promise(resolve => setTimeout(resolve, 500))
-    expect(closedWorkers).toBe(numberOfWorkers)
-  })
-
-  it('Validations test', () => {
-    let error
-    try {
-      const pool1 = new FixedClusterPool()
-      console.log(pool1)
-    } catch (e) {
-      error = e
-    }
-    expect(error).toBeTruthy()
-    expect(error.message).toBeTruthy()
+    const res = await exitPromise
+    expect(res).toBe(numberOfWorkers)
   })
 
   it('Should work even without opts in input', async () => {
index 4efb0f4b83bdad12b0d96e83d6276ab773198eda..66aeaa3a20a9ebf870c67a2c8d99d5c46181f338 100644 (file)
@@ -1,5 +1,6 @@
 const expect = require('expect')
 const { DynamicThreadPool } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
 const min = 1
 const max = 3
 const pool = new DynamicThreadPool(
@@ -20,21 +21,15 @@ 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
     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)
-    pool.workers.forEach(w => {
-      w.on('exit', () => {
-        closedThreads++
-      })
-    })
     expect(fullPool > 1).toBeTruthy()
-    await new Promise(resolve => setTimeout(resolve, 2000))
-    expect(closedThreads).toBe(max - min)
+    const res = await TestUtils.waitExits(pool, max - min)
+    expect(res).toBe(max - min)
   })
 
   it('Verify scale thread up and down is working', async () => {
@@ -43,13 +38,13 @@ describe('Dynamic thread pool test suite ', () => {
       pool.execute({ test: 'test' })
     }
     expect(pool.workers.length).toBe(max)
-    await new Promise(resolve => setTimeout(resolve, 1000))
+    await TestUtils.waitExits(pool, max - min)
     expect(pool.workers.length).toBe(min)
     for (let i = 0; i < max * 10; i++) {
       pool.execute({ test: 'test' })
     }
     expect(pool.workers.length).toBe(max)
-    await new Promise(resolve => setTimeout(resolve, 1500))
+    await TestUtils.waitExits(pool, max - min)
     expect(pool.workers.length).toBe(min)
   })
 
@@ -65,15 +60,11 @@ describe('Dynamic thread pool test suite ', () => {
   })
 
   it('Validations test', () => {
-    let error
-    try {
+    expect(() => {
       const pool1 = new DynamicThreadPool()
-      console.log(pool1)
-    } catch (e) {
-      error = e
-    }
-    expect(error).toBeTruthy()
-    expect(error.message).toBeTruthy()
+    }).toThrowError(
+      new Error('Please specify a file with a worker implementation')
+    )
   })
 
   it('Should work even without opts in input', async () => {
@@ -90,7 +81,7 @@ describe('Dynamic thread pool test suite ', () => {
     const longRunningPool = new DynamicThreadPool(
       min,
       max,
-      './tests/worker/thread/longRunningWorkerHardBehavior.js',
+      './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
       {
         errorHandler: e => console.error(e),
         onlineHandler: () => console.log('worker is online')
@@ -101,7 +92,7 @@ describe('Dynamic thread pool test suite ', () => {
       longRunningPool.execute({ test: 'test' })
     }
     expect(longRunningPool.workers.length).toBe(max)
-    await new Promise(resolve => setTimeout(resolve, 1500))
+    await TestUtils.waitExits(longRunningPool, max - min)
     expect(longRunningPool.workers.length).toBe(min)
   })
 
@@ -109,7 +100,7 @@ describe('Dynamic thread pool test suite ', () => {
     const longRunningPool = new DynamicThreadPool(
       min,
       max,
-      './tests/worker/thread/longRunningWorkerSoftBehavior.js',
+      './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
       {
         errorHandler: e => console.error(e),
         onlineHandler: () => console.log('worker is online')
@@ -120,7 +111,7 @@ describe('Dynamic thread pool test suite ', () => {
       longRunningPool.execute({ test: 'test' })
     }
     expect(longRunningPool.workers.length).toBe(max)
-    await new Promise(resolve => setTimeout(resolve, 1500))
+    await TestUtils.sleep(1500)
     // Here we expect the workers to be at the max size since that the task is still running
     expect(longRunningPool.workers.length).toBe(max)
   })
index 755ccf4d4fa2aa12b0621344d1010af377431248..2a7d005b3dcd6d88a53e4e53e1d2216c55483de4 100644 (file)
@@ -1,5 +1,6 @@
 const expect = require('expect')
 const { FixedThreadPool } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
 const numberOfThreads = 10
 const maxTasks = 400
 const pool = new FixedThreadPool(
@@ -93,26 +94,10 @@ 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 () => {
diff --git a/tests/test-utils.js b/tests/test-utils.js
new file mode 100644 (file)
index 0000000..690f4b3
--- /dev/null
@@ -0,0 +1,21 @@
+class TestUtils {
+  static async waitExits (pool, numberOfExitEventsToWait) {
+    let exitEvents = 0
+    return new Promise(function (resolve, reject) {
+      pool.workers.forEach(w => {
+        w.on('exit', () => {
+          exitEvents++
+          if (exitEvents === numberOfExitEventsToWait) {
+            resolve(exitEvents)
+          }
+        })
+      })
+    })
+  }
+
+  static async sleep (ms) {
+    return new Promise(resolve => setTimeout(resolve, ms))
+  }
+}
+
+module.exports = TestUtils
index 054c4bb30c2d8ef2ad2ed9dd52bc5c6f9af793e2..9bcae28163bf475972f334623505e4a23531ed64 100644 (file)
@@ -6,6 +6,5 @@ function echo (data) {
 }
 
 module.exports = new ClusterWorker(echo, {
-  maxInactiveTime: 500,
   killBehavior: KillBehaviors.HARD
 })
index 071428c5bc5f1414b5e61718ccf605826480bf45..cfe9427bdf6fa44c684a5861e18d81d1905aae42 100644 (file)
@@ -6,6 +6,5 @@ function echo (data) {
 }
 
 module.exports = new ThreadWorker(echo, {
-  maxInactiveTime: 500,
   killBehavior: KillBehaviors.HARD
 })