| 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 fixed 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 LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => { |
| 61 | const min = 0 |
| 62 | const max = 3 |
| 63 | const pool = new DynamicThreadPool( |
| 64 | min, |
| 65 | max, |
| 66 | './tests/worker-files/thread/testWorker.js', |
| 67 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } |
| 68 | ) |
| 69 | // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` |
| 70 | const promises = [] |
| 71 | for (let i = 0; i < max * 2; i++) { |
| 72 | promises.push(pool.execute({ test: 'test' })) |
| 73 | } |
| 74 | await Promise.all(promises) |
| 75 | |
| 76 | // We need to clean up the resources after our test |
| 77 | await pool.destroy() |
| 78 | }) |
| 79 | |
| 80 | it('Verify unknown strategies throw error', () => { |
| 81 | const min = 1 |
| 82 | const max = 3 |
| 83 | expect( |
| 84 | () => |
| 85 | new DynamicThreadPool( |
| 86 | min, |
| 87 | max, |
| 88 | './tests/worker-files/thread/testWorker.js', |
| 89 | { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } |
| 90 | ) |
| 91 | ).toThrowError( |
| 92 | new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") |
| 93 | ) |
| 94 | }) |
| 95 | }) |