93649c39c2c667c8b5b518e9e1067abc8ad8b2cd
1 const { expect
} = require('expect')
3 WorkerChoiceStrategies
,
6 } = require('../../../lib/index')
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 expect(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
).toBe(
13 'WEIGHTED_ROUND_ROBIN'
17 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
20 const pool
= new DynamicThreadPool(
23 './tests/worker-files/thread/testWorker.js'
25 expect(pool
.opts
.workerChoiceStrategy
).toBe(
26 WorkerChoiceStrategies
.ROUND_ROBIN
28 // We need to clean up the resources after our test
32 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
35 const pool
= new DynamicThreadPool(
38 './tests/worker-files/thread/testWorker.js'
40 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
41 expect(pool
.opts
.workerChoiceStrategy
).toBe(
42 WorkerChoiceStrategies
.ROUND_ROBIN
44 // We need to clean up the resources after our test
48 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
50 const pool
= new FixedThreadPool(
52 './tests/worker-files/thread/testWorker.js',
53 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
55 expect(pool
.opts
.workerChoiceStrategy
).toBe(
56 WorkerChoiceStrategies
.ROUND_ROBIN
58 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
60 for (let i
= 0; i
< max
* 2; i
++) {
61 promises
.push(pool
.execute({ test
: 'test' }))
63 await Promise
.all(promises
)
64 // We need to clean up the resources after our test
68 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
71 const pool
= new DynamicThreadPool(
74 './tests/worker-files/thread/testWorker.js',
75 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
77 expect(pool
.opts
.workerChoiceStrategy
).toBe(
78 WorkerChoiceStrategies
.ROUND_ROBIN
80 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
82 for (let i
= 0; i
< max
* 2; i
++) {
83 promises
.push(pool
.execute({ test
: 'test' }))
85 await Promise
.all(promises
)
86 // We need to clean up the resources after our test
90 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
92 const pool
= new FixedThreadPool(
94 './tests/worker-files/thread/testWorker.js',
95 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
97 expect(pool
.opts
.workerChoiceStrategy
).toBe(
98 WorkerChoiceStrategies
.LESS_RECENTLY_USED
100 // We need to clean up the resources after our test
104 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
106 const pool
= new FixedThreadPool(
108 './tests/worker-files/thread/testWorker.js'
110 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
111 expect(pool
.opts
.workerChoiceStrategy
).toBe(
112 WorkerChoiceStrategies
.LESS_RECENTLY_USED
114 // We need to clean up the resources after our test
118 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
120 const pool
= new FixedThreadPool(
122 './tests/worker-files/thread/testWorker.js',
123 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
125 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
127 for (let i
= 0; i
< max
* 2; i
++) {
128 promises
.push(pool
.execute({ test
: 'test' }))
130 await Promise
.all(promises
)
131 // We need to clean up the resources after our test
135 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
138 const pool
= new DynamicThreadPool(
141 './tests/worker-files/thread/testWorker.js',
142 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
144 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
146 for (let i
= 0; i
< max
* 2; i
++) {
147 promises
.push(pool
.execute({ test
: 'test' }))
149 await Promise
.all(promises
)
150 // We need to clean up the resources after our test
154 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
156 const pool
= new FixedThreadPool(
158 './tests/worker-files/thread/testWorker.js',
159 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
161 expect(pool
.opts
.workerChoiceStrategy
).toBe(
162 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
164 // We need to clean up the resources after our test
168 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
170 const pool
= new FixedThreadPool(
172 './tests/worker-files/thread/testWorker.js'
174 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
175 expect(pool
.opts
.workerChoiceStrategy
).toBe(
176 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
178 // We need to clean up the resources after our test
182 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
184 const pool
= new FixedThreadPool(
186 './tests/worker-files/thread/testWorker.js',
187 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
189 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
191 for (let i
= 0; i
< max
* 2; i
++) {
192 promises
.push(pool
.execute({ test
: 'test' }))
194 await Promise
.all(promises
)
195 // We need to clean up the resources after our test
199 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
202 const pool
= new DynamicThreadPool(
205 './tests/worker-files/thread/testWorker.js',
206 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
208 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
210 for (let i
= 0; i
< max
* 2; i
++) {
211 promises
.push(pool
.execute({ test
: 'test' }))
213 await Promise
.all(promises
)
214 // We need to clean up the resources after our test
218 it('Verify unknown strategies throw error', () => {
223 new DynamicThreadPool(
226 './tests/worker-files/thread/testWorker.js',
227 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
230 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")