From f931db5c2405593ceeef6e3ea86c784e6cf82165 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 13 Oct 2022 22:19:44 +0200 Subject: [PATCH] Improve tests coverage on worker choice strategies MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- package-lock.json | 14 +++++++------- package.json | 2 +- ...hted-round-robin-worker-choice-strategy.test.js | 8 +++++++- tests/test-utils.js | 9 +++++++++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1f1d18d6..5d7576b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,7 @@ "eslint-plugin-n": "^15.3.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-prettierx": "^0.18.0", - "eslint-plugin-promise": "^6.0.1", + "eslint-plugin-promise": "^6.1.0", "eslint-plugin-spellcheck": "^0.0.19", "expect": "^29.1.2", "husky": "^8.0.1", @@ -2855,9 +2855,9 @@ } }, "node_modules/eslint-plugin-promise": { - "version": "6.0.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.0.1.tgz", - "integrity": "sha512-uM4Tgo5u3UWQiroOyDEsYcVMOo7re3zmno0IZmB5auxoaQNIceAbXEkSt8RNrKtaYehARHG06pYK6K1JhtP0Zw==", + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.1.0.tgz", + "integrity": "sha512-NYCfDZF/KHt27p06nFAttgWuFyIDSUMnNaJBIY1FY9GpBFhdT2vMG64HlFguSgcJeyM5by6Yr5csSOuJm60eXQ==", "dev": true, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" @@ -10568,9 +10568,9 @@ } }, "eslint-plugin-promise": { - "version": "6.0.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.0.1.tgz", - "integrity": "sha512-uM4Tgo5u3UWQiroOyDEsYcVMOo7re3zmno0IZmB5auxoaQNIceAbXEkSt8RNrKtaYehARHG06pYK6K1JhtP0Zw==", + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.1.0.tgz", + "integrity": "sha512-NYCfDZF/KHt27p06nFAttgWuFyIDSUMnNaJBIY1FY9GpBFhdT2vMG64HlFguSgcJeyM5by6Yr5csSOuJm60eXQ==", "dev": true }, "eslint-plugin-spellcheck": { diff --git a/package.json b/package.json index a425e783..7ede58c3 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "eslint-plugin-n": "^15.3.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-prettierx": "^0.18.0", - "eslint-plugin-promise": "^6.0.1", + "eslint-plugin-promise": "^6.1.0", "eslint-plugin-spellcheck": "^0.0.19", "expect": "^29.1.2", "husky": "^8.0.1", diff --git a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js index 24246595..41c7751c 100644 --- a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js +++ b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js @@ -4,6 +4,7 @@ const { FixedThreadPool } = require('../../../lib/index') const { WeightedRoundRobinWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy') +const TestUtils = require('../../test-utils') describe('Weighted round robin strategy worker choice strategy test suite', () => { // const min = 1 @@ -24,6 +25,12 @@ describe('Weighted round robin strategy worker choice strategy test suite', () = it('Verify that reset() resets internals', () => { const strategy = new WeightedRoundRobinWorkerChoiceStrategy(pool) + strategy.previousWorkerIndex = TestUtils.generateRandomInteger( + Number.MAX_SAFE_INTEGER + ) + strategy.currentWorkerIndex = TestUtils.generateRandomInteger( + Number.MAX_SAFE_INTEGER + ) const workersTaskRunTimeClearStub = sinon .stub(strategy.workersTaskRunTime, 'clear') .returns() @@ -34,7 +41,6 @@ describe('Weighted round robin strategy worker choice strategy test suite', () = expect(resetResult).toBe(true) expect(strategy.previousWorkerIndex).toBe(0) expect(strategy.currentWorkerIndex).toBe(0) - expect(strategy.defaultWorkerWeight).toBeGreaterThan(0) expect(workersTaskRunTimeClearStub.calledOnce).toBe(true) expect(initWorkersTaskRunTimeStub.calledOnce).toBe(true) }) diff --git a/tests/test-utils.js b/tests/test-utils.js index 78218e26..a3affb61 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -36,6 +36,15 @@ class TestUtils { }) } + static generateRandomInteger (max, min = 0) { + max = Math.floor(max) + if (min) { + min = Math.ceil(min) + return Math.floor(Math.random() * (max - min + 1)) + min + } + return Math.floor(Math.random() * (max + 1)) + } + static jsonIntegerSerialization (n) { for (let i = 0; i < n; i++) { const o = { -- 2.34.1