| 1 | const expect = require('expect') |
| 2 | const { FixedClusterPool, FixedThreadPool } = require('../../../lib/index') |
| 3 | const expectedError = new Error('Worker could not be found in tasks map') |
| 4 | |
| 5 | class StubPoolWithTasksMapClear extends FixedThreadPool { |
| 6 | removeAllWorker () { |
| 7 | this.tasks.clear() |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | class StubPoolWithIsMainMethod extends FixedThreadPool { |
| 12 | isMain () { |
| 13 | return false |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | describe('Abstract pool test suite', () => { |
| 18 | it('Simulate worker not found during increaseWorkersTask', () => { |
| 19 | const pool = new StubPoolWithTasksMapClear( |
| 20 | 1, |
| 21 | './tests/worker-files/thread/testWorker.js' |
| 22 | ) |
| 23 | // Simulate worker not found. |
| 24 | pool.removeAllWorker() |
| 25 | expect(() => pool.increaseWorkersTask()).toThrowError(expectedError) |
| 26 | }) |
| 27 | |
| 28 | it('Simulate worker not found during decreaseWorkersTasks', () => { |
| 29 | const pool = new StubPoolWithTasksMapClear( |
| 30 | 1, |
| 31 | './tests/worker-files/thread/testWorker.js', |
| 32 | { |
| 33 | errorHandler: e => console.error(e) |
| 34 | } |
| 35 | ) |
| 36 | // Simulate worker not found. |
| 37 | pool.removeAllWorker() |
| 38 | expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError) |
| 39 | }) |
| 40 | |
| 41 | it('Simulate pool creation from a non main thread/process', () => { |
| 42 | expect( |
| 43 | () => |
| 44 | new StubPoolWithIsMainMethod( |
| 45 | 1, |
| 46 | './tests/worker-files/thread/testWorker.js', |
| 47 | { |
| 48 | errorHandler: e => console.error(e) |
| 49 | } |
| 50 | ) |
| 51 | ).toThrowError(new Error('Cannot start a pool from a worker!')) |
| 52 | }) |
| 53 | |
| 54 | it('Verify that filePath is checked', () => { |
| 55 | expect(() => new StubPoolWithIsMainMethod(1)).toThrowError( |
| 56 | new Error('Cannot start a pool from a worker!') |
| 57 | ) |
| 58 | expect(() => new StubPoolWithIsMainMethod(1, '')).toThrowError( |
| 59 | new Error('Cannot start a pool from a worker!') |
| 60 | ) |
| 61 | }) |
| 62 | |
| 63 | it('Verify that numberOfWorkers is checked', () => { |
| 64 | expect(() => new FixedThreadPool()).toThrowError( |
| 65 | new Error( |
| 66 | 'Cannot instantiate a pool without specifying the number of workers' |
| 67 | ) |
| 68 | ) |
| 69 | }) |
| 70 | |
| 71 | it('Verify that a negative number of workers is checked', () => { |
| 72 | expect( |
| 73 | () => |
| 74 | new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js') |
| 75 | ).toThrowError( |
| 76 | new Error('Cannot instantiate a pool with a negative number of workers') |
| 77 | ) |
| 78 | }) |
| 79 | |
| 80 | it('Verify that a non integer number of workers is checked', () => { |
| 81 | expect( |
| 82 | () => |
| 83 | new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') |
| 84 | ).toThrowError( |
| 85 | new Error( |
| 86 | 'Cannot instantiate a pool with a non integer number of workers' |
| 87 | ) |
| 88 | ) |
| 89 | }) |
| 90 | }) |