| 1 | const { expect } = require('expect') |
| 2 | const { FixedThreadPool, PoolEvents } = require('../../../lib/index') |
| 3 | const { WorkerFunctions } = require('../../test-types') |
| 4 | const TestUtils = require('../../test-utils') |
| 5 | |
| 6 | describe('Fixed thread pool test suite', () => { |
| 7 | const numberOfThreads = 6 |
| 8 | const pool = new FixedThreadPool( |
| 9 | numberOfThreads, |
| 10 | './tests/worker-files/thread/testWorker.js', |
| 11 | { |
| 12 | errorHandler: e => console.error(e) |
| 13 | } |
| 14 | ) |
| 15 | const emptyPool = new FixedThreadPool( |
| 16 | numberOfThreads, |
| 17 | './tests/worker-files/thread/emptyWorker.js', |
| 18 | { exitHandler: () => console.log('empty pool worker exited') } |
| 19 | ) |
| 20 | const echoPool = new FixedThreadPool( |
| 21 | numberOfThreads, |
| 22 | './tests/worker-files/thread/echoWorker.js' |
| 23 | ) |
| 24 | const errorPool = new FixedThreadPool( |
| 25 | numberOfThreads, |
| 26 | './tests/worker-files/thread/errorWorker.js', |
| 27 | { |
| 28 | errorHandler: e => console.error(e) |
| 29 | } |
| 30 | ) |
| 31 | const asyncErrorPool = new FixedThreadPool( |
| 32 | numberOfThreads, |
| 33 | './tests/worker-files/thread/asyncErrorWorker.js', |
| 34 | { |
| 35 | errorHandler: e => console.error(e) |
| 36 | } |
| 37 | ) |
| 38 | const asyncPool = new FixedThreadPool( |
| 39 | numberOfThreads, |
| 40 | './tests/worker-files/thread/asyncWorker.js' |
| 41 | ) |
| 42 | |
| 43 | after('Destroy all pools', async () => { |
| 44 | // We need to clean up the resources after our test |
| 45 | await echoPool.destroy() |
| 46 | await asyncPool.destroy() |
| 47 | await errorPool.destroy() |
| 48 | await asyncErrorPool.destroy() |
| 49 | await emptyPool.destroy() |
| 50 | }) |
| 51 | |
| 52 | it('Verify that the function is executed in a worker thread', async () => { |
| 53 | let result = await pool.execute({ |
| 54 | function: WorkerFunctions.fibonacci |
| 55 | }) |
| 56 | expect(result).toBe(false) |
| 57 | result = await pool.execute({ |
| 58 | function: WorkerFunctions.factorial |
| 59 | }) |
| 60 | expect(result).toBe(false) |
| 61 | }) |
| 62 | |
| 63 | it('Verify that is possible to invoke the execute method without input', async () => { |
| 64 | const result = await pool.execute() |
| 65 | expect(result).toBe(false) |
| 66 | }) |
| 67 | |
| 68 | it("Verify that 'busy' event is emitted", async () => { |
| 69 | let poolBusy = 0 |
| 70 | pool.emitter.on(PoolEvents.busy, () => ++poolBusy) |
| 71 | for (let i = 0; i < numberOfThreads * 2; i++) { |
| 72 | pool.execute() |
| 73 | } |
| 74 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. |
| 75 | // So in total numberOfThreads + 1 times for a loop submitting up to numberOfThreads * 2 tasks to the fixed pool. |
| 76 | expect(poolBusy).toBe(numberOfThreads + 1) |
| 77 | }) |
| 78 | |
| 79 | it('Verify that is possible to have a worker that return undefined', async () => { |
| 80 | const result = await emptyPool.execute() |
| 81 | expect(result).toBeUndefined() |
| 82 | }) |
| 83 | |
| 84 | it('Verify that data are sent to the worker correctly', async () => { |
| 85 | const data = { f: 10 } |
| 86 | const result = await echoPool.execute(data) |
| 87 | expect(result).toStrictEqual(data) |
| 88 | }) |
| 89 | |
| 90 | it('Verify that error handling is working properly:sync', async () => { |
| 91 | const data = { f: 10 } |
| 92 | let inError |
| 93 | try { |
| 94 | await errorPool.execute(data) |
| 95 | } catch (e) { |
| 96 | inError = e |
| 97 | } |
| 98 | expect(inError).toBeDefined() |
| 99 | expect(inError).toBeInstanceOf(Error) |
| 100 | expect(inError.message).toBeDefined() |
| 101 | expect(typeof inError.message === 'string').toBe(true) |
| 102 | expect(inError.message).toBe('Error Message from ThreadWorker') |
| 103 | }) |
| 104 | |
| 105 | it('Verify that error handling is working properly:async', async () => { |
| 106 | const data = { f: 10 } |
| 107 | let inError |
| 108 | try { |
| 109 | await asyncErrorPool.execute(data) |
| 110 | } catch (e) { |
| 111 | inError = e |
| 112 | } |
| 113 | expect(inError).toBeDefined() |
| 114 | expect(inError).toBeInstanceOf(Error) |
| 115 | expect(inError.message).toBeDefined() |
| 116 | expect(typeof inError.message === 'string').toBe(true) |
| 117 | expect(inError.message).toBe('Error Message from ThreadWorker:async') |
| 118 | }) |
| 119 | |
| 120 | it('Verify that async function is working properly', async () => { |
| 121 | const data = { f: 10 } |
| 122 | const startTime = performance.now() |
| 123 | const result = await asyncPool.execute(data) |
| 124 | const usedTime = performance.now() - startTime |
| 125 | expect(result).toStrictEqual(data) |
| 126 | expect(usedTime).toBeGreaterThanOrEqual(2000) |
| 127 | }) |
| 128 | |
| 129 | it('Shutdown test', async () => { |
| 130 | const exitPromise = TestUtils.waitExits(pool, numberOfThreads) |
| 131 | await pool.destroy() |
| 132 | const numberOfExitEvents = await exitPromise |
| 133 | expect(numberOfExitEvents).toBe(numberOfThreads) |
| 134 | }) |
| 135 | |
| 136 | it('Should work even without opts in input', async () => { |
| 137 | const pool1 = new FixedThreadPool( |
| 138 | numberOfThreads, |
| 139 | './tests/worker-files/thread/testWorker.js' |
| 140 | ) |
| 141 | const res = await pool1.execute() |
| 142 | expect(res).toBe(false) |
| 143 | // We need to clean up the resources after our test |
| 144 | await pool1.destroy() |
| 145 | }) |
| 146 | |
| 147 | it('Verify that a pool with zero worker fails', async () => { |
| 148 | expect( |
| 149 | () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js') |
| 150 | ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker')) |
| 151 | }) |
| 152 | }) |