chore(deps-dev): bump tatami-ng to 0.6.0
[poolifier.git] / tests / pools / selection-strategies / selection-strategies-utils.test.mjs
1 import { expect } from 'expect'
2
3 import { FixedClusterPool, FixedThreadPool } from '../../../lib/index.cjs'
4 import {
5 buildWorkerChoiceStrategyOptions,
6 getWorkerChoiceStrategiesRetries,
7 } from '../../../lib/pools/selection-strategies/selection-strategies-utils.cjs'
8
9 describe('Selection strategies utils test suite', () => {
10 const numberOfWorkers = 4
11 const numberOfThreads = 4
12 let clusterFixedPool, threadFixedPool
13
14 before('Create pools', () => {
15 clusterFixedPool = new FixedClusterPool(
16 numberOfWorkers,
17 './tests/worker-files/cluster/testWorker.cjs'
18 )
19 threadFixedPool = new FixedThreadPool(
20 numberOfThreads,
21 './tests/worker-files/thread/testWorker.mjs'
22 )
23 })
24
25 after('Destroy pools', async () => {
26 await clusterFixedPool.destroy()
27 await threadFixedPool.destroy()
28 })
29
30 it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => {
31 expect(buildWorkerChoiceStrategyOptions(clusterFixedPool)).toStrictEqual({
32 elu: { median: false },
33 runTime: { median: false },
34 waitTime: { median: false },
35 weights: expect.objectContaining({
36 0: expect.any(Number),
37 [clusterFixedPool.info.maxSize - 1]: expect.any(Number),
38 }),
39 })
40 const workerChoiceStrategyOptions = {
41 elu: { median: true },
42 runTime: { median: true },
43 waitTime: { median: true },
44 weights: {
45 0: 100,
46 1: 100,
47 },
48 }
49 expect(
50 buildWorkerChoiceStrategyOptions(
51 clusterFixedPool,
52 workerChoiceStrategyOptions
53 )
54 ).toStrictEqual(workerChoiceStrategyOptions)
55 })
56
57 it('Verify getWorkerChoiceStrategyRetries() behavior', async () => {
58 expect(getWorkerChoiceStrategiesRetries(threadFixedPool)).toBe(
59 threadFixedPool.info.maxSize * 2
60 )
61 const workerChoiceStrategyOptions = {
62 elu: { median: true },
63 runTime: { median: true },
64 waitTime: { median: true },
65 weights: {
66 0: 100,
67 1: 100,
68 },
69 }
70 expect(
71 getWorkerChoiceStrategiesRetries(
72 threadFixedPool,
73 workerChoiceStrategyOptions
74 )
75 ).toBe(
76 threadFixedPool.info.maxSize +
77 Object.keys(workerChoiceStrategyOptions.weights).length
78 )
79 })
80 })