1 import { MessageChannel, Worker } from 'node:worker_threads'
2 import cluster from 'node:cluster'
3 import { expect } from 'expect'
4 import { WorkerNode } from '../../lib/pools/worker-node.js'
5 import { WorkerTypes } from '../../lib/index.js'
6 import { CircularArray } from '../../lib/circular-array.js'
7 import { Deque } from '../../lib/deque.js'
8 import { DEFAULT_TASK_NAME } from '../../lib/utils.js'
10 describe('Worker node test suite', () => {
11 const threadWorker = new Worker('./tests/worker-files/thread/testWorker.mjs')
12 const clusterWorker = cluster.fork()
13 const threadWorkerNode = new WorkerNode(threadWorker, 12)
14 const clusterWorkerNode = new WorkerNode(clusterWorker, 12)
16 it('Worker node instantiation', () => {
17 expect(() => new WorkerNode()).toThrowError(
18 new TypeError('Cannot construct a worker node without a worker')
20 expect(() => new WorkerNode(threadWorker)).toThrowError(
22 'Cannot construct a worker node without a tasks queue back pressure size'
26 () => new WorkerNode(threadWorker, 'invalidTasksQueueBackPressureSize')
29 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
32 expect(() => new WorkerNode(threadWorker, 0.2)).toThrowError(
34 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
37 expect(() => new WorkerNode(threadWorker, 0)).toThrowError(
39 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
42 expect(() => new WorkerNode(threadWorker, -1)).toThrowError(
44 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
47 expect(threadWorkerNode).toBeInstanceOf(WorkerNode)
48 expect(threadWorkerNode.worker).toBe(threadWorker)
49 expect(threadWorkerNode.info).toStrictEqual({
50 id: threadWorker.threadId,
51 type: WorkerTypes.thread,
55 expect(threadWorkerNode.usage).toStrictEqual({
65 history: new CircularArray()
68 history: new CircularArray()
72 history: new CircularArray()
75 history: new CircularArray()
79 expect(threadWorkerNode.messageChannel).toBeInstanceOf(MessageChannel)
80 expect(threadWorkerNode.tasksQueueBackPressureSize).toBe(12)
81 expect(threadWorkerNode.tasksQueue).toBeInstanceOf(Deque)
82 expect(threadWorkerNode.tasksQueue.size).toBe(0)
83 expect(threadWorkerNode.tasksQueueSize()).toBe(
84 threadWorkerNode.tasksQueue.size
86 expect(threadWorkerNode.onBackPressureStarted).toBe(false)
87 expect(threadWorkerNode.onEmptyQueueCount).toBe(0)
88 expect(threadWorkerNode.taskFunctionsUsage).toBeInstanceOf(Map)
90 expect(clusterWorkerNode).toBeInstanceOf(WorkerNode)
91 expect(clusterWorkerNode.worker).toBe(clusterWorker)
92 expect(clusterWorkerNode.info).toStrictEqual({
94 type: WorkerTypes.cluster,
98 expect(clusterWorkerNode.usage).toStrictEqual({
108 history: new CircularArray()
111 history: new CircularArray()
115 history: new CircularArray()
118 history: new CircularArray()
122 expect(clusterWorkerNode.messageChannel).toBeUndefined()
123 expect(clusterWorkerNode.tasksQueueBackPressureSize).toBe(12)
124 expect(clusterWorkerNode.tasksQueue).toBeInstanceOf(Deque)
125 expect(clusterWorkerNode.tasksQueue.size).toBe(0)
126 expect(clusterWorkerNode.tasksQueueSize()).toBe(
127 clusterWorkerNode.tasksQueue.size
129 expect(clusterWorkerNode.onBackPressureStarted).toBe(false)
130 expect(clusterWorkerNode.onEmptyQueueCount).toBe(0)
131 expect(clusterWorkerNode.taskFunctionsUsage).toBeInstanceOf(Map)
134 it('Worker node getTaskFunctionWorkerUsage()', () => {
136 threadWorkerNode.getTaskFunctionWorkerUsage('invalidTaskFunction')
139 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined"
142 threadWorkerNode.info.taskFunctionNames = [DEFAULT_TASK_NAME, 'fn1']
144 threadWorkerNode.getTaskFunctionWorkerUsage('invalidTaskFunction')
147 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements"
150 threadWorkerNode.info.taskFunctionNames = [DEFAULT_TASK_NAME, 'fn1', 'fn2']
152 threadWorkerNode.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME)
162 history: new CircularArray()
165 history: new CircularArray()
169 history: new CircularArray()
172 history: new CircularArray()
176 expect(threadWorkerNode.getTaskFunctionWorkerUsage('fn1')).toStrictEqual({
185 history: new CircularArray()
188 history: new CircularArray()
192 history: new CircularArray()
195 history: new CircularArray()
199 expect(threadWorkerNode.getTaskFunctionWorkerUsage('fn2')).toStrictEqual({
208 history: new CircularArray()
211 history: new CircularArray()
215 history: new CircularArray()
218 history: new CircularArray()
222 expect(threadWorkerNode.taskFunctionsUsage.size).toBe(2)
225 it('Worker node deleteTaskFunctionWorkerUsage()', () => {
226 expect(threadWorkerNode.info.taskFunctionNames).toStrictEqual([
231 expect(threadWorkerNode.taskFunctionsUsage.size).toBe(2)
233 threadWorkerNode.deleteTaskFunctionWorkerUsage('invalidTaskFunction')
235 expect(threadWorkerNode.taskFunctionsUsage.size).toBe(2)
236 expect(threadWorkerNode.deleteTaskFunctionWorkerUsage('fn1')).toBe(true)
237 expect(threadWorkerNode.taskFunctionsUsage.size).toBe(1)