Improve worker selection strategies coverage. (#220)
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 22 Feb 2021 18:48:46 +0000 (19:48 +0100)
committerGitHub <noreply@github.com>
Mon, 22 Feb 2021 18:48:46 +0000 (19:48 +0100)
* Improve worker selection strategies coverage.

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

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
package-lock.json
src/pools/abstract-pool.ts
tests/pools/selection-strategies.test.js

index cb4eb615b9506cabfbc981123e1c5c63d0b35071..3596c88e476bd5d44938ba29ab5050f97ef8f119 100644 (file)
         "@typescript-eslint/types": "4.15.2",
         "@typescript-eslint/typescript-estree": "4.15.2",
         "debug": "^4.1.1"
-      },
-      "dependencies": {
-        "@typescript-eslint/scope-manager": {
-          "version": "4.15.2",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-4.15.2.tgz",
-          "integrity": "sha512-Zm0tf/MSKuX6aeJmuXexgdVyxT9/oJJhaCkijv0DvJVT3ui4zY6XYd6iwIo/8GEZGy43cd7w1rFMiCLHbRzAPQ==",
-          "dev": true,
-          "requires": {
-            "@typescript-eslint/types": "4.15.2",
-            "@typescript-eslint/visitor-keys": "4.15.2"
-          }
-        },
-        "@typescript-eslint/types": {
-          "version": "4.15.2",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-4.15.2.tgz",
-          "integrity": "sha512-r7lW7HFkAarfUylJ2tKndyO9njwSyoy6cpfDKWPX6/ctZA+QyaYscAHXVAfJqtnY6aaTwDYrOhp+ginlbc7HfQ==",
-          "dev": true
-        },
-        "@typescript-eslint/typescript-estree": {
-          "version": "4.15.2",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-4.15.2.tgz",
-          "integrity": "sha512-cGR8C2g5SPtHTQvAymEODeqx90pJHadWsgTtx6GbnTWKqsg7yp6Eaya9nFzUd4KrKhxdYTTFBiYeTPQaz/l8bw==",
-          "dev": true,
-          "requires": {
-            "@typescript-eslint/types": "4.15.2",
-            "@typescript-eslint/visitor-keys": "4.15.2",
-            "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.15.2",
-          "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-4.15.2.tgz",
-          "integrity": "sha512-TME1VgSb7wTwgENN5KVj4Nqg25hP8DisXxNBojM4Nn31rYaNDIocNm5cmjOFfh42n7NVERxWrDFoETO/76ePyg==",
-          "dev": true,
-          "requires": {
-            "@typescript-eslint/types": "4.15.2",
-            "eslint-visitor-keys": "^2.0.0"
-          }
-        }
       }
     },
     "@typescript-eslint/scope-manager": {
index f1ac76ed587b8059c87430edcf1bddea18264577..38187edc41b462fc9944744f3cfbce16d9c595e4 100644 (file)
@@ -210,6 +210,7 @@ export abstract class AbstractPool<
   public setWorkerChoiceStrategy (
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
+    this.opts.workerChoiceStrategy = workerChoiceStrategy
     this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
       workerChoiceStrategy
     )
index 01636b675cd76f53f78152a9f750531c4f8b32af..0c5e461155d0820fed1de17f07381a5eecb64078 100644 (file)
@@ -11,18 +11,41 @@ describe('Selection strategies test suite', () => {
     expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED')
   })
 
-  it('Verify LESS_RECENTLY_USED strategy is taken', async () => {
+  it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
     const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
       { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
     )
+    expect(pool.opts.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.LESS_RECENTLY_USED
+    )
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
 
+  it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
+    const max = 3
+    const pool = new FixedThreadPool(
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_RECENTLY_USED
     )
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
 
+  it('Verify LESS_RECENTLY_USED strategy can be run in a pool', async () => {
+    const max = 3
+    const pool = new FixedThreadPool(
+      max,
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
+    )
     // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {