From 6cd5248f7b289828220cac63f8fe77d021c572e9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 27 Aug 2023 01:28:59 +0200 Subject: [PATCH] test: code cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- tests/pools/abstract/abstract-pool.test.js | 7 +-- tests/pools/abstract/worker-node.test.js | 9 ++-- tests/pools/cluster/fixed.test.js | 5 ++- tests/pools/thread/fixed.test.js | 5 ++- tests/worker/abstract-worker.test.js | 50 ++++++++++++---------- 5 files changed, 43 insertions(+), 33 deletions(-) diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index ddb0534f..3b3c92c0 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -12,6 +12,7 @@ const { } = require('../../../lib') const { CircularArray } = require('../../../lib/circular-array') const { Deque } = require('../../../lib/deque') +const { DEFAULT_TASK_NAME } = require('../../../lib/utils') const { version } = require('../../../package.json') const { waitPoolEvents } = require('../../test-utils') @@ -1155,7 +1156,7 @@ describe('Abstract pool test suite', () => { ) await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1) expect(dynamicThreadPool.listTaskFunctions()).toStrictEqual([ - 'default', + DEFAULT_TASK_NAME, 'jsonIntegerSerialization', 'factorial', 'fibonacci' @@ -1166,7 +1167,7 @@ describe('Abstract pool test suite', () => { ) await waitPoolEvents(fixedClusterPool, PoolEvents.ready, 1) expect(fixedClusterPool.listTaskFunctions()).toStrictEqual([ - 'default', + DEFAULT_TASK_NAME, 'jsonIntegerSerialization', 'factorial', 'fibonacci' @@ -1192,7 +1193,7 @@ describe('Abstract pool test suite', () => { expect(pool.info.executedTasks).toBe(4) for (const workerNode of pool.workerNodes) { expect(workerNode.info.taskFunctions).toStrictEqual([ - 'default', + DEFAULT_TASK_NAME, 'jsonIntegerSerialization', 'factorial', 'fibonacci' diff --git a/tests/pools/abstract/worker-node.test.js b/tests/pools/abstract/worker-node.test.js index 9401eb1a..ede278f2 100644 --- a/tests/pools/abstract/worker-node.test.js +++ b/tests/pools/abstract/worker-node.test.js @@ -4,6 +4,7 @@ const { WorkerNode } = require('../../../lib/pools/worker-node') const { WorkerTypes } = require('../../../lib') const { CircularArray } = require('../../../lib/circular-array') const { Deque } = require('../../../lib/deque') +const { DEFAULT_TASK_NAME } = require('../../../lib/utils') describe('Worker node test suite', () => { const worker = new Worker('./tests/worker-files/thread/testWorker.js') @@ -80,7 +81,7 @@ describe('Worker node test suite', () => { "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined" ) ) - workerNode.info.taskFunctions = ['default', 'fn1'] + workerNode.info.taskFunctions = [DEFAULT_TASK_NAME, 'fn1'] expect(() => workerNode.getTaskFunctionWorkerUsage('invalidTaskFunction') ).toThrowError( @@ -88,8 +89,10 @@ describe('Worker node test suite', () => { "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements" ) ) - workerNode.info.taskFunctions = ['default', 'fn1', 'fn2'] - expect(workerNode.getTaskFunctionWorkerUsage('default')).toStrictEqual({ + workerNode.info.taskFunctions = [DEFAULT_TASK_NAME, 'fn1', 'fn2'] + expect( + workerNode.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME) + ).toStrictEqual({ tasks: { executed: 0, executing: 0, diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 85eb6a0a..42e13a9a 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -2,6 +2,7 @@ const { expect } = require('expect') const { FixedClusterPool, PoolEvents } = require('../../../lib') const { TaskFunctions } = require('../../test-types') const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils') +const { DEFAULT_TASK_NAME } = require('../../../lib/utils') describe('Fixed cluster pool test suite', () => { const numberOfWorkers = 8 @@ -191,7 +192,7 @@ describe('Fixed cluster pool test suite', () => { expect(typeof inError === 'string').toBe(true) expect(inError).toBe('Error Message from ClusterWorker') expect(taskError).toStrictEqual({ - name: 'default', + name: DEFAULT_TASK_NAME, message: 'Error Message from ClusterWorker', data }) @@ -218,7 +219,7 @@ describe('Fixed cluster pool test suite', () => { expect(typeof inError === 'string').toBe(true) expect(inError).toBe('Error Message from ClusterWorker:async') expect(taskError).toStrictEqual({ - name: 'default', + name: DEFAULT_TASK_NAME, message: 'Error Message from ClusterWorker:async', data }) diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 29ccd827..f63f2265 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -2,6 +2,7 @@ const { expect } = require('expect') const { FixedThreadPool, PoolEvents } = require('../../../lib') const { TaskFunctions } = require('../../test-types') const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils') +const { DEFAULT_TASK_NAME } = require('../../../lib/utils') describe('Fixed thread pool test suite', () => { const numberOfThreads = 6 @@ -219,7 +220,7 @@ describe('Fixed thread pool test suite', () => { expect(typeof inError.message === 'string').toBe(true) expect(inError.message).toBe('Error Message from ThreadWorker') expect(taskError).toStrictEqual({ - name: 'default', + name: DEFAULT_TASK_NAME, message: new Error('Error Message from ThreadWorker'), data }) @@ -248,7 +249,7 @@ describe('Fixed thread pool test suite', () => { expect(typeof inError.message === 'string').toBe(true) expect(inError.message).toBe('Error Message from ThreadWorker:async') expect(taskError).toStrictEqual({ - name: 'default', + name: DEFAULT_TASK_NAME, message: new Error('Error Message from ThreadWorker:async'), data }) diff --git a/tests/worker/abstract-worker.test.js b/tests/worker/abstract-worker.test.js index e87fdb0b..0a920dd5 100644 --- a/tests/worker/abstract-worker.test.js +++ b/tests/worker/abstract-worker.test.js @@ -1,7 +1,7 @@ const { expect } = require('expect') const sinon = require('sinon') const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib') -const { EMPTY_FUNCTION } = require('../../lib/utils') +const { DEFAULT_TASK_NAME, EMPTY_FUNCTION } = require('../../lib/utils') describe('Abstract worker test suite', () => { class StubWorkerWithMainWorker extends ThreadWorker { @@ -96,10 +96,10 @@ describe('Abstract worker test suite', () => { it('Verify that taskFunctions parameter with unique function is taken', () => { const worker = new ThreadWorker(() => {}) - expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) expect(worker.taskFunctions.size).toBe(2) - expect(worker.taskFunctions.get('default')).toStrictEqual( + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( worker.taskFunctions.get('fn1') ) }) @@ -125,11 +125,11 @@ describe('Abstract worker test suite', () => { return 2 } const worker = new ClusterWorker({ fn1, fn2 }) - expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) expect(worker.taskFunctions.size).toBe(3) - expect(worker.taskFunctions.get('default')).toStrictEqual( + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( worker.taskFunctions.get('fn1') ) }) @@ -187,7 +187,7 @@ describe('Abstract worker test suite', () => { expect(() => worker.hasTaskFunction('')).toThrowError( new TypeError('name parameter is an empty string') ) - expect(worker.hasTaskFunction('default')).toBe(true) + expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toBe(true) expect(worker.hasTaskFunction('fn1')).toBe(true) expect(worker.hasTaskFunction('fn2')).toBe(true) expect(worker.hasTaskFunction('fn3')).toBe(false) @@ -213,29 +213,29 @@ describe('Abstract worker test suite', () => { expect(() => worker.addTaskFunction('fn3', '')).toThrowError( new TypeError('fn parameter is not a function') ) - expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) expect(worker.taskFunctions.size).toBe(2) - expect(worker.taskFunctions.get('default')).toStrictEqual( + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( worker.taskFunctions.get('fn1') ) - expect(() => worker.addTaskFunction('default', fn2)).toThrowError( + expect(() => worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toThrowError( new Error('Cannot add a task function with the default reserved name') ) worker.addTaskFunction('fn2', fn2) - expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) expect(worker.taskFunctions.size).toBe(3) - expect(worker.taskFunctions.get('default')).toStrictEqual( + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( worker.taskFunctions.get('fn1') ) worker.addTaskFunction('fn1', fn1Replacement) - expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) expect(worker.taskFunctions.size).toBe(3) - expect(worker.taskFunctions.get('default')).toStrictEqual( + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( worker.taskFunctions.get('fn1') ) }) @@ -258,14 +258,14 @@ describe('Abstract worker test suite', () => { id: 1, send: sinon.stub().returns() }) - expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) expect(worker.taskFunctions.size).toBe(3) - expect(worker.taskFunctions.get('default')).toStrictEqual( + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( worker.taskFunctions.get('fn1') ) - expect(() => worker.removeTaskFunction('default')).toThrowError( + expect(() => worker.removeTaskFunction(DEFAULT_TASK_NAME)).toThrowError( new Error( 'Cannot remove the task function with the default reserved name' ) @@ -276,7 +276,7 @@ describe('Abstract worker test suite', () => { ) ) worker.removeTaskFunction('fn2') - expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn2')).toBeUndefined() expect(worker.taskFunctions.size).toBe(2) @@ -291,7 +291,11 @@ describe('Abstract worker test suite', () => { return 2 } const worker = new ClusterWorker({ fn1, fn2 }) - expect(worker.listTaskFunctions()).toStrictEqual(['default', 'fn1', 'fn2']) + expect(worker.listTaskFunctions()).toStrictEqual([ + DEFAULT_TASK_NAME, + 'fn1', + 'fn2' + ]) }) it('Verify that setDefaultTaskFunction() works', () => { @@ -308,14 +312,14 @@ describe('Abstract worker test suite', () => { expect(() => worker.setDefaultTaskFunction('', fn1)).toThrowError( new TypeError('name parameter is an empty string') ) - expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) expect(worker.taskFunctions.size).toBe(3) - expect(worker.taskFunctions.get('default')).toStrictEqual( + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( worker.taskFunctions.get('fn1') ) - expect(() => worker.setDefaultTaskFunction('default')).toThrowError( + expect(() => worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toThrowError( new Error( 'Cannot set the default task function reserved name as the default task function' ) @@ -326,11 +330,11 @@ describe('Abstract worker test suite', () => { ) ) worker.setDefaultTaskFunction('fn1') - expect(worker.taskFunctions.get('default')).toStrictEqual( + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( worker.taskFunctions.get('fn1') ) worker.setDefaultTaskFunction('fn2') - expect(worker.taskFunctions.get('default')).toStrictEqual( + expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( worker.taskFunctions.get('fn2') ) }) -- 2.34.1