| 1 | const expect = require('expect') |
| 2 | const { |
| 3 | FixedClusterPool, |
| 4 | FixedThreadPool, |
| 5 | WorkerChoiceStrategies |
| 6 | } = require('../../../lib/index') |
| 7 | const expectedError = new Error('Worker could not be found in tasks map') |
| 8 | |
| 9 | const numberOfWorkers = 1 |
| 10 | |
| 11 | class StubPoolWithTasksMapClear extends FixedThreadPool { |
| 12 | removeAllWorker () { |
| 13 | this.tasks.clear() |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | class StubPoolWithIsMainMethod extends FixedThreadPool { |
| 18 | isMain () { |
| 19 | return false |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | describe('Abstract pool test suite', () => { |
| 24 | it('Simulate worker not found during increaseWorkersTask', () => { |
| 25 | const pool = new StubPoolWithTasksMapClear( |
| 26 | numberOfWorkers, |
| 27 | './tests/worker-files/thread/testWorker.js' |
| 28 | ) |
| 29 | // Simulate worker not found. |
| 30 | pool.removeAllWorker() |
| 31 | expect(() => pool.increaseWorkersTask()).toThrowError(expectedError) |
| 32 | pool.destroy() |
| 33 | }) |
| 34 | |
| 35 | it('Simulate worker not found during decreaseWorkersTasks', () => { |
| 36 | const pool = new StubPoolWithTasksMapClear( |
| 37 | numberOfWorkers, |
| 38 | './tests/worker-files/thread/testWorker.js', |
| 39 | { |
| 40 | errorHandler: e => console.error(e) |
| 41 | } |
| 42 | ) |
| 43 | // Simulate worker not found. |
| 44 | pool.removeAllWorker() |
| 45 | expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError) |
| 46 | pool.destroy() |
| 47 | }) |
| 48 | |
| 49 | it('Simulate pool creation from a non main thread/process', () => { |
| 50 | expect( |
| 51 | () => |
| 52 | new StubPoolWithIsMainMethod( |
| 53 | numberOfWorkers, |
| 54 | './tests/worker-files/thread/testWorker.js', |
| 55 | { |
| 56 | errorHandler: e => console.error(e) |
| 57 | } |
| 58 | ) |
| 59 | ).toThrowError(new Error('Cannot start a pool from a worker!')) |
| 60 | }) |
| 61 | |
| 62 | it('Verify that filePath is checked', () => { |
| 63 | const expectedError = new Error( |
| 64 | 'Please specify a file with a worker implementation' |
| 65 | ) |
| 66 | expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError( |
| 67 | expectedError |
| 68 | ) |
| 69 | expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError( |
| 70 | expectedError |
| 71 | ) |
| 72 | }) |
| 73 | |
| 74 | it('Verify that numberOfWorkers is checked', () => { |
| 75 | expect(() => new FixedThreadPool()).toThrowError( |
| 76 | new Error( |
| 77 | 'Cannot instantiate a pool without specifying the number of workers' |
| 78 | ) |
| 79 | ) |
| 80 | }) |
| 81 | |
| 82 | it('Verify that a negative number of workers is checked', () => { |
| 83 | expect( |
| 84 | () => |
| 85 | new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js') |
| 86 | ).toThrowError( |
| 87 | new Error('Cannot instantiate a pool with a negative number of workers') |
| 88 | ) |
| 89 | }) |
| 90 | |
| 91 | it('Verify that a non integer number of workers is checked', () => { |
| 92 | expect( |
| 93 | () => |
| 94 | new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') |
| 95 | ).toThrowError( |
| 96 | new Error( |
| 97 | 'Cannot instantiate a pool with a non integer number of workers' |
| 98 | ) |
| 99 | ) |
| 100 | }) |
| 101 | |
| 102 | it('Verify that pool options are checked', () => { |
| 103 | let pool = new FixedThreadPool( |
| 104 | numberOfWorkers, |
| 105 | './tests/worker-files/thread/testWorker.js' |
| 106 | ) |
| 107 | expect(pool.opts.enableEvents).toEqual(true) |
| 108 | expect(pool.emitter).toBeDefined() |
| 109 | expect(pool.opts.workerChoiceStrategy).toBe( |
| 110 | WorkerChoiceStrategies.ROUND_ROBIN |
| 111 | ) |
| 112 | pool.destroy() |
| 113 | pool = new FixedThreadPool( |
| 114 | numberOfWorkers, |
| 115 | './tests/worker-files/thread/testWorker.js', |
| 116 | { |
| 117 | workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED, |
| 118 | enableEvents: false |
| 119 | } |
| 120 | ) |
| 121 | expect(pool.opts.enableEvents).toEqual(false) |
| 122 | expect(pool.emitter).toBeUndefined() |
| 123 | expect(pool.opts.workerChoiceStrategy).toBe( |
| 124 | WorkerChoiceStrategies.LESS_RECENTLY_USED |
| 125 | ) |
| 126 | pool.destroy() |
| 127 | }) |
| 128 | |
| 129 | it("Verify that pool event emitter 'busy' event can register a callback", () => { |
| 130 | const pool = new FixedThreadPool( |
| 131 | numberOfWorkers, |
| 132 | './tests/worker-files/thread/testWorker.js' |
| 133 | ) |
| 134 | const promises = [] |
| 135 | let poolBusy = 0 |
| 136 | pool.emitter.on('busy', () => poolBusy++) |
| 137 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
| 138 | promises.push(pool.execute({ test: 'test' })) |
| 139 | } |
| 140 | expect(poolBusy).toEqual(numberOfWorkers) |
| 141 | pool.destroy() |
| 142 | }) |
| 143 | }) |