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')
8 describe('Worker node test suite', () => {
9 const worker
= new Worker('./tests/worker-files/thread/testWorker.js')
10 const workerNode
= new WorkerNode(worker
, WorkerTypes
.thread
, 12)
12 it('Worker node instantiation', () => {
13 expect(() => new WorkerNode()).toThrowError(
14 new TypeError('Cannot construct a worker node without a worker')
16 expect(() => new WorkerNode(worker
)).toThrowError(
17 new TypeError('Cannot construct a worker node without a worker type')
19 expect(() => new WorkerNode(worker
, WorkerTypes
.thread
)).toThrowError(
21 'Cannot construct a worker node without a tasks queue back pressure size'
29 'invalidTasksQueueBackPressureSize'
33 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
36 expect(workerNode
).toBeInstanceOf(WorkerNode
)
37 expect(workerNode
.worker
).toBe(worker
)
38 expect(workerNode
.info
).toStrictEqual({
40 type
: WorkerTypes
.thread
,
44 expect(workerNode
.usage
).toStrictEqual({
54 history
: expect
.any(CircularArray
)
57 history
: expect
.any(CircularArray
)
61 history
: expect
.any(CircularArray
)
64 history
: expect
.any(CircularArray
)
68 expect(workerNode
.messageChannel
).toBeInstanceOf(MessageChannel
)
69 expect(workerNode
.tasksQueueBackPressureSize
).toBe(12)
70 expect(workerNode
.tasksQueue
).toBeInstanceOf(Deque
)
71 expect(workerNode
.tasksQueue
.size
).toBe(0)
72 expect(workerNode
.taskFunctionsUsage
).toBeInstanceOf(Map
)
75 it('Worker node getTaskFunctionWorkerUsage()', () => {
77 workerNode
.getTaskFunctionWorkerUsage('invalidTaskFunction')
80 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined"
83 workerNode
.info
.taskFunctions
= ['default', 'fn1']
85 workerNode
.getTaskFunctionWorkerUsage('invalidTaskFunction')
88 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements"
91 workerNode
.info
.taskFunctions
= ['default', 'fn1', 'fn2']
92 expect(workerNode
.getTaskFunctionWorkerUsage('default')).toStrictEqual({
101 history
: expect
.any(CircularArray
)
104 history
: expect
.any(CircularArray
)
108 history
: expect
.any(CircularArray
)
111 history
: expect
.any(CircularArray
)
115 expect(workerNode
.getTaskFunctionWorkerUsage('fn1')).toStrictEqual({
124 history
: expect
.any(CircularArray
)
127 history
: expect
.any(CircularArray
)
131 history
: expect
.any(CircularArray
)
134 history
: expect
.any(CircularArray
)
138 expect(workerNode
.getTaskFunctionWorkerUsage('fn2')).toStrictEqual({
147 history
: expect
.any(CircularArray
)
150 history
: expect
.any(CircularArray
)
154 history
: expect
.any(CircularArray
)
157 history
: expect
.any(CircularArray
)
161 expect(workerNode
.taskFunctionsUsage
.size
).toBe(2)