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')
14 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
17 const pool
= new DynamicThreadPool(
20 './tests/worker-files/thread/testWorker.js'
22 expect(pool
.opts
.workerChoiceStrategy
).toBe(
23 WorkerChoiceStrategies
.ROUND_ROBIN
25 // We need to clean up the resources after our test
29 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
32 const pool
= new DynamicThreadPool(
35 './tests/worker-files/thread/testWorker.js'
37 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
38 expect(pool
.opts
.workerChoiceStrategy
).toBe(
39 WorkerChoiceStrategies
.ROUND_ROBIN
41 // We need to clean up the resources after our test
45 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
47 const pool
= new FixedThreadPool(
49 './tests/worker-files/thread/testWorker.js',
50 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
52 expect(pool
.opts
.workerChoiceStrategy
).toBe(
53 WorkerChoiceStrategies
.ROUND_ROBIN
55 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
57 for (let i
= 0; i
< max
* 2; i
++) {
58 promises
.push(pool
.execute({ test
: 'test' }))
60 await Promise
.all(promises
)
61 // We need to clean up the resources after our test
65 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
68 const pool
= new DynamicThreadPool(
71 './tests/worker-files/thread/testWorker.js',
72 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
74 expect(pool
.opts
.workerChoiceStrategy
).toBe(
75 WorkerChoiceStrategies
.ROUND_ROBIN
77 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
79 for (let i
= 0; i
< max
* 2; i
++) {
80 promises
.push(pool
.execute({ test
: 'test' }))
82 await Promise
.all(promises
)
83 // We need to clean up the resources after our test
87 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
89 const pool
= new FixedThreadPool(
91 './tests/worker-files/thread/testWorker.js',
92 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
94 expect(pool
.opts
.workerChoiceStrategy
).toBe(
95 WorkerChoiceStrategies
.LESS_RECENTLY_USED
97 // We need to clean up the resources after our test
101 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
103 const pool
= new FixedThreadPool(
105 './tests/worker-files/thread/testWorker.js'
107 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
108 expect(pool
.opts
.workerChoiceStrategy
).toBe(
109 WorkerChoiceStrategies
.LESS_RECENTLY_USED
111 // We need to clean up the resources after our test
115 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
117 const pool
= new FixedThreadPool(
119 './tests/worker-files/thread/testWorker.js',
120 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
122 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
124 for (let i
= 0; i
< max
* 2; i
++) {
125 promises
.push(pool
.execute({ test
: 'test' }))
127 await Promise
.all(promises
)
128 // We need to clean up the resources after our test
132 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
135 const pool
= new DynamicThreadPool(
138 './tests/worker-files/thread/testWorker.js',
139 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
141 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
143 for (let i
= 0; i
< max
* 2; i
++) {
144 promises
.push(pool
.execute({ test
: 'test' }))
146 await Promise
.all(promises
)
147 // We need to clean up the resources after our test
151 it('Verify unknown strategies throw error', () => {
156 new DynamicThreadPool(
159 './tests/worker-files/thread/testWorker.js',
160 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
163 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")