1 const { MessageChannel
, Worker
} = require('worker_threads')
2 const cluster
= require('cluster')
3 const { expect
} = require('expect')
4 const { WorkerNode
} = require('../../../lib/pools/worker-node')
5 const { WorkerTypes
} = require('../../../lib')
6 const { CircularArray
} = require('../../../lib/circular-array')
7 const { Deque
} = require('../../../lib/deque')
8 const { DEFAULT_TASK_NAME
} = require('../../../lib/utils')
10 describe('Worker node test suite', () => {
11 const threadWorker
= new Worker('./tests/worker-files/thread/testWorker.js')
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
.taskFunctionsUsage
).toBeInstanceOf(Map
)
85 expect(clusterWorkerNode
).toBeInstanceOf(WorkerNode
)
86 expect(clusterWorkerNode
.worker
).toBe(clusterWorker
)
87 expect(clusterWorkerNode
.info
).toStrictEqual({
89 type
: WorkerTypes
.cluster
,
93 expect(clusterWorkerNode
.usage
).toStrictEqual({
103 history
: new CircularArray()
106 history
: new CircularArray()
110 history
: new CircularArray()
113 history
: new CircularArray()
117 expect(clusterWorkerNode
.messageChannel
).toBeUndefined()
118 expect(clusterWorkerNode
.tasksQueueBackPressureSize
).toBe(12)
119 expect(clusterWorkerNode
.tasksQueue
).toBeInstanceOf(Deque
)
120 expect(clusterWorkerNode
.tasksQueue
.size
).toBe(0)
121 expect(clusterWorkerNode
.taskFunctionsUsage
).toBeInstanceOf(Map
)
124 it('Worker node getTaskFunctionWorkerUsage()', () => {
126 threadWorkerNode
.getTaskFunctionWorkerUsage('invalidTaskFunction')
129 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined"
132 threadWorkerNode
.info
.taskFunctions
= [DEFAULT_TASK_NAME
, 'fn1']
134 threadWorkerNode
.getTaskFunctionWorkerUsage('invalidTaskFunction')
137 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements"
140 threadWorkerNode
.info
.taskFunctions
= [DEFAULT_TASK_NAME
, 'fn1', 'fn2']
142 threadWorkerNode
.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME
)
152 history
: new CircularArray()
155 history
: new CircularArray()
159 history
: new CircularArray()
162 history
: new CircularArray()
166 expect(threadWorkerNode
.getTaskFunctionWorkerUsage('fn1')).toStrictEqual({
175 history
: new CircularArray()
178 history
: new CircularArray()
182 history
: new CircularArray()
185 history
: new CircularArray()
189 expect(threadWorkerNode
.getTaskFunctionWorkerUsage('fn2')).toStrictEqual({
198 history
: new CircularArray()
201 history
: new CircularArray()
205 history
: new CircularArray()
208 history
: new CircularArray()
212 expect(threadWorkerNode
.taskFunctionsUsage
.size
).toBe(2)