Merge dependabot/npm_and_yarn/examples/typescript/websocket-server-pool/ws-hybrid...
[poolifier.git] / tests / pools / utils.test.mjs
... / ...
CommitLineData
1import { Worker as ThreadWorker } from 'node:worker_threads'
2import { Worker as ClusterWorker } from 'node:cluster'
3import { expect } from 'expect'
4import {
5 CircularArray,
6 DEFAULT_CIRCULAR_ARRAY_SIZE
7} from '../../lib/circular-array.cjs'
8import {
9 createWorker,
10 getDefaultTasksQueueOptions,
11 updateMeasurementStatistics
12} from '../../lib/pools/utils.cjs'
13import { WorkerTypes } from '../../lib/index.cjs'
14
15describe('Pool utils test suite', () => {
16 it('Verify getDefaultTasksQueueOptions() behavior', () => {
17 const poolMaxSize = 4
18 expect(getDefaultTasksQueueOptions(poolMaxSize)).toStrictEqual({
19 concurrency: 1,
20 size: Math.pow(poolMaxSize, 2),
21 taskStealing: true,
22 tasksStealingOnBackPressure: true,
23 tasksFinishedTimeout: 2000
24 })
25 })
26
27 it('Verify updateMeasurementStatistics() behavior', () => {
28 const measurementStatistics = {
29 history: new CircularArray()
30 }
31 updateMeasurementStatistics(
32 measurementStatistics,
33 { aggregate: true, average: false, median: false },
34 0.01
35 )
36 expect(measurementStatistics).toStrictEqual({
37 aggregate: 0.01,
38 maximum: 0.01,
39 minimum: 0.01,
40 history: new CircularArray()
41 })
42 updateMeasurementStatistics(
43 measurementStatistics,
44 { aggregate: true, average: false, median: false },
45 0.02
46 )
47 expect(measurementStatistics).toStrictEqual({
48 aggregate: 0.03,
49 maximum: 0.02,
50 minimum: 0.01,
51 history: new CircularArray()
52 })
53 updateMeasurementStatistics(
54 measurementStatistics,
55 { aggregate: true, average: true, median: false },
56 0.001
57 )
58 expect(measurementStatistics).toStrictEqual({
59 aggregate: 0.031,
60 maximum: 0.02,
61 minimum: 0.001,
62 average: 0.001,
63 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001)
64 })
65 updateMeasurementStatistics(
66 measurementStatistics,
67 { aggregate: true, average: true, median: false },
68 0.003
69 )
70 expect(measurementStatistics).toStrictEqual({
71 aggregate: 0.034,
72 maximum: 0.02,
73 minimum: 0.001,
74 average: 0.002,
75 history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001, 0.003)
76 })
77 updateMeasurementStatistics(
78 measurementStatistics,
79 { aggregate: true, average: false, median: true },
80 0.006
81 )
82 expect(measurementStatistics).toStrictEqual({
83 aggregate: 0.04,
84 maximum: 0.02,
85 minimum: 0.001,
86 median: 0.003,
87 history: new CircularArray(
88 DEFAULT_CIRCULAR_ARRAY_SIZE,
89 0.001,
90 0.003,
91 0.006
92 )
93 })
94 updateMeasurementStatistics(
95 measurementStatistics,
96 { aggregate: true, average: true, median: false },
97 0.01
98 )
99 expect(measurementStatistics).toStrictEqual({
100 aggregate: 0.05,
101 maximum: 0.02,
102 minimum: 0.001,
103 average: 0.005,
104 history: new CircularArray(
105 DEFAULT_CIRCULAR_ARRAY_SIZE,
106 0.001,
107 0.003,
108 0.006,
109 0.01
110 )
111 })
112 })
113
114 it('Verify createWorker() behavior', () => {
115 expect(
116 createWorker(
117 WorkerTypes.thread,
118 './tests/worker-files/thread/testWorker.mjs'
119 )
120 ).toBeInstanceOf(ThreadWorker)
121 expect(
122 createWorker(
123 WorkerTypes.cluster,
124 './tests/worker-files/cluster/testWorker.mjs'
125 )
126 ).toBeInstanceOf(ClusterWorker)
127 })
128})