X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fworker-node.test.mjs;h=549db4b6aa5ae82ac6d87ab3007c376f2e854853;hb=a4d25410764bca884efaea4d46ed9bf83b8a8dbb;hp=0780db255d618cf913b782a8e1e957613d139d8e;hpb=a074ffee1b46f43d0dcfba58128748c7492104dd;p=poolifier.git diff --git a/tests/pools/worker-node.test.mjs b/tests/pools/worker-node.test.mjs index 0780db25..549db4b6 100644 --- a/tests/pools/worker-node.test.mjs +++ b/tests/pools/worker-node.test.mjs @@ -1,56 +1,131 @@ -import { MessageChannel, Worker } from 'node:worker_threads' -import cluster from 'node:cluster' +import { MessageChannel, Worker as ThreadWorker } from 'node:worker_threads' +import { Worker as ClusterWorker } from 'node:cluster' import { expect } from 'expect' -import { WorkerNode } from '../../lib/pools/worker-node.js' -import { WorkerTypes } from '../../lib/index.js' -import { CircularArray } from '../../lib/circular-array.js' -import { Deque } from '../../lib/deque.js' -import { DEFAULT_TASK_NAME } from '../../lib/utils.js' +import { WorkerNode } from '../../lib/pools/worker-node.cjs' +import { WorkerTypes } from '../../lib/index.cjs' +import { CircularArray } from '../../lib/circular-array.cjs' +import { Deque } from '../../lib/deque.cjs' +import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs' describe('Worker node test suite', () => { - const threadWorker = new Worker('./tests/worker-files/thread/testWorker.js') - const clusterWorker = cluster.fork() - const threadWorkerNode = new WorkerNode(threadWorker, 12) - const clusterWorkerNode = new WorkerNode(clusterWorker, 12) + const threadWorkerNode = new WorkerNode( + WorkerTypes.thread, + './tests/worker-files/thread/testWorker.mjs', + { tasksQueueBackPressureSize: 12 } + ) + const clusterWorkerNode = new WorkerNode( + WorkerTypes.cluster, + './tests/worker-files/cluster/testWorker.cjs', + { tasksQueueBackPressureSize: 12 } + ) it('Worker node instantiation', () => { - expect(() => new WorkerNode()).toThrowError( - new TypeError('Cannot construct a worker node without a worker') + expect(() => new WorkerNode()).toThrow( + new TypeError('Cannot construct a worker node without a worker type') ) - expect(() => new WorkerNode(threadWorker)).toThrowError( + expect( + () => + new WorkerNode( + 'invalidWorkerType', + './tests/worker-files/thread/testWorker.mjs', + { tasksQueueBackPressureSize: 12 } + ) + ).toThrow( + new TypeError( + "Cannot construct a worker node with an invalid worker type 'invalidWorkerType'" + ) + ) + expect( + () => + new WorkerNode( + WorkerTypes.thread, + './tests/worker-files/thread/testWorker.mjs' + ) + ).toThrow( + new TypeError( + 'Cannot construct a worker node without worker node options' + ) + ) + expect( + () => + new WorkerNode( + WorkerTypes.thread, + './tests/worker-files/thread/testWorker.mjs', + '' + ) + ).toThrow( + new TypeError( + 'Cannot construct a worker node with invalid options: must be a plain object' + ) + ) + expect( + () => + new WorkerNode( + WorkerTypes.thread, + './tests/worker-files/thread/testWorker.mjs', + {} + ) + ).toThrow( new TypeError( - 'Cannot construct a worker node without a tasks queue back pressure size' + 'Cannot construct a worker node without a tasks queue back pressure size option' ) ) expect( - () => new WorkerNode(threadWorker, 'invalidTasksQueueBackPressureSize') - ).toThrowError( + () => + new WorkerNode( + WorkerTypes.thread, + './tests/worker-files/thread/testWorker.mjs', + { tasksQueueBackPressureSize: 'invalidTasksQueueBackPressureSize' } + ) + ).toThrow( new TypeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' + 'Cannot construct a worker node with a tasks queue back pressure size option that is not an integer' ) ) - expect(() => new WorkerNode(threadWorker, 0.2)).toThrowError( + expect( + () => + new WorkerNode( + WorkerTypes.thread, + './tests/worker-files/thread/testWorker.mjs', + { tasksQueueBackPressureSize: 0.2 } + ) + ).toThrow( new TypeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' + 'Cannot construct a worker node with a tasks queue back pressure size option that is not an integer' ) ) - expect(() => new WorkerNode(threadWorker, 0)).toThrowError( + expect( + () => + new WorkerNode( + WorkerTypes.thread, + './tests/worker-files/thread/testWorker.mjs', + { tasksQueueBackPressureSize: 0 } + ) + ).toThrow( new RangeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer' + 'Cannot construct a worker node with a tasks queue back pressure size option that is not a positive integer' ) ) - expect(() => new WorkerNode(threadWorker, -1)).toThrowError( + expect( + () => + new WorkerNode( + WorkerTypes.thread, + './tests/worker-files/thread/testWorker.mjs', + { tasksQueueBackPressureSize: -1 } + ) + ).toThrow( new RangeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer' + 'Cannot construct a worker node with a tasks queue back pressure size option that is not a positive integer' ) ) expect(threadWorkerNode).toBeInstanceOf(WorkerNode) - expect(threadWorkerNode.worker).toBe(threadWorker) + expect(threadWorkerNode.worker).toBeInstanceOf(ThreadWorker) expect(threadWorkerNode.info).toStrictEqual({ - id: threadWorker.threadId, + id: threadWorkerNode.worker.threadId, type: WorkerTypes.thread, dynamic: false, - ready: false + ready: false, + stealing: false }) expect(threadWorkerNode.usage).toStrictEqual({ tasks: { @@ -58,6 +133,7 @@ describe('Worker node test suite', () => { executing: 0, queued: 0, maxQueued: 0, + sequentiallyStolen: 0, stolen: 0, failed: 0 }, @@ -84,16 +160,16 @@ describe('Worker node test suite', () => { threadWorkerNode.tasksQueue.size ) expect(threadWorkerNode.onBackPressureStarted).toBe(false) - expect(threadWorkerNode.onEmptyQueueCount).toBe(0) expect(threadWorkerNode.taskFunctionsUsage).toBeInstanceOf(Map) expect(clusterWorkerNode).toBeInstanceOf(WorkerNode) - expect(clusterWorkerNode.worker).toBe(clusterWorker) + expect(clusterWorkerNode.worker).toBeInstanceOf(ClusterWorker) expect(clusterWorkerNode.info).toStrictEqual({ - id: clusterWorker.id, + id: clusterWorkerNode.worker.id, type: WorkerTypes.cluster, dynamic: false, - ready: false + ready: false, + stealing: false }) expect(clusterWorkerNode.usage).toStrictEqual({ tasks: { @@ -101,6 +177,7 @@ describe('Worker node test suite', () => { executing: 0, queued: 0, maxQueued: 0, + sequentiallyStolen: 0, stolen: 0, failed: 0 }, @@ -127,14 +204,13 @@ describe('Worker node test suite', () => { clusterWorkerNode.tasksQueue.size ) expect(clusterWorkerNode.onBackPressureStarted).toBe(false) - expect(clusterWorkerNode.onEmptyQueueCount).toBe(0) expect(clusterWorkerNode.taskFunctionsUsage).toBeInstanceOf(Map) }) it('Worker node getTaskFunctionWorkerUsage()', () => { expect(() => threadWorkerNode.getTaskFunctionWorkerUsage('invalidTaskFunction') - ).toThrowError( + ).toThrow( new TypeError( "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined" ) @@ -142,7 +218,7 @@ describe('Worker node test suite', () => { threadWorkerNode.info.taskFunctionNames = [DEFAULT_TASK_NAME, 'fn1'] expect(() => threadWorkerNode.getTaskFunctionWorkerUsage('invalidTaskFunction') - ).toThrowError( + ).toThrow( new TypeError( "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements" ) @@ -155,6 +231,7 @@ describe('Worker node test suite', () => { executed: 0, executing: 0, queued: 0, + sequentiallyStolen: 0, stolen: 0, failed: 0 }, @@ -178,6 +255,7 @@ describe('Worker node test suite', () => { executed: 0, executing: 0, queued: 0, + sequentiallyStolen: 0, stolen: 0, failed: 0 }, @@ -201,6 +279,7 @@ describe('Worker node test suite', () => { executed: 0, executing: 0, queued: 0, + sequentiallyStolen: 0, stolen: 0, failed: 0 },