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(threadWorkerNode
).toBeInstanceOf(WorkerNode
)
33 expect(threadWorkerNode
.worker
).toBe(threadWorker
)
34 expect(threadWorkerNode
.info
).toStrictEqual({
35 id
: threadWorker
.threadId
,
36 type
: WorkerTypes
.thread
,
40 expect(threadWorkerNode
.usage
).toStrictEqual({
50 history
: expect
.any(CircularArray
)
53 history
: expect
.any(CircularArray
)
57 history
: expect
.any(CircularArray
)
60 history
: expect
.any(CircularArray
)
64 expect(threadWorkerNode
.messageChannel
).toBeInstanceOf(MessageChannel
)
65 expect(threadWorkerNode
.tasksQueueBackPressureSize
).toBe(12)
66 expect(threadWorkerNode
.tasksQueue
).toBeInstanceOf(Deque
)
67 expect(threadWorkerNode
.tasksQueue
.size
).toBe(0)
68 expect(threadWorkerNode
.taskFunctionsUsage
).toBeInstanceOf(Map
)
70 expect(clusterWorkerNode
).toBeInstanceOf(WorkerNode
)
71 expect(clusterWorkerNode
.worker
).toBe(clusterWorker
)
72 expect(clusterWorkerNode
.info
).toStrictEqual({
74 type
: WorkerTypes
.cluster
,
78 expect(clusterWorkerNode
.usage
).toStrictEqual({
88 history
: expect
.any(CircularArray
)
91 history
: expect
.any(CircularArray
)
95 history
: expect
.any(CircularArray
)
98 history
: expect
.any(CircularArray
)
102 expect(clusterWorkerNode
.messageChannel
).toBeUndefined()
103 expect(clusterWorkerNode
.tasksQueueBackPressureSize
).toBe(12)
104 expect(clusterWorkerNode
.tasksQueue
).toBeInstanceOf(Deque
)
105 expect(clusterWorkerNode
.tasksQueue
.size
).toBe(0)
106 expect(clusterWorkerNode
.taskFunctionsUsage
).toBeInstanceOf(Map
)
109 it('Worker node getTaskFunctionWorkerUsage()', () => {
111 threadWorkerNode
.getTaskFunctionWorkerUsage('invalidTaskFunction')
114 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined"
117 threadWorkerNode
.info
.taskFunctions
= [DEFAULT_TASK_NAME
, 'fn1']
119 threadWorkerNode
.getTaskFunctionWorkerUsage('invalidTaskFunction')
122 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements"
125 threadWorkerNode
.info
.taskFunctions
= [DEFAULT_TASK_NAME
, 'fn1', 'fn2']
127 threadWorkerNode
.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME
)
137 history
: expect
.any(CircularArray
)
140 history
: expect
.any(CircularArray
)
144 history
: expect
.any(CircularArray
)
147 history
: expect
.any(CircularArray
)
151 expect(threadWorkerNode
.getTaskFunctionWorkerUsage('fn1')).toStrictEqual({
160 history
: expect
.any(CircularArray
)
163 history
: expect
.any(CircularArray
)
167 history
: expect
.any(CircularArray
)
170 history
: expect
.any(CircularArray
)
174 expect(threadWorkerNode
.getTaskFunctionWorkerUsage('fn2')).toStrictEqual({
183 history
: expect
.any(CircularArray
)
186 history
: expect
.any(CircularArray
)
190 history
: expect
.any(CircularArray
)
193 history
: expect
.any(CircularArray
)
197 expect(threadWorkerNode
.taskFunctionsUsage
.size
).toBe(2)