Commit | Line | Data |
---|---|---|
e9ed6eee | 1 | import cluster, { Worker as ClusterWorker } from 'node:cluster' |
ded253e2 JB |
2 | import { Worker as ThreadWorker } from 'node:worker_threads' |
3 | ||
a074ffee | 4 | import { expect } from 'expect' |
ded253e2 | 5 | |
a074ffee JB |
6 | import { |
7 | CircularArray, | |
8 | DEFAULT_CIRCULAR_ARRAY_SIZE | |
d35e5717 | 9 | } from '../../lib/circular-array.cjs' |
bcfb06ce | 10 | import { WorkerTypes } from '../../lib/index.cjs' |
c3719753 JB |
11 | import { |
12 | createWorker, | |
ded253e2 | 13 | DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
32b141fd | 14 | getDefaultTasksQueueOptions, |
e9ed6eee JB |
15 | getWorkerId, |
16 | getWorkerType, | |
c3719753 | 17 | updateMeasurementStatistics |
d35e5717 | 18 | } from '../../lib/pools/utils.cjs' |
bfc75cca JB |
19 | |
20 | describe('Pool utils test suite', () => { | |
e9ed6eee JB |
21 | it('Verify DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS values', () => { |
22 | expect(DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS).toStrictEqual({ | |
23 | aggregate: false, | |
24 | average: false, | |
25 | median: false | |
26 | }) | |
27 | }) | |
28 | ||
32b141fd JB |
29 | it('Verify getDefaultTasksQueueOptions() behavior', () => { |
30 | const poolMaxSize = 4 | |
31 | expect(getDefaultTasksQueueOptions(poolMaxSize)).toStrictEqual({ | |
32 | concurrency: 1, | |
33 | size: Math.pow(poolMaxSize, 2), | |
34 | taskStealing: true, | |
2eee7220 | 35 | tasksStealingOnBackPressure: false, |
568d0075 | 36 | tasksFinishedTimeout: 2000 |
32b141fd JB |
37 | }) |
38 | }) | |
39 | ||
bfc75cca JB |
40 | it('Verify updateMeasurementStatistics() behavior', () => { |
41 | const measurementStatistics = { | |
42 | history: new CircularArray() | |
43 | } | |
44 | updateMeasurementStatistics( | |
45 | measurementStatistics, | |
46 | { aggregate: true, average: false, median: false }, | |
47 | 0.01 | |
48 | ) | |
49 | expect(measurementStatistics).toStrictEqual({ | |
50 | aggregate: 0.01, | |
51 | maximum: 0.01, | |
52 | minimum: 0.01, | |
53 | history: new CircularArray() | |
54 | }) | |
55 | updateMeasurementStatistics( | |
56 | measurementStatistics, | |
57 | { aggregate: true, average: false, median: false }, | |
58 | 0.02 | |
59 | ) | |
60 | expect(measurementStatistics).toStrictEqual({ | |
61 | aggregate: 0.03, | |
62 | maximum: 0.02, | |
63 | minimum: 0.01, | |
64 | history: new CircularArray() | |
65 | }) | |
66 | updateMeasurementStatistics( | |
67 | measurementStatistics, | |
68 | { aggregate: true, average: true, median: false }, | |
69 | 0.001 | |
70 | ) | |
71 | expect(measurementStatistics).toStrictEqual({ | |
72 | aggregate: 0.031, | |
73 | maximum: 0.02, | |
74 | minimum: 0.001, | |
75 | average: 0.001, | |
76 | history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001) | |
77 | }) | |
78 | updateMeasurementStatistics( | |
79 | measurementStatistics, | |
80 | { aggregate: true, average: true, median: false }, | |
81 | 0.003 | |
82 | ) | |
83 | expect(measurementStatistics).toStrictEqual({ | |
84 | aggregate: 0.034, | |
85 | maximum: 0.02, | |
86 | minimum: 0.001, | |
87 | average: 0.002, | |
88 | history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001, 0.003) | |
89 | }) | |
90 | updateMeasurementStatistics( | |
91 | measurementStatistics, | |
92 | { aggregate: true, average: false, median: true }, | |
93 | 0.006 | |
94 | ) | |
95 | expect(measurementStatistics).toStrictEqual({ | |
96 | aggregate: 0.04, | |
97 | maximum: 0.02, | |
98 | minimum: 0.001, | |
99 | median: 0.003, | |
100 | history: new CircularArray( | |
101 | DEFAULT_CIRCULAR_ARRAY_SIZE, | |
102 | 0.001, | |
103 | 0.003, | |
104 | 0.006 | |
105 | ) | |
106 | }) | |
107 | updateMeasurementStatistics( | |
108 | measurementStatistics, | |
109 | { aggregate: true, average: true, median: false }, | |
110 | 0.01 | |
111 | ) | |
112 | expect(measurementStatistics).toStrictEqual({ | |
113 | aggregate: 0.05, | |
114 | maximum: 0.02, | |
115 | minimum: 0.001, | |
116 | average: 0.005, | |
117 | history: new CircularArray( | |
118 | DEFAULT_CIRCULAR_ARRAY_SIZE, | |
119 | 0.001, | |
120 | 0.003, | |
121 | 0.006, | |
122 | 0.01 | |
123 | ) | |
124 | }) | |
125 | }) | |
c3719753 JB |
126 | |
127 | it('Verify createWorker() behavior', () => { | |
128 | expect( | |
129 | createWorker( | |
130 | WorkerTypes.thread, | |
c63a35a0 JB |
131 | './tests/worker-files/thread/testWorker.mjs', |
132 | {} | |
c3719753 JB |
133 | ) |
134 | ).toBeInstanceOf(ThreadWorker) | |
135 | expect( | |
136 | createWorker( | |
137 | WorkerTypes.cluster, | |
c63a35a0 JB |
138 | './tests/worker-files/cluster/testWorker.mjs', |
139 | {} | |
c3719753 JB |
140 | ) |
141 | ).toBeInstanceOf(ClusterWorker) | |
142 | }) | |
e9ed6eee JB |
143 | |
144 | it('Verify getWorkerType() behavior', () => { | |
145 | expect( | |
146 | getWorkerType( | |
147 | new ThreadWorker('./tests/worker-files/thread/testWorker.mjs') | |
148 | ) | |
149 | ).toBe(WorkerTypes.thread) | |
150 | expect(getWorkerType(cluster.fork())).toBe(WorkerTypes.cluster) | |
151 | }) | |
152 | ||
153 | it('Verify getWorkerId() behavior', () => { | |
154 | const threadWorker = new ThreadWorker( | |
155 | './tests/worker-files/thread/testWorker.mjs' | |
156 | ) | |
157 | const clusterWorker = cluster.fork() | |
158 | expect(getWorkerId(threadWorker)).toBe(threadWorker.threadId) | |
159 | expect(getWorkerId(clusterWorker)).toBe(clusterWorker.id) | |
160 | }) | |
bfc75cca | 161 | }) |