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 LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
47 const pool
= new FixedThreadPool(
49 './tests/worker-files/thread/testWorker.js',
50 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
52 expect(pool
.opts
.workerChoiceStrategy
).toBe(
53 WorkerChoiceStrategies
.LESS_RECENTLY_USED
55 // We need to clean up the resources after our test
59 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
61 const pool
= new FixedThreadPool(
63 './tests/worker-files/thread/testWorker.js'
65 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
66 expect(pool
.opts
.workerChoiceStrategy
).toBe(
67 WorkerChoiceStrategies
.LESS_RECENTLY_USED
69 // We need to clean up the resources after our test
73 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
75 const pool
= new FixedThreadPool(
77 './tests/worker-files/thread/testWorker.js',
78 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
80 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#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 can be run in a dynamic pool', async () => {
93 const pool
= new DynamicThreadPool(
96 './tests/worker-files/thread/testWorker.js',
97 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
99 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
101 for (let i
= 0; i
< max
* 2; i
++) {
102 promises
.push(pool
.execute({ test
: 'test' }))
104 await Promise
.all(promises
)
105 // We need to clean up the resources after our test
109 it('Verify unknown strategies throw error', () => {
114 new DynamicThreadPool(
117 './tests/worker-files/thread/testWorker.js',
118 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
121 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")