Removed max tasks (#225)
[poolifier.git] / tests / pools / selection-strategies.test.js
CommitLineData
a35560ba
S
1const expect = require('expect')
2const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
5 FixedThreadPool
6} = require('../../lib/index')
a35560ba
S
7
8describe('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
b98ec2e6 14 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
a35560ba
S
15 const max = 3
16 const pool = new FixedThreadPool(
17 max,
18 './tests/worker-files/thread/testWorker.js',
19 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
20 )
b98ec2e6
JB
21 expect(pool.opts.workerChoiceStrategy).toBe(
22 WorkerChoiceStrategies.LESS_RECENTLY_USED
23 )
24 // We need to clean up the resources after our test
25 await pool.destroy()
26 })
a35560ba 27
b98ec2e6
JB
28 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
29 const max = 3
30 const pool = new FixedThreadPool(
31 max,
32 './tests/worker-files/thread/testWorker.js'
33 )
34 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
a35560ba
S
35 expect(pool.opts.workerChoiceStrategy).toBe(
36 WorkerChoiceStrategies.LESS_RECENTLY_USED
37 )
b98ec2e6
JB
38 // We need to clean up the resources after our test
39 await pool.destroy()
40 })
a35560ba 41
b98ec2e6
JB
42 it('Verify LESS_RECENTLY_USED strategy can be run in a pool', async () => {
43 const max = 3
44 const pool = new FixedThreadPool(
45 max,
46 './tests/worker-files/thread/testWorker.js',
47 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
48 )
a35560ba
S
49 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
50 const promises = []
51 for (let i = 0; i < max * 2; i++) {
52 promises.push(pool.execute({ test: 'test' }))
53 }
54 await Promise.all(promises)
55
56 // We need to clean up the resources after our test
57 await pool.destroy()
58 })
59
60 it('Verify unknown strategies throw error', () => {
61 const min = 1
62 const max = 3
63 expect(
64 () =>
65 new DynamicThreadPool(
66 min,
67 max,
68 './tests/worker-files/thread/testWorker.js',
1927ee67 69 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
a35560ba
S
70 )
71 ).toThrowError(
72 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
73 )
74 })
75})