Add two tests to assess worker options. (#260)
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 9 Mar 2021 09:32:35 +0000 (10:32 +0100)
committerGitHub <noreply@github.com>
Tue, 9 Mar 2021 09:32:35 +0000 (10:32 +0100)
package-lock.json
tests/pools/abstract/abstract-pool.test.js
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/worker/abstract-worker.test.js

index 9ab91708838b0b6f4bc90b8ab9f907a85199e589..e568ee60edf7b8024a4f6cfe4f3de429dcc6e2b5 100644 (file)
         "regexpp": "^3.0.0",
         "semver": "^7.3.2",
         "tsutils": "^3.17.1"
-      },
-      "dependencies": {
-        "@typescript-eslint/scope-manager": {
-          "version": "4.17.0",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-4.17.0.tgz",
-          "integrity": "sha512-OJ+CeTliuW+UZ9qgULrnGpPQ1bhrZNFpfT/Bc0pzNeyZwMik7/ykJ0JHnQ7krHanFN9wcnPK89pwn84cRUmYjw==",
-          "dev": true,
-          "requires": {
-            "@typescript-eslint/types": "4.17.0",
-            "@typescript-eslint/visitor-keys": "4.17.0"
-          }
-        },
-        "@typescript-eslint/types": {
-          "version": "4.17.0",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-4.17.0.tgz",
-          "integrity": "sha512-RN5z8qYpJ+kXwnLlyzZkiJwfW2AY458Bf8WqllkondQIcN2ZxQowAToGSd9BlAUZDB5Ea8I6mqL2quGYCLT+2g==",
-          "dev": true
-        },
-        "@typescript-eslint/visitor-keys": {
-          "version": "4.17.0",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-4.17.0.tgz",
-          "integrity": "sha512-WfuMN8mm5SSqXuAr9NM+fItJ0SVVphobWYkWOwQ1odsfC014Vdxk/92t4JwS1Q6fCA/ABfCKpa3AVtpUKTNKGQ==",
-          "dev": true,
-          "requires": {
-            "@typescript-eslint/types": "4.17.0",
-            "eslint-visitor-keys": "^2.0.0"
-          }
-        }
       }
     },
     "@typescript-eslint/experimental-utils": {
         "@typescript-eslint/typescript-estree": "4.17.0",
         "eslint-scope": "^5.0.0",
         "eslint-utils": "^2.0.0"
-      },
-      "dependencies": {
-        "@typescript-eslint/scope-manager": {
-          "version": "4.17.0",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-4.17.0.tgz",
-          "integrity": "sha512-OJ+CeTliuW+UZ9qgULrnGpPQ1bhrZNFpfT/Bc0pzNeyZwMik7/ykJ0JHnQ7krHanFN9wcnPK89pwn84cRUmYjw==",
-          "dev": true,
-          "requires": {
-            "@typescript-eslint/types": "4.17.0",
-            "@typescript-eslint/visitor-keys": "4.17.0"
-          }
-        },
-        "@typescript-eslint/types": {
-          "version": "4.17.0",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-4.17.0.tgz",
-          "integrity": "sha512-RN5z8qYpJ+kXwnLlyzZkiJwfW2AY458Bf8WqllkondQIcN2ZxQowAToGSd9BlAUZDB5Ea8I6mqL2quGYCLT+2g==",
-          "dev": true
-        },
-        "@typescript-eslint/typescript-estree": {
-          "version": "4.17.0",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-4.17.0.tgz",
-          "integrity": "sha512-lRhSFIZKUEPPWpWfwuZBH9trYIEJSI0vYsrxbvVvNyIUDoKWaklOAelsSkeh3E2VBSZiNe9BZ4E5tYBZbUczVQ==",
-          "dev": true,
-          "requires": {
-            "@typescript-eslint/types": "4.17.0",
-            "@typescript-eslint/visitor-keys": "4.17.0",
-            "debug": "^4.1.1",
-            "globby": "^11.0.1",
-            "is-glob": "^4.0.1",
-            "semver": "^7.3.2",
-            "tsutils": "^3.17.1"
-          }
-        },
-        "@typescript-eslint/visitor-keys": {
-          "version": "4.17.0",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-4.17.0.tgz",
-          "integrity": "sha512-WfuMN8mm5SSqXuAr9NM+fItJ0SVVphobWYkWOwQ1odsfC014Vdxk/92t4JwS1Q6fCA/ABfCKpa3AVtpUKTNKGQ==",
-          "dev": true,
-          "requires": {
-            "@typescript-eslint/types": "4.17.0",
-            "eslint-visitor-keys": "^2.0.0"
-          }
-        }
       }
     },
     "@typescript-eslint/parser": {
index a2170491243313c13c65c36f8222eb5324e19eb9..bb6a2e4a89037e84bc99a3bccd14e675894a9a14 100644 (file)
@@ -104,7 +104,7 @@ describe('Abstract pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js'
     )
-    expect(pool.opts.enableEvents).toEqual(true)
+    expect(pool.opts.enableEvents).toBe(true)
     expect(pool.emitter).toBeDefined()
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
@@ -118,7 +118,7 @@ describe('Abstract pool test suite', () => {
         enableEvents: false
       }
     )
-    expect(pool.opts.enableEvents).toEqual(false)
+    expect(pool.opts.enableEvents).toBe(false)
     expect(pool.emitter).toBeUndefined()
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_RECENTLY_USED
@@ -137,7 +137,7 @@ describe('Abstract pool test suite', () => {
     for (let i = 0; i < numberOfWorkers * 2; i++) {
       promises.push(pool.execute({ test: 'test' }))
     }
-    expect(poolBusy).toEqual(numberOfWorkers)
+    expect(poolBusy).toBe(numberOfWorkers)
     pool.destroy()
   })
 })
