test: code cleanup
[poolifier.git] / tests / pools / abstract / worker-node.test.js
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')
8
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)
12
13 it('Worker node instantiation', () => {
14 expect(() => new WorkerNode()).toThrowError(
15 new TypeError('Cannot construct a worker node without a worker')
16 )
17 expect(() => new WorkerNode(worker)).toThrowError(
18 new TypeError('Cannot construct a worker node without a worker type')
19 )
20 expect(() => new WorkerNode(worker, WorkerTypes.thread)).toThrowError(
21 new TypeError(
22 'Cannot construct a worker node without a tasks queue back pressure size'
23 )
24 )
25 expect(
26 () =>
27 new WorkerNode(
28 worker,
29 WorkerTypes.thread,
30 'invalidTasksQueueBackPressureSize'
31 )
32 ).toThrowError(
33 new TypeError(
34 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
35 )
36 )
37 expect(workerNode).toBeInstanceOf(WorkerNode)
38 expect(workerNode.worker).toBe(worker)
39 expect(workerNode.info).toStrictEqual({
40 id: worker.threadId,
41 type: WorkerTypes.thread,
42 dynamic: false,
43 ready: false
44 })
45 expect(workerNode.usage).toStrictEqual({
46 tasks: {
47 executed: 0,
48 executing: 0,
49 queued: 0,
50 maxQueued: 0,
51 stolen: 0,
52 failed: 0
53 },
54 runTime: {
55 history: expect.any(CircularArray)
56 },
57 waitTime: {
58 history: expect.any(CircularArray)
59 },
60 elu: {
61 idle: {
62 history: expect.any(CircularArray)
63 },
64 active: {
65 history: expect.any(CircularArray)
66 }
67 }
68 })
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)
74 })
75
76 it('Worker node getTaskFunctionWorkerUsage()', () => {
77 expect(() =>
78 workerNode.getTaskFunctionWorkerUsage('invalidTaskFunction')
79 ).toThrowError(
80 new TypeError(
81 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined"
82 )
83 )
84 workerNode.info.taskFunctions = [DEFAULT_TASK_NAME, 'fn1']
85 expect(() =>
86 workerNode.getTaskFunctionWorkerUsage('invalidTaskFunction')
87 ).toThrowError(
88 new TypeError(
89 "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements"
90 )
91 )
92 workerNode.info.taskFunctions = [DEFAULT_TASK_NAME, 'fn1', 'fn2']
93 expect(
94 workerNode.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME)
95 ).toStrictEqual({
96 tasks: {
97 executed: 0,
98 executing: 0,
99 queued: 0,
100 stolen: 0,
101 failed: 0
102 },
103 runTime: {
104 history: expect.any(CircularArray)
105 },
106 waitTime: {
107 history: expect.any(CircularArray)
108 },
109 elu: {
110 idle: {
111 history: expect.any(CircularArray)
112 },
113 active: {
114 history: expect.any(CircularArray)
115 }
116 }
117 })
118 expect(workerNode.getTaskFunctionWorkerUsage('fn1')).toStrictEqual({
119 tasks: {
120 executed: 0,
121 executing: 0,
122 queued: 0,
123 stolen: 0,
124 failed: 0
125 },
126 runTime: {
127 history: expect.any(CircularArray)
128 },
129 waitTime: {
130 history: expect.any(CircularArray)
131 },
132 elu: {
133 idle: {
134 history: expect.any(CircularArray)
135 },
136 active: {
137 history: expect.any(CircularArray)
138 }
139 }
140 })
141 expect(workerNode.getTaskFunctionWorkerUsage('fn2')).toStrictEqual({
142 tasks: {
143 executed: 0,
144 executing: 0,
145 queued: 0,
146 stolen: 0,
147 failed: 0
148 },
149 runTime: {
150 history: expect.any(CircularArray)
151 },
152 waitTime: {
153 history: expect.any(CircularArray)
154 },
155 elu: {
156 idle: {
157 history: expect.any(CircularArray)
158 },
159 active: {
160 history: expect.any(CircularArray)
161 }
162 }
163 })
164 expect(workerNode.taskFunctionsUsage.size).toBe(2)
165 })
166 })