Check that a pool have a minimum number of workers (#213)
[poolifier.git] / tests / pools / selection-strategies.test.js
1 const expect = require('expect')
2 const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
5 FixedThreadPool
6 } = require('../../lib/index')
7
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 })
13
14 it('Verify LESS_RECENTLY_USED strategy is taken', async () => {
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 )
21
22 expect(pool.opts.workerChoiceStrategy).toBe(
23 WorkerChoiceStrategies.LESS_RECENTLY_USED
24 )
25
26 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
27 const promises = []
28 for (let i = 0; i < max * 2; i++) {
29 promises.push(pool.execute({ test: 'test' }))
30 }
31 await Promise.all(promises)
32
33 // We need to clean up the resources after our test
34 await pool.destroy()
35 })
36
37 it('Verify unknown strategies throw error', () => {
38 const min = 1
39 const max = 3
40 expect(
41 () =>
42 new DynamicThreadPool(
43 min,
44 max,
45 './tests/worker-files/thread/testWorker.js',
46 {
47 maxTasks: 1000,
48 workerChoiceStrategy: 'UNKNOWN_STRATEGY'
49 }
50 )
51 ).toThrowError(
52 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
53 )
54 })
55 })