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 LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
16 const pool
= new FixedThreadPool(
18 './tests/worker-files/thread/testWorker.js',
19 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
21 expect(pool
.opts
.workerChoiceStrategy
).toBe(
22 WorkerChoiceStrategies
.LESS_RECENTLY_USED
24 // We need to clean up the resources after our test
28 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
30 const pool
= new FixedThreadPool(
32 './tests/worker-files/thread/testWorker.js'
34 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
35 expect(pool
.opts
.workerChoiceStrategy
).toBe(
36 WorkerChoiceStrategies
.LESS_RECENTLY_USED
38 // We need to clean up the resources after our test
42 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
44 const pool
= new FixedThreadPool(
46 './tests/worker-files/thread/testWorker.js',
47 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
49 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
51 for (let i
= 0; i
< max
* 2; i
++) {
52 promises
.push(pool
.execute({ test
: 'test' }))
54 await Promise
.all(promises
)
56 // We need to clean up the resources after our test
60 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
63 const pool
= new DynamicThreadPool(
66 './tests/worker-files/thread/testWorker.js',
67 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
69 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
71 for (let i
= 0; i
< max
* 2; i
++) {
72 promises
.push(pool
.execute({ test
: 'test' }))
74 await Promise
.all(promises
)
76 // We need to clean up the resources after our test
80 it('Verify unknown strategies throw error', () => {
85 new DynamicThreadPool(
88 './tests/worker-files/thread/testWorker.js',
89 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
92 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")