index 9e610d65ace3ab0f8e38b734cfeadfd38a4dffa2..9a39c1d62925251e2b0d7727047ac8a3d6982003 100644 (file)
@@ -28,7 +28,7 @@ describe('Dynamic cluster pool test suite', () => {
     }
     expect(pool.workers.length).toBeLessThanOrEqual(max)
     expect(pool.workers.length).toBeGreaterThan(min)
-    expect(poolBusy).toEqual(max + 1)
+    expect(poolBusy).toBe(max + 1)
     const numberOfExitEvents = await TestUtils.waitExits(pool, max - min)
     expect(numberOfExitEvents).toBe(max - min)
   })
@@ -69,6 +69,7 @@ describe('Dynamic cluster pool test suite', () => {
       './tests/worker-files/cluster/testWorker.js'
     )
     const result = await pool1.execute({ test: 'test' })
+    expect(result).toBeDefined()
     expect(result).toBeFalsy()
     // We need to clean up the resources after our test
     await pool1.destroy()
index 04467620f7d50c61b8813af25ecd011b6d145e8e..7048bb7937946108777aa8faa65fe569031bc98e 100644 (file)
@@ -74,7 +74,7 @@ describe('Fixed cluster pool test suite', () => {
     for (let i = 0; i < numberOfWorkers * 2; i++) {
       promises.push(pool.execute({ test: 'test' }))
     }
-    expect(poolBusy).toEqual(numberOfWorkers)
+    expect(poolBusy).toBe(numberOfWorkers)
   })
 
   it('Verify that is possible to have a worker that return undefined', async () => {
@@ -98,7 +98,7 @@ describe('Fixed cluster pool test suite', () => {
       inError = e
     }
     expect(inError).toBeDefined()
-    expect(typeof inError === 'string').toEqual(true)
+    expect(typeof inError === 'string').toBe(true)
     expect(inError).toBe('Error Message from ClusterWorker')
   })
 
@@ -111,7 +111,7 @@ describe('Fixed cluster pool test suite', () => {
       inError = e
     }
     expect(inError).toBeDefined()
-    expect(typeof inError === 'string').toEqual(true)
+    expect(typeof inError === 'string').toBe(true)
     expect(inError).toBe('Error Message from ClusterWorker:async')
   })
 
index 31d64727707d3bc4c31b14501fbe4b41e81aa2fb..9cba566f916abccdd380b38a0795a5502992a933 100644 (file)
@@ -27,7 +27,7 @@ describe('Dynamic thread pool test suite', () => {
       promises.push(pool.execute({ test: 'test' }))
     }
     expect(pool.workers.length).toBe(max)
-    expect(poolBusy).toEqual(max + 1)
+    expect(poolBusy).toBe(max + 1)
     const res = await TestUtils.waitExits(pool, max - min)
     expect(res).toBe(max - min)
   })
