Commit | Line | Data |
---|---|---|
26fb3c18 | 1 | const { MessageChannel, Worker } = require('worker_threads') |
75de9f41 | 2 | const cluster = require('cluster') |
26fb3c18 JB |
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') | |
6cd5248f | 8 | const { DEFAULT_TASK_NAME } = require('../../../lib/utils') |
26fb3c18 JB |
9 | |
10 | describe('Worker node test suite', () => { | |
75de9f41 JB |
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) | |
26fb3c18 JB |
15 | |
16 | it('Worker node instantiation', () => { | |
17 | expect(() => new WorkerNode()).toThrowError( | |
18 | new TypeError('Cannot construct a worker node without a worker') | |
19 | ) | |
75de9f41 | 20 | expect(() => new WorkerNode(threadWorker)).toThrowError( |
26fb3c18 JB |
21 | new TypeError( |
22 | 'Cannot construct a worker node without a tasks queue back pressure size' | |
23 | ) | |
24 | ) | |
25 | expect( | |
75de9f41 | 26 | () => new WorkerNode(threadWorker, 'invalidTasksQueueBackPressureSize') |
26fb3c18 JB |
27 | ).toThrowError( |
28 | new TypeError( | |
29 | 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' | |
30 | ) | |
31 | ) | |
75de9f41 JB |
32 | expect(threadWorkerNode).toBeInstanceOf(WorkerNode) |
33 | expect(threadWorkerNode.worker).toBe(threadWorker) | |
34 | expect(threadWorkerNode.info).toStrictEqual({ | |
35 | id: threadWorker.threadId, | |
26fb3c18 JB |
36 | type: WorkerTypes.thread, |
37 | dynamic: false, | |
38 | ready: false | |
39 | }) | |
75de9f41 JB |
40 | expect(threadWorkerNode.usage).toStrictEqual({ |
41 | tasks: { | |
42 | executed: 0, | |
43 | executing: 0, | |
44 | queued: 0, | |
45 | maxQueued: 0, | |
46 | stolen: 0, | |
47 | failed: 0 | |
48 | }, | |
49 | runTime: { | |
4ba4c7f9 | 50 | history: new CircularArray() |
75de9f41 JB |
51 | }, |
52 | waitTime: { | |
4ba4c7f9 | 53 | history: new CircularArray() |
75de9f41 JB |
54 | }, |
55 | elu: { | |
56 | idle: { | |
4ba4c7f9 | 57 | history: new CircularArray() |
75de9f41 JB |
58 | }, |
59 | active: { | |
4ba4c7f9 | 60 | history: new CircularArray() |
75de9f41 JB |
61 | } |
62 | } | |
63 | }) | |
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) | |
69 | ||
70 | expect(clusterWorkerNode).toBeInstanceOf(WorkerNode) | |
71 | expect(clusterWorkerNode.worker).toBe(clusterWorker) | |
72 | expect(clusterWorkerNode.info).toStrictEqual({ | |
73 | id: clusterWorker.id, | |
74 | type: WorkerTypes.cluster, | |
75 | dynamic: false, | |
76 | ready: false | |
77 | }) | |
78 | expect(clusterWorkerNode.usage).toStrictEqual({ | |
26fb3c18 JB |
79 | tasks: { |
80 | executed: 0, | |
81 | executing: 0, | |
82 | queued: 0, | |
83 | maxQueued: 0, | |
84 | stolen: 0, | |
85 | failed: 0 | |
86 | }, | |
87 | runTime: { | |
4ba4c7f9 | 88 | history: new CircularArray() |
26fb3c18 JB |
89 | }, |
90 | waitTime: { | |
4ba4c7f9 | 91 | history: new CircularArray() |
26fb3c18 JB |
92 | }, |
93 | elu: { | |
94 | idle: { | |
4ba4c7f9 | 95 | history: new CircularArray() |
26fb3c18 JB |
96 | }, |
97 | active: { | |
4ba4c7f9 | 98 | history: new CircularArray() |
26fb3c18 JB |
99 | } |
100 | } | |
101 | }) | |
75de9f41 JB |
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) | |
26fb3c18 JB |
107 | }) |
108 | ||
109 | it('Worker node getTaskFunctionWorkerUsage()', () => { | |
110 | expect(() => | |
75de9f41 | 111 | threadWorkerNode.getTaskFunctionWorkerUsage('invalidTaskFunction') |
26fb3c18 JB |
112 | ).toThrowError( |
113 | new TypeError( | |
114 | "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list is not yet defined" | |
115 | ) | |
116 | ) | |
75de9f41 | 117 | threadWorkerNode.info.taskFunctions = [DEFAULT_TASK_NAME, 'fn1'] |
26fb3c18 | 118 | expect(() => |
75de9f41 | 119 | threadWorkerNode.getTaskFunctionWorkerUsage('invalidTaskFunction') |
26fb3c18 JB |
120 | ).toThrowError( |
121 | new TypeError( | |
122 | "Cannot get task function worker usage for task function name 'invalidTaskFunction' when task function names list has less than 3 elements" | |
123 | ) | |
124 | ) | |
75de9f41 | 125 | threadWorkerNode.info.taskFunctions = [DEFAULT_TASK_NAME, 'fn1', 'fn2'] |
6cd5248f | 126 | expect( |
75de9f41 | 127 | threadWorkerNode.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME) |
6cd5248f | 128 | ).toStrictEqual({ |
26fb3c18 JB |
129 | tasks: { |
130 | executed: 0, | |
131 | executing: 0, | |
132 | queued: 0, | |
133 | stolen: 0, | |
134 | failed: 0 | |
135 | }, | |
136 | runTime: { | |
4ba4c7f9 | 137 | history: new CircularArray() |
26fb3c18 JB |
138 | }, |
139 | waitTime: { | |
4ba4c7f9 | 140 | history: new CircularArray() |
26fb3c18 JB |
141 | }, |
142 | elu: { | |
143 | idle: { | |
4ba4c7f9 | 144 | history: new CircularArray() |
26fb3c18 JB |
145 | }, |
146 | active: { | |
4ba4c7f9 | 147 | history: new CircularArray() |
26fb3c18 JB |
148 | } |
149 | } | |
150 | }) | |
75de9f41 | 151 | expect(threadWorkerNode.getTaskFunctionWorkerUsage('fn1')).toStrictEqual({ |
26fb3c18 JB |
152 | tasks: { |
153 | executed: 0, | |
154 | executing: 0, | |
155 | queued: 0, | |
156 | stolen: 0, | |
157 | failed: 0 | |
158 | }, | |
159 | runTime: { | |
4ba4c7f9 | 160 | history: new CircularArray() |
26fb3c18 JB |
161 | }, |
162 | waitTime: { | |
4ba4c7f9 | 163 | history: new CircularArray() |
26fb3c18 JB |
164 | }, |
165 | elu: { | |
166 | idle: { | |
4ba4c7f9 | 167 | history: new CircularArray() |
26fb3c18 JB |
168 | }, |
169 | active: { | |
4ba4c7f9 | 170 | history: new CircularArray() |
26fb3c18 JB |
171 | } |
172 | } | |
173 | }) | |
75de9f41 | 174 | expect(threadWorkerNode.getTaskFunctionWorkerUsage('fn2')).toStrictEqual({ |
26fb3c18 JB |
175 | tasks: { |
176 | executed: 0, | |
177 | executing: 0, | |
178 | queued: 0, | |
179 | stolen: 0, | |
180 | failed: 0 | |
181 | }, | |
182 | runTime: { | |
4ba4c7f9 | 183 | history: new CircularArray() |
26fb3c18 JB |
184 | }, |
185 | waitTime: { | |
4ba4c7f9 | 186 | history: new CircularArray() |
26fb3c18 JB |
187 | }, |
188 | elu: { | |
189 | idle: { | |
4ba4c7f9 | 190 | history: new CircularArray() |
26fb3c18 JB |
191 | }, |
192 | active: { | |
4ba4c7f9 | 193 | history: new CircularArray() |
26fb3c18 JB |
194 | } |
195 | } | |
196 | }) | |
75de9f41 | 197 | expect(threadWorkerNode.taskFunctionsUsage.size).toBe(2) |
26fb3c18 JB |
198 | }) |
199 | }) |