| 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 at pool creation', 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 | 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 | }) |
| 27 | |
| 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) |
| 35 | expect(pool.opts.workerChoiceStrategy).toBe( |
| 36 | WorkerChoiceStrategies.LESS_RECENTLY_USED |
| 37 | ) |
| 38 | // We need to clean up the resources after our test |
| 39 | await pool.destroy() |
| 40 | }) |
| 41 | |
| 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 | ) |
| 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', |
| 69 | { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } |
| 70 | ) |
| 71 | ).toThrowError( |
| 72 | new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") |
| 73 | ) |
| 74 | }) |
| 75 | }) |