1 import { MessageChannel, Worker as ThreadWorker } from 'node:worker_threads'
2 import { Worker as ClusterWorker } 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 threadWorkerNode = new WorkerNode(
13 './tests/worker-files/thread/testWorker.mjs',
14 { tasksQueueBackPressureSize: 12 }
16 const clusterWorkerNode = new WorkerNode(
18 './tests/worker-files/cluster/testWorker.js',
19 { tasksQueueBackPressureSize: 12 }
22 it('Worker node instantiation', () => {
23 expect(() => new WorkerNode()).toThrow(
24 new TypeError('Cannot construct a worker node without a worker type')
30 './tests/worker-files/thread/testWorker.mjs',
31 { tasksQueueBackPressureSize: 12 }
35 "Cannot construct a worker node with an invalid worker type 'invalidWorkerType'"
42 './tests/worker-files/thread/testWorker.mjs'
46 'Cannot construct a worker node without worker node options'
53 './tests/worker-files/thread/testWorker.mjs',
58 'Cannot construct a worker node with invalid options: must be a plain object'
65 './tests/worker-files/thread/testWorker.mjs',
70 'Cannot construct a worker node without a tasks queue back pressure size option'
77 './tests/worker-files/thread/testWorker.mjs',
78 { tasksQueueBackPressureSize: 'invalidTasksQueueBackPressureSize' }
82 'Cannot construct a worker node with a tasks queue back pressure size option that is not an integer'
89 './tests/worker-files/thread/testWorker.mjs',
90 { tasksQueueBackPressureSize: 0.2 }
94 'Cannot construct a worker node with a tasks queue back pressure size option that is not an integer'
101 './tests/worker-files/thread/testWorker.mjs',
102 { tasksQueueBackPressureSize: 0 }
106 'Cannot construct a worker node with a tasks queue back pressure size option that is not a positive integer'
113 './tests/worker-files/thread/testWorker.mjs',
114 { tasksQueueBackPressureSize: -1 }
118 'Cannot construct a worker node with a tasks queue back pressure size option that is not a positive integer'
121 expect(threadWorkerNode).toBeInstanceOf(WorkerNode)
122 expect(threadWorkerNode.worker).toBeInstanceOf(ThreadWorker)
123 expect(threadWorkerNode.info).toStrictEqual({
124 id: threadWorkerNode.worker.threadId,
125 type: WorkerTypes.thread,
129 expect(threadWorkerNode.usage).toStrictEqual({
135 sequentiallyStolen: 0,
140 history: new CircularArray()
143 history: new CircularArray()
147 history: new CircularArray()
150 history: new CircularArray()
154 expect(threadWorkerNode.messageChannel).toBeInstanceOf(MessageChannel)
155 expect(threadWorkerNode.tasksQueueBackPressureSize).toBe(12)
156 expect(threadWorkerNode.tasksQueue).toBeInstanceOf(Deque)
157 expect(threadWorkerNode.tasksQueue.size).toBe(0)
158 expect(threadWorkerNode.tasksQueueSize()).toBe(
159 threadWorkerNode.tasksQueue.size
161 expect(threadWorkerNode.onBackPressureStarted).toBe(false)
162 expect(threadWorkerNode.taskFunctionsUsage).toBeInstanceOf(Map)
164 expect(clusterWorkerNode).toBeInstanceOf(WorkerNode)
165 expect(clusterWorkerNode.worker).toBeInstanceOf(ClusterWorker)
166 expect(clusterWorkerNode.info).toStrictEqual({
167 id: clusterWorkerNode.worker.id,
168 type: WorkerTypes.cluster,
172 expect(clusterWorkerNode.usage).toStrictEqual({
178 sequentiallyStolen: 0,
183 history: new CircularArray()
186 history: new CircularArray()
190 history: new CircularArray()
193 history: new CircularArray()
197 expect(clusterWorkerNode.messageChannel).toBeUndefined()
198 expect(clusterWorkerNode.tasksQueueBackPressureSize).toBe(12)
199 expect(clusterWorkerNode.tasksQueue).toBeInstanceOf(Deque)
200 expect(clusterWorkerNode.tasksQueue.size).toBe(0)
201 expect(clusterWorkerNode.tasksQueueSize()).toBe(
202 clusterWorkerNode.tasksQueue.size
204 expect(clusterWorkerNode.onBackPressureStarted).toBe(false)
205 expect(clusterWorkerNode.taskFunctionsUsage).toBeInstanceOf(Map)
208 it('Worker node getTaskFunctionWorkerUsage()', () => {
210 threadWorkerNode.getTaskFunctionWorkerUsage('invalidTaskFunction')
213 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined"
216 threadWorkerNode.info.taskFunctionNames = [DEFAULT_TASK_NAME, 'fn1']
218 threadWorkerNode.getTaskFunctionWorkerUsage('invalidTaskFunction')
221 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements"
224 threadWorkerNode.info.taskFunctionNames = [DEFAULT_TASK_NAME, 'fn1', 'fn2']
226 threadWorkerNode.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME)
232 sequentiallyStolen: 0,
237 history: new CircularArray()
240 history: new CircularArray()
244 history: new CircularArray()
247 history: new CircularArray()
251 expect(threadWorkerNode.getTaskFunctionWorkerUsage('fn1')).toStrictEqual({
256 sequentiallyStolen: 0,
261 history: new CircularArray()
264 history: new CircularArray()
268 history: new CircularArray()
271 history: new CircularArray()
275 expect(threadWorkerNode.getTaskFunctionWorkerUsage('fn2')).toStrictEqual({
280 sequentiallyStolen: 0,
285 history: new CircularArray()
288 history: new CircularArray()
292 history: new CircularArray()
295 history: new CircularArray()
299 expect(threadWorkerNode.taskFunctionsUsage.size).toBe(2)
302 it('Worker node deleteTaskFunctionWorkerUsage()', () => {
303 expect(threadWorkerNode.info.taskFunctionNames).toStrictEqual([
308 expect(threadWorkerNode.taskFunctionsUsage.size).toBe(2)
310 threadWorkerNode.deleteTaskFunctionWorkerUsage('invalidTaskFunction')
312 expect(threadWorkerNode.taskFunctionsUsage.size).toBe(2)
313 expect(threadWorkerNode.deleteTaskFunctionWorkerUsage('fn1')).toBe(true)
314 expect(threadWorkerNode.taskFunctionsUsage.size).toBe(1)