| 1 | const expect = require('expect') |
| 2 | const { |
| 3 | WorkerChoiceStrategies, |
| 4 | DynamicThreadPool, |
| 5 | FixedThreadPool |
| 6 | } = require('../../lib/index') |
| 7 | const TestUtils = require('../test-utils') |
| 8 | |
| 9 | describe('Selection strategies test suite', () => { |
| 10 | it('Verify that WorkerChoiceStrategies enumeration provides string values', () => { |
| 11 | expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN') |
| 12 | expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED') |
| 13 | }) |
| 14 | |
| 15 | it('Verify LESS_RECENTLY_USED is taken', async () => { |
| 16 | const max = 3 |
| 17 | const pool = new FixedThreadPool( |
| 18 | max, |
| 19 | './tests/worker-files/thread/testWorker.js', |
| 20 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } |
| 21 | ) |
| 22 | |
| 23 | expect(pool.opts.workerChoiceStrategy).toBe( |
| 24 | WorkerChoiceStrategies.LESS_RECENTLY_USED |
| 25 | ) |
| 26 | |
| 27 | // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` |
| 28 | const promises = [] |
| 29 | for (let i = 0; i < max * 2; i++) { |
| 30 | promises.push(pool.execute({ test: 'test' })) |
| 31 | } |
| 32 | await Promise.all(promises) |
| 33 | |
| 34 | // We need to clean up the resources after our test |
| 35 | await pool.destroy() |
| 36 | }) |
| 37 | |
| 38 | it('Verify unknown strategies throw error', () => { |
| 39 | const min = 1 |
| 40 | const max = 3 |
| 41 | expect( |
| 42 | () => |
| 43 | new DynamicThreadPool( |
| 44 | min, |
| 45 | max, |
| 46 | './tests/worker-files/thread/testWorker.js', |
| 47 | { |
| 48 | maxTasks: 1000, |
| 49 | workerChoiceStrategy: 'UNKNOWN_STRATEGY' |
| 50 | } |
| 51 | ) |
| 52 | ).toThrowError( |
| 53 | new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") |
| 54 | ) |
| 55 | }) |
| 56 | }) |