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