9071b5712e4f3ec71f705da9e4cc9f2eadbae996
[poolifier.git] / tests / pools / selection-strategies.test.js
1 const { expect } = require('expect')
2 const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
5 FixedThreadPool
6 } = require('../../lib/index')
7
8 describe('Selection strategies test suite', () => {
9 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
10 expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
11 expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED')
12 })
13
14 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
15 const min = 0
16 const max = 3
17 const pool = new DynamicThreadPool(
18 min,
19 max,
20 './tests/worker-files/thread/testWorker.js'
21 )
22 expect(pool.opts.workerChoiceStrategy).toBe(
23 WorkerChoiceStrategies.ROUND_ROBIN
24 )
25 // We need to clean up the resources after our test
26 await pool.destroy()
27 })
28
29 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
30 const min = 0
31 const max = 3
32 const pool = new DynamicThreadPool(
33 min,
34 max,
35 './tests/worker-files/thread/testWorker.js'
36 )
37 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
38 expect(pool.opts.workerChoiceStrategy).toBe(
39 WorkerChoiceStrategies.ROUND_ROBIN
40 )
41 // We need to clean up the resources after our test
42 await pool.destroy()
43 })
44
45 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
46 const max = 3
47 const pool = new FixedThreadPool(
48 max,
49 './tests/worker-files/thread/testWorker.js',
50 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
51 )
52 expect(pool.opts.workerChoiceStrategy).toBe(
53 WorkerChoiceStrategies.LESS_RECENTLY_USED
54 )
55 // We need to clean up the resources after our test
56 await pool.destroy()
57 })
58
59 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
60 const max = 3
61 const pool = new FixedThreadPool(
62 max,
63 './tests/worker-files/thread/testWorker.js'
64 )
65 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
66 expect(pool.opts.workerChoiceStrategy).toBe(
67 WorkerChoiceStrategies.LESS_RECENTLY_USED
68 )
69 // We need to clean up the resources after our test
70 await pool.destroy()
71 })
72
73 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
74 const max = 3
75 const pool = new FixedThreadPool(
76 max,
77 './tests/worker-files/thread/testWorker.js',
78 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
79 )
80 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
81 const promises = []
82 for (let i = 0; i < max * 2; i++) {
83 promises.push(pool.execute({ test: 'test' }))
84 }
85 await Promise.all(promises)
86 // We need to clean up the resources after our test
87 await pool.destroy()
88 })
89
90 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
91 const min = 0
92 const max = 3
93 const pool = new DynamicThreadPool(
94 min,
95 max,
96 './tests/worker-files/thread/testWorker.js',
97 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
98 )
99 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
100 const promises = []
101 for (let i = 0; i < max * 2; i++) {
102 promises.push(pool.execute({ test: 'test' }))
103 }
104 await Promise.all(promises)
105 // We need to clean up the resources after our test
106 await pool.destroy()
107 })
108
109 it('Verify unknown strategies throw error', () => {
110 const min = 1
111 const max = 3
112 expect(
113 () =>
114 new DynamicThreadPool(
115 min,
116 max,
117 './tests/worker-files/thread/testWorker.js',
118 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
119 )
120 ).toThrowError(
121 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
122 )
123 })
124 })