@@ -72,6 +72,7 @@ describe('Dynamic thread pool test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     const res = await pool1.execute({ test: 'test' })
+    expect(res).toBeDefined()
     expect(res).toBeFalsy()
     // We need to clean up the resources after our test
     await pool1.destroy()
index 09a58e2df5cafd27569842ae945373ea1a5b017f..81ef967ec5e0540abdd94a697b642b3e08c69b3e 100644 (file)
@@ -74,7 +74,7 @@ describe('Fixed thread pool test suite', () => {
     for (let i = 0; i < numberOfThreads * 2; i++) {
       promises.push(pool.execute({ test: 'test' }))
     }
-    expect(poolBusy).toEqual(numberOfThreads)
+    expect(poolBusy).toBe(numberOfThreads)
   })
 
   it('Verify that is possible to have a worker that return undefined', async () => {
@@ -100,7 +100,7 @@ describe('Fixed thread pool test suite', () => {
     expect(inError).toBeDefined()
     expect(inError).toBeInstanceOf(Error)
     expect(inError.message).toBeDefined()
-    expect(typeof inError.message === 'string').toEqual(true)
+    expect(typeof inError.message === 'string').toBe(true)
   })
 
   it('Verify that error handling is working properly:async', async () => {
@@ -114,7 +114,7 @@ describe('Fixed thread pool test suite', () => {
     expect(inError).toBeDefined()
     expect(inError).toBeInstanceOf(Error)
     expect(inError.message).toBeDefined()
-    expect(typeof inError.message === 'string').toEqual(true)
+    expect(typeof inError.message === 'string').toBe(true)
   })
 
   it('Verify that async function is working properly', async () => {
index a22d1c9ff0bc8b2db5ca58ca2e634ed1cade7c62..9b4864c77dda745d3d162b833875b6709a38b6d9 100644 (file)
@@ -1,5 +1,5 @@
 const expect = require('expect')
-const { ClusterWorker, ThreadWorker } = require('../../lib')
+const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
 
 class StubPoolWithIsMainWorker extends ThreadWorker {
   constructor (fn, opts) {
@@ -15,6 +15,24 @@ describe('Abstract worker test suite', () => {
     )
   })
 
+  it('Verify worker default values', () => {
+    const worker = new ThreadWorker(() => {})
+    expect(worker.maxInactiveTime).toBe(1000 * 60)
+    expect(worker.killBehavior).toBe(KillBehaviors.SOFT)
+    expect(worker.async).toBe(false)
+  })
+
+  it('Verify that worker options are set at worker creation', () => {
+    const worker = new ClusterWorker(() => {}, {
+      maxInactiveTime: 6000,
+      async: true,
+      killBehavior: KillBehaviors.HARD
+    })
+    expect(worker.maxInactiveTime).toBe(6000)
+    expect(worker.killBehavior).toBe(KillBehaviors.HARD)
+    expect(worker.async).toBe(true)
+  })
+
   it('Verify that handleError function is working properly', () => {
     const error = new Error('My error')
     const worker = new ThreadWorker(() => {})