From: Jérôme Benoit Date: Mon, 10 Oct 2022 15:23:31 +0000 (+0200) Subject: Cleanup tests code X-Git-Tag: v2.3.1~40 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=e1ffb94fcac39a7459c47edaa086a7729a1dec44;p=poolifier.git Cleanup tests code Signed-off-by: Jérôme Benoit --- diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 54b32ec0..96dc9a10 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -4,22 +4,23 @@ const { FixedThreadPool, WorkerChoiceStrategies } = require('../../../lib/index') -const numberOfWorkers = 1 -const workerNotFoundInTasksUsageMapError = new Error( - 'Worker could not be found in worker tasks usage map' -) -class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool { - removeAllWorker () { - this.workersTasksUsage.clear() + +describe('Abstract pool test suite', () => { + const numberOfWorkers = 1 + const workerNotFoundInTasksUsageMapError = new Error( + 'Worker could not be found in worker tasks usage map' + ) + class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool { + removeAllWorker () { + this.workersTasksUsage.clear() + } } -} -class StubPoolWithIsMainMethod extends FixedThreadPool { - isMain () { - return false + class StubPoolWithIsMainMethod extends FixedThreadPool { + isMain () { + return false + } } -} -describe('Abstract pool test suite', () => { it('Simulate pool creation from a non main thread/process', () => { expect( () => diff --git a/tests/pools/cluster/dynamic.test.js b/tests/pools/cluster/dynamic.test.js index bd7a0672..6ce3d0a1 100644 --- a/tests/pools/cluster/dynamic.test.js +++ b/tests/pools/cluster/dynamic.test.js @@ -2,18 +2,19 @@ const { expect } = require('expect') const { DynamicClusterPool } = require('../../../lib/index') const WorkerFunctions = require('../../test-types') const TestUtils = require('../../test-utils') -const min = 1 -const max = 3 -const pool = new DynamicClusterPool( - min, - max, - './tests/worker-files/cluster/testWorker.js', - { - errorHandler: e => console.error(e) - } -) describe('Dynamic cluster pool test suite', () => { + const min = 1 + const max = 3 + const pool = new DynamicClusterPool( + min, + max, + './tests/worker-files/cluster/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + it('Verify that the function is executed in a worker cluster', async () => { let result = await pool.execute({ function: WorkerFunctions.fibonacci @@ -72,8 +73,8 @@ describe('Dynamic cluster pool test suite', () => { it('Should work even without opts in input', async () => { const pool1 = new DynamicClusterPool( - 1, - 1, + min, + max, './tests/worker-files/cluster/testWorker.js' ) const result = await pool1.execute() diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 5bc9d77e..b1005595 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -2,43 +2,44 @@ const { expect } = require('expect') const { FixedClusterPool } = require('../../../lib/index') const WorkerFunctions = require('../../test-types') const TestUtils = require('../../test-utils') -const numberOfWorkers = 10 -const pool = new FixedClusterPool( - numberOfWorkers, - './tests/worker-files/cluster/testWorker.js', - { - errorHandler: e => console.error(e) - } -) -const emptyPool = new FixedClusterPool( - 1, - './tests/worker-files/cluster/emptyWorker.js', - { exitHandler: () => console.log('empty pool worker exited') } -) -const echoPool = new FixedClusterPool( - 1, - './tests/worker-files/cluster/echoWorker.js' -) -const errorPool = new FixedClusterPool( - 1, - './tests/worker-files/cluster/errorWorker.js', - { - errorHandler: e => console.error(e) - } -) -const asyncErrorPool = new FixedClusterPool( - 1, - './tests/worker-files/cluster/asyncErrorWorker.js', - { - errorHandler: e => console.error(e) - } -) -const asyncPool = new FixedClusterPool( - 1, - './tests/worker-files/cluster/asyncWorker.js' -) describe('Fixed cluster pool test suite', () => { + const numberOfWorkers = 6 + const pool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + const emptyPool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/emptyWorker.js', + { exitHandler: () => console.log('empty pool worker exited') } + ) + const echoPool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/echoWorker.js' + ) + const errorPool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/errorWorker.js', + { + errorHandler: e => console.error(e) + } + ) + const asyncErrorPool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/asyncErrorWorker.js', + { + errorHandler: e => console.error(e) + } + ) + const asyncPool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/asyncWorker.js' + ) + after('Destroy all pools', async () => { // We need to clean up the resources after our test await echoPool.destroy() @@ -92,7 +93,7 @@ describe('Fixed cluster pool test suite', () => { it('Verify that data are sent to the worker correctly', async () => { const data = { f: 10 } const result = await echoPool.execute(data) - expect(result).toEqual(data) + expect(result).toStrictEqual(data) }) it('Verify that error handling is working properly:sync', async () => { @@ -126,7 +127,7 @@ describe('Fixed cluster pool test suite', () => { const startTime = new Date().getTime() const result = await asyncPool.execute(data) const usedTime = new Date().getTime() - startTime - expect(result).toEqual(data) + expect(result).toStrictEqual(data) expect(usedTime).toBeGreaterThanOrEqual(2000) }) @@ -139,7 +140,7 @@ describe('Fixed cluster pool test suite', () => { it('Should work even without opts in input', async () => { const pool1 = new FixedClusterPool( - 1, + numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) const res = await pool1.execute() diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index bc6d0b32..a2365a5d 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -6,6 +6,9 @@ const { } = require('../../../lib/index') describe('Selection strategies test suite', () => { + const min = 0 + const max = 3 + it('Verify that WorkerChoiceStrategies enumeration provides string values', () => { expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN') expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED') @@ -16,8 +19,6 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -31,8 +32,6 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -47,8 +46,6 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { - const min = 0 - const max = 3 let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -73,7 +70,6 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -93,8 +89,6 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -115,7 +109,6 @@ describe('Selection strategies test suite', () => { }) 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', @@ -129,7 +122,6 @@ describe('Selection strategies test suite', () => { }) 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' @@ -143,8 +135,6 @@ describe('Selection strategies test suite', () => { }) it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => { - const min = 0 - const max = 3 let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -169,7 +159,6 @@ describe('Selection strategies test suite', () => { }) it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -186,8 +175,6 @@ describe('Selection strategies test suite', () => { }) it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -205,7 +192,6 @@ describe('Selection strategies test suite', () => { }) it('Verify FAIR_SHARE strategy is taken at pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -219,7 +205,6 @@ describe('Selection strategies test suite', () => { }) it('Verify FAIR_SHARE strategy can be set after pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -233,8 +218,6 @@ describe('Selection strategies test suite', () => { }) it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => { - const min = 0 - const max = 3 let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -259,7 +242,6 @@ describe('Selection strategies test suite', () => { }) it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -276,8 +258,6 @@ describe('Selection strategies test suite', () => { }) it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -295,7 +275,6 @@ describe('Selection strategies test suite', () => { }) it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -309,7 +288,6 @@ describe('Selection strategies test suite', () => { }) it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -323,8 +301,6 @@ describe('Selection strategies test suite', () => { }) it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { - const min = 0 - const max = 3 let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -349,7 +325,6 @@ describe('Selection strategies test suite', () => { }) it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -366,8 +341,6 @@ describe('Selection strategies test suite', () => { }) it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -385,8 +358,6 @@ describe('Selection strategies test suite', () => { }) it('Verify unknown strategies throw error', () => { - const min = 1 - const max = 3 expect( () => new DynamicThreadPool( diff --git a/tests/pools/thread/dynamic.test.js b/tests/pools/thread/dynamic.test.js index 5a85f6ae..8702dee8 100644 --- a/tests/pools/thread/dynamic.test.js +++ b/tests/pools/thread/dynamic.test.js @@ -2,18 +2,19 @@ const { expect } = require('expect') const { DynamicThreadPool } = require('../../../lib/index') const WorkerFunctions = require('../../test-types') const TestUtils = require('../../test-utils') -const min = 1 -const max = 3 -const pool = new DynamicThreadPool( - min, - max, - './tests/worker-files/thread/testWorker.js', - { - errorHandler: e => console.error(e) - } -) describe('Dynamic thread pool test suite', () => { + const min = 1 + const max = 3 + const pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + it('Verify that the function is executed in a worker thread', async () => { let result = await pool.execute({ function: WorkerFunctions.fibonacci @@ -72,8 +73,8 @@ describe('Dynamic thread pool test suite', () => { it('Should work even without opts in input', async () => { const pool1 = new DynamicThreadPool( - 1, - 1, + min, + max, './tests/worker-files/thread/testWorker.js' ) const res = await pool1.execute() diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 8a2b90c2..5bbc8e0e 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -2,43 +2,44 @@ const { expect } = require('expect') const { FixedThreadPool } = require('../../../lib/index') const WorkerFunctions = require('../../test-types') const TestUtils = require('../../test-utils') -const numberOfThreads = 10 -const pool = new FixedThreadPool( - numberOfThreads, - './tests/worker-files/thread/testWorker.js', - { - errorHandler: e => console.error(e) - } -) -const emptyPool = new FixedThreadPool( - 1, - './tests/worker-files/thread/emptyWorker.js', - { exitHandler: () => console.log('empty pool worker exited') } -) -const echoPool = new FixedThreadPool( - 1, - './tests/worker-files/thread/echoWorker.js' -) -const errorPool = new FixedThreadPool( - 1, - './tests/worker-files/thread/errorWorker.js', - { - errorHandler: e => console.error(e) - } -) -const asyncErrorPool = new FixedThreadPool( - 1, - './tests/worker-files/thread/asyncErrorWorker.js', - { - errorHandler: e => console.error(e) - } -) -const asyncPool = new FixedThreadPool( - 1, - './tests/worker-files/thread/asyncWorker.js' -) describe('Fixed thread pool test suite', () => { + const numberOfThreads = 6 + const pool = new FixedThreadPool( + numberOfThreads, + './tests/worker-files/thread/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + const emptyPool = new FixedThreadPool( + numberOfThreads, + './tests/worker-files/thread/emptyWorker.js', + { exitHandler: () => console.log('empty pool worker exited') } + ) + const echoPool = new FixedThreadPool( + numberOfThreads, + './tests/worker-files/thread/echoWorker.js' + ) + const errorPool = new FixedThreadPool( + numberOfThreads, + './tests/worker-files/thread/errorWorker.js', + { + errorHandler: e => console.error(e) + } + ) + const asyncErrorPool = new FixedThreadPool( + numberOfThreads, + './tests/worker-files/thread/asyncErrorWorker.js', + { + errorHandler: e => console.error(e) + } + ) + const asyncPool = new FixedThreadPool( + numberOfThreads, + './tests/worker-files/thread/asyncWorker.js' + ) + after('Destroy all pools', async () => { // We need to clean up the resources after our test await echoPool.destroy() @@ -92,7 +93,7 @@ describe('Fixed thread pool test suite', () => { it('Verify that data are sent to the worker correctly', async () => { const data = { f: 10 } const result = await echoPool.execute(data) - expect(result).toEqual(data) + expect(result).toStrictEqual(data) }) it('Verify that error handling is working properly:sync', async () => { @@ -130,7 +131,7 @@ describe('Fixed thread pool test suite', () => { const startTime = new Date().getTime() const result = await asyncPool.execute(data) const usedTime = new Date().getTime() - startTime - expect(result).toEqual(data) + expect(result).toStrictEqual(data) expect(usedTime).toBeGreaterThanOrEqual(2000) }) @@ -143,7 +144,7 @@ describe('Fixed thread pool test suite', () => { it('Should work even without opts in input', async () => { const pool1 = new FixedThreadPool( - 1, + numberOfThreads, './tests/worker-files/thread/testWorker.js' ) const res = await pool1.execute() diff --git a/tests/worker/abstract-worker.test.js b/tests/worker/abstract-worker.test.js index b9ace12c..9c8dd097 100644 --- a/tests/worker/abstract-worker.test.js +++ b/tests/worker/abstract-worker.test.js @@ -1,14 +1,14 @@ const { expect } = require('expect') const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib') -class StubPoolWithIsMainWorker extends ThreadWorker { - constructor (fn, opts) { - super(fn, opts) - this.mainWorker = false +describe('Abstract worker test suite', () => { + class StubPoolWithIsMainWorker extends ThreadWorker { + constructor (fn, opts) { + super(fn, opts) + this.mainWorker = false + } } -} -describe('Abstract worker test suite', () => { it('Verify that fn function is mandatory', () => { expect(() => new ClusterWorker()).toThrowError( new Error('fn parameter is mandatory') diff --git a/tests/worker/cluster-worker.test.js b/tests/worker/cluster-worker.test.js index b3b4f027..847c7e11 100644 --- a/tests/worker/cluster-worker.test.js +++ b/tests/worker/cluster-worker.test.js @@ -4,7 +4,7 @@ const { ClusterWorker } = require('../../lib') describe('Cluster worker test suite', () => { it('Verify worker has default maxInactiveTime', () => { const worker = new ClusterWorker(() => {}) - expect(worker.opts.maxInactiveTime).toEqual(60000) + expect(worker.opts.maxInactiveTime).toStrictEqual(60000) }) it('Verify that handleError function works properly', () => { diff --git a/tests/worker/thread-worker.test.js b/tests/worker/thread-worker.test.js index 4cf2c045..c04d3b2f 100644 --- a/tests/worker/thread-worker.test.js +++ b/tests/worker/thread-worker.test.js @@ -1,20 +1,20 @@ const { expect } = require('expect') const { ThreadWorker } = require('../../lib') -let numberOfMessagesPosted = 0 -const postMessage = function () { - numberOfMessagesPosted++ -} -class SpyWorker extends ThreadWorker { - getMainWorker () { - return { postMessage } +describe('Thread worker test suite', () => { + let numberOfMessagesPosted = 0 + const postMessage = function () { + numberOfMessagesPosted++ + } + class SpyWorker extends ThreadWorker { + getMainWorker () { + return { postMessage } + } } -} -describe('Thread worker test suite', () => { it('Verify worker has default maxInactiveTime', () => { const worker = new ThreadWorker(() => {}) - expect(worker.opts.maxInactiveTime).toEqual(60000) + expect(worker.opts.maxInactiveTime).toStrictEqual(60000) }) it('Verify worker invoke the getMainWorker and postMessage methods', () => {