From: Jérôme Benoit Date: Sat, 8 Oct 2022 12:51:04 +0000 (+0200) Subject: Factor out some UTs code in test-utils.js X-Git-Tag: v2.2.1~11 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=15d563152134452a245e4842b20fbf4030457e16;p=poolifier.git Factor out some UTs code in test-utils.js Signed-off-by: Jérôme Benoit --- diff --git a/tests/pools/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js similarity index 99% rename from tests/pools/selection-strategies.test.js rename to tests/pools/selection-strategies/selection-strategies.test.js index c554ab14..6448c667 100644 --- a/tests/pools/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -3,7 +3,7 @@ const { WorkerChoiceStrategies, DynamicThreadPool, FixedThreadPool -} = require('../../lib/index') +} = require('../../../lib/index') describe('Selection strategies test suite', () => { it('Verify that WorkerChoiceStrategies enumeration provides string values', () => { diff --git a/tests/test-utils.js b/tests/test-utils.js index a34cd788..ab57685e 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -17,9 +17,20 @@ class TestUtils { return new Promise(resolve => setTimeout(resolve, ms)) } - static async workerSleepFunction (data, ms) { - return new Promise(resolve => { - setTimeout(() => resolve(data), ms) + static async workerSleepFunction ( + data, + ms, + rejection = false, + rejectionMessage = '' + ) { + return new Promise((resolve, reject) => { + setTimeout( + () => + rejection === true + ? reject(new Error(rejectionMessage)) + : resolve(data), + ms + ) }) } diff --git a/tests/worker-files/cluster/asyncErrorWorker.js b/tests/worker-files/cluster/asyncErrorWorker.js index b9fd9016..0b6f5d8a 100644 --- a/tests/worker-files/cluster/asyncErrorWorker.js +++ b/tests/worker-files/cluster/asyncErrorWorker.js @@ -1,13 +1,14 @@ 'use strict' const { ClusterWorker, KillBehaviors } = require('../../../lib/index') +const TestUtils = require('../../test-utils') async function error (data) { - return new Promise((resolve, reject) => { - setTimeout( - () => reject(new Error('Error Message from ClusterWorker:async')), - 2000 - ) - }) + return TestUtils.workerSleepFunction( + data, + 2000, + true, + 'Error Message from ClusterWorker:async' + ) } module.exports = new ClusterWorker(error, { diff --git a/tests/worker-files/thread/asyncErrorWorker.js b/tests/worker-files/thread/asyncErrorWorker.js index d973f0a4..39f4055d 100644 --- a/tests/worker-files/thread/asyncErrorWorker.js +++ b/tests/worker-files/thread/asyncErrorWorker.js @@ -1,10 +1,14 @@ 'use strict' const { ThreadWorker, KillBehaviors } = require('../../../lib/index') +const TestUtils = require('../../test-utils') async function error (data) { - return new Promise((resolve, reject) => { - setTimeout(() => reject(new Error(data)), 2000) - }) + return TestUtils.workerSleepFunction( + data, + 2000, + true, + 'Error Message from ThreadWorker:async' + ) } module.exports = new ThreadWorker(error, { diff --git a/tests/worker-files/thread/errorWorker.js b/tests/worker-files/thread/errorWorker.js index cc13c050..841af907 100644 --- a/tests/worker-files/thread/errorWorker.js +++ b/tests/worker-files/thread/errorWorker.js @@ -2,7 +2,7 @@ const { ThreadWorker, KillBehaviors } = require('../../../lib/index') function error (data) { - throw new Error(data) + throw new Error('Error Message from ThreadWorker') } module.exports = new ThreadWorker(error, {