From: Jérôme Benoit Date: Mon, 22 Feb 2021 18:48:46 +0000 (+0100) Subject: Improve worker selection strategies coverage. (#220) X-Git-Tag: v2.0.0~23 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b98ec2e690d5ba33a13a2848b0f1315b885c2f4b;p=poolifier.git Improve worker selection strategies coverage. (#220) * Improve worker selection strategies coverage. Signed-off-by: Jérôme Benoit * Dedupe packages. Signed-off-by: Jérôme Benoit --- diff --git a/package-lock.json b/package-lock.json index cb4eb615..3596c88e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -657,49 +657,6 @@ "@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": { diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index f1ac76ed..38187edc 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -210,6 +210,7 @@ export abstract class AbstractPool< public setWorkerChoiceStrategy ( workerChoiceStrategy: WorkerChoiceStrategy ): void { + this.opts.workerChoiceStrategy = workerChoiceStrategy this.workerChoiceStrategyContext.setWorkerChoiceStrategy( workerChoiceStrategy ) diff --git a/tests/pools/selection-strategies.test.js b/tests/pools/selection-strategies.test.js index 01636b67..0c5e4611 100644 --- a/tests/pools/selection-strategies.test.js +++ b/tests/pools/selection-strategies.test.js @@ -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++) {