From e1e0cb25f128238f3f7b3a49f41fa5e5be236577 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 31 Jul 2024 16:54:41 +0200 Subject: [PATCH] test: cleanup resources setup and teardown MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../selection-strategies-utils.test.mjs | 46 +++++++++++++------ ...ound-robin-worker-choice-strategy.test.mjs | 4 +- .../worker-choice-strategies-context.test.mjs | 4 +- tests/pools/worker-node.test.mjs | 45 ++++++++++-------- 4 files changed, 62 insertions(+), 37 deletions(-) diff --git a/tests/pools/selection-strategies/selection-strategies-utils.test.mjs b/tests/pools/selection-strategies/selection-strategies-utils.test.mjs index 9b57e789..f2223b83 100644 --- a/tests/pools/selection-strategies/selection-strategies-utils.test.mjs +++ b/tests/pools/selection-strategies/selection-strategies-utils.test.mjs @@ -7,19 +7,34 @@ import { } from '../../../lib/pools/selection-strategies/selection-strategies-utils.cjs' describe('Selection strategies utils test suite', () => { - it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => { - const numberOfWorkers = 4 - const pool = new FixedClusterPool( + const numberOfWorkers = 4 + const numberOfThreads = 4 + let clusterFixedPool, threadFixedPool + + before('Create pools', () => { + clusterFixedPool = new FixedClusterPool( numberOfWorkers, './tests/worker-files/cluster/testWorker.cjs' ) - expect(buildWorkerChoiceStrategyOptions(pool)).toStrictEqual({ + threadFixedPool = new FixedThreadPool( + numberOfThreads, + './tests/worker-files/thread/testWorker.mjs' + ) + }) + + after('Destroy pools', async () => { + await clusterFixedPool.destroy() + await threadFixedPool.destroy() + }) + + it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => { + expect(buildWorkerChoiceStrategyOptions(clusterFixedPool)).toStrictEqual({ runTime: { median: false }, waitTime: { median: false }, elu: { median: false }, weights: expect.objectContaining({ 0: expect.any(Number), - [pool.info.maxSize - 1]: expect.any(Number), + [clusterFixedPool.info.maxSize - 1]: expect.any(Number), }), }) const workerChoiceStrategyOptions = { @@ -32,18 +47,17 @@ describe('Selection strategies utils test suite', () => { }, } expect( - buildWorkerChoiceStrategyOptions(pool, workerChoiceStrategyOptions) + buildWorkerChoiceStrategyOptions( + clusterFixedPool, + workerChoiceStrategyOptions + ) ).toStrictEqual(workerChoiceStrategyOptions) - await pool.destroy() }) it('Verify getWorkerChoiceStrategyRetries() behavior', async () => { - const numberOfThreads = 4 - const pool = new FixedThreadPool( - numberOfThreads, - './tests/worker-files/thread/testWorker.mjs' + expect(getWorkerChoiceStrategiesRetries(threadFixedPool)).toBe( + threadFixedPool.info.maxSize * 2 ) - expect(getWorkerChoiceStrategiesRetries(pool)).toBe(pool.info.maxSize * 2) const workerChoiceStrategyOptions = { runTime: { median: true }, waitTime: { median: true }, @@ -54,11 +68,13 @@ describe('Selection strategies utils test suite', () => { }, } expect( - getWorkerChoiceStrategiesRetries(pool, workerChoiceStrategyOptions) + getWorkerChoiceStrategiesRetries( + threadFixedPool, + workerChoiceStrategyOptions + ) ).toBe( - pool.info.maxSize + + threadFixedPool.info.maxSize + Object.keys(workerChoiceStrategyOptions.weights).length ) - await pool.destroy() }) }) diff --git a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.mjs b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.mjs index b3ea806e..fae2aecb 100644 --- a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.mjs +++ b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.mjs @@ -11,14 +11,14 @@ describe('Weighted round robin strategy worker choice strategy test suite', () = const max = 3 let pool - before(() => { + before('Create pool', () => { pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.mjs' ) }) - after(async () => { + after('Destroy pool', async () => { await pool.destroy() }) diff --git a/tests/pools/selection-strategies/worker-choice-strategies-context.test.mjs b/tests/pools/selection-strategies/worker-choice-strategies-context.test.mjs index e046af55..427103a4 100644 --- a/tests/pools/selection-strategies/worker-choice-strategies-context.test.mjs +++ b/tests/pools/selection-strategies/worker-choice-strategies-context.test.mjs @@ -20,7 +20,7 @@ describe('Worker choice strategies context test suite', () => { const max = 3 let fixedPool, dynamicPool - before(() => { + before('Create pools', () => { fixedPool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.mjs' @@ -36,7 +36,7 @@ describe('Worker choice strategies context test suite', () => { restore() }) - after(async () => { + after('Destroy pools', async () => { await fixedPool.destroy() await dynamicPool.destroy() }) diff --git a/tests/pools/worker-node.test.mjs b/tests/pools/worker-node.test.mjs index 886e01bf..4fea0e43 100644 --- a/tests/pools/worker-node.test.mjs +++ b/tests/pools/worker-node.test.mjs @@ -11,24 +11,33 @@ import { PriorityQueue } from '../../lib/queues/priority-queue.cjs' import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs' describe('Worker node test suite', () => { - const threadWorkerNode = new WorkerNode( - WorkerTypes.thread, - './tests/worker-files/thread/testWorker.mjs', - { - tasksQueueBackPressureSize: 12, - tasksQueueBucketSize: 6, - tasksQueuePriority: true, - } - ) - const clusterWorkerNode = new WorkerNode( - WorkerTypes.cluster, - './tests/worker-files/cluster/testWorker.cjs', - { - tasksQueueBackPressureSize: 12, - tasksQueueBucketSize: 6, - tasksQueuePriority: true, - } - ) + let threadWorkerNode, clusterWorkerNode + + before('Create worker nodes', () => { + threadWorkerNode = new WorkerNode( + WorkerTypes.thread, + './tests/worker-files/thread/testWorker.mjs', + { + tasksQueueBackPressureSize: 12, + tasksQueueBucketSize: 6, + tasksQueuePriority: true, + } + ) + clusterWorkerNode = new WorkerNode( + WorkerTypes.cluster, + './tests/worker-files/cluster/testWorker.cjs', + { + tasksQueueBackPressureSize: 12, + tasksQueueBucketSize: 6, + tasksQueuePriority: true, + } + ) + }) + + after('Terminate worker nodes', async () => { + await threadWorkerNode.terminate() + await clusterWorkerNode.terminate() + }) it('Worker node instantiation', () => { expect(() => new WorkerNode()).toThrow( -- 2.34.1