1 const { MessageChannel
, Worker
} = require('worker_threads')
2 const { expect
} = require('expect')
3 const { WorkerNode
} = require('../../../lib/pools/worker-node')
4 const { WorkerTypes
} = require('../../../lib')
5 const { CircularArray
} = require('../../../lib/circular-array')
6 const { Deque
} = require('../../../lib/deque')
7 const { DEFAULT_TASK_NAME
} = require('../../../lib/utils')
9 describe('Worker node test suite', () => {
10 const worker
= new Worker('./tests/worker-files/thread/testWorker.js')
11 const workerNode
= new WorkerNode(worker
, WorkerTypes
.thread
, 12)
13 it('Worker node instantiation', () => {
14 expect(() => new WorkerNode()).toThrowError(
15 new TypeError('Cannot construct a worker node without a worker')
17 expect(() => new WorkerNode(worker
)).toThrowError(
18 new TypeError('Cannot construct a worker node without a worker type')
20 expect(() => new WorkerNode(worker
, WorkerTypes
.thread
)).toThrowError(
22 'Cannot construct a worker node without a tasks queue back pressure size'
30 'invalidTasksQueueBackPressureSize'
34 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
37 expect(workerNode
).toBeInstanceOf(WorkerNode
)
38 expect(workerNode
.worker
).toBe(worker
)
39 expect(workerNode
.info
).toStrictEqual({
41 type
: WorkerTypes
.thread
,
45 expect(workerNode
.usage
).toStrictEqual({
55 history
: expect
.any(CircularArray
)
58 history
: expect
.any(CircularArray
)
62 history
: expect
.any(CircularArray
)
65 history
: expect
.any(CircularArray
)
69 expect(workerNode
.messageChannel
).toBeInstanceOf(MessageChannel
)
70 expect(workerNode
.tasksQueueBackPressureSize
).toBe(12)
71 expect(workerNode
.tasksQueue
).toBeInstanceOf(Deque
)
72 expect(workerNode
.tasksQueue
.size
).toBe(0)
73 expect(workerNode
.taskFunctionsUsage
).toBeInstanceOf(Map
)
76 it('Worker node getTaskFunctionWorkerUsage()', () => {
78 workerNode
.getTaskFunctionWorkerUsage('invalidTaskFunction')
81 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined"
84 workerNode
.info
.taskFunctions
= [DEFAULT_TASK_NAME
, 'fn1']
86 workerNode
.getTaskFunctionWorkerUsage('invalidTaskFunction')
89 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements"
92 workerNode
.info
.taskFunctions
= [DEFAULT_TASK_NAME
, 'fn1', 'fn2']
94 workerNode
.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME
)
104 history
: expect
.any(CircularArray
)
107 history
: expect
.any(CircularArray
)
111 history
: expect
.any(CircularArray
)
114 history
: expect
.any(CircularArray
)
118 expect(workerNode
.getTaskFunctionWorkerUsage('fn1')).toStrictEqual({
127 history
: expect
.any(CircularArray
)
130 history
: expect
.any(CircularArray
)
134 history
: expect
.any(CircularArray
)
137 history
: expect
.any(CircularArray
)
141 expect(workerNode
.getTaskFunctionWorkerUsage('fn2')).toStrictEqual({
150 history
: expect
.any(CircularArray
)
153 history
: expect
.any(CircularArray
)
157 history
: expect
.any(CircularArray
)
160 history
: expect
.any(CircularArray
)
164 expect(workerNode
.taskFunctionsUsage
.size
).toBe(2)