| 1 | const expect = require('expect') |
| 2 | const { FixedThreadPool } = require('../../../lib/index') |
| 3 | const numberOfThreads = 10 |
| 4 | const maxTasks = 400 |
| 5 | const pool = new FixedThreadPool( |
| 6 | numberOfThreads, |
| 7 | './tests/worker-files/thread/testWorker.js', |
| 8 | { |
| 9 | errorHandler: e => console.error(e) |
| 10 | } |
| 11 | ) |
| 12 | const emptyPool = new FixedThreadPool( |
| 13 | 1, |
| 14 | './tests/worker-files/thread/emptyWorker.js' |
| 15 | ) |
| 16 | const echoPool = new FixedThreadPool( |
| 17 | 1, |
| 18 | './tests/worker-files/thread/echoWorker.js' |
| 19 | ) |
| 20 | const errorPool = new FixedThreadPool( |
| 21 | 1, |
| 22 | './tests/worker-files/thread/errorWorker.js', |
| 23 | { |
| 24 | errorHandler: e => console.error(e), |
| 25 | onlineHandler: () => console.log('worker is online') |
| 26 | } |
| 27 | ) |
| 28 | const asyncPool = new FixedThreadPool( |
| 29 | 1, |
| 30 | './tests/worker-files/thread/asyncWorker.js', |
| 31 | { maxTasks: maxTasks } |
| 32 | ) |
| 33 | |
| 34 | describe('Fixed thread pool test suite ', () => { |
| 35 | it('Choose worker round robin test', async () => { |
| 36 | const results = new Set() |
| 37 | for (let i = 0; i < numberOfThreads; i++) { |
| 38 | results.add(pool.chooseWorker().threadId) |
| 39 | } |
| 40 | expect(results.size).toBe(numberOfThreads) |
| 41 | }) |
| 42 | |
| 43 | it('Verify that the function is executed in a worker thread', async () => { |
| 44 | const result = await pool.execute({ test: 'test' }) |
| 45 | expect(result).toBeDefined() |
| 46 | expect(result).toBeFalsy() |
| 47 | }) |
| 48 | |
| 49 | it('Verify that is possible to invoke the execute method without input', async () => { |
| 50 | const result = await pool.execute() |
| 51 | expect(result).toBeDefined() |
| 52 | expect(result).toBeFalsy() |
| 53 | }) |
| 54 | |
| 55 | it('Verify that is possible to have a worker that return undefined', async () => { |
| 56 | const result = await emptyPool.execute() |
| 57 | expect(result).toBeFalsy() |
| 58 | }) |
| 59 | |
| 60 | it('Verify that data are sent to the worker correctly', async () => { |
| 61 | const data = { f: 10 } |
| 62 | const result = await echoPool.execute(data) |
| 63 | expect(result).toBeTruthy() |
| 64 | expect(result.f).toBe(data.f) |
| 65 | }) |
| 66 | |
| 67 | it('Verify that error handling is working properly', async () => { |
| 68 | const data = { f: 10 } |
| 69 | let inError |
| 70 | try { |
| 71 | await errorPool.execute(data) |
| 72 | } catch (e) { |
| 73 | inError = e |
| 74 | } |
| 75 | expect(inError).toBeTruthy() |
| 76 | expect(inError instanceof Error).toBeTruthy() |
| 77 | expect(inError.message).toBeTruthy() |
| 78 | }) |
| 79 | |
| 80 | it('Verify that async function is working properly', async () => { |
| 81 | const data = { f: 10 } |
| 82 | const startTime = new Date().getTime() |
| 83 | const result = await asyncPool.execute(data) |
| 84 | const usedTime = new Date().getTime() - startTime |
| 85 | expect(result).toBeTruthy() |
| 86 | expect(result.f).toBe(data.f) |
| 87 | expect(usedTime).toBeGreaterThanOrEqual(2000) |
| 88 | }) |
| 89 | |
| 90 | it('Verify that maxTasks is set properly', async () => { |
| 91 | const worker = asyncPool.chooseWorker() |
| 92 | expect(worker.port2.getMaxListeners()).toBe(maxTasks) |
| 93 | }) |
| 94 | |
| 95 | it('Shutdown test', async () => { |
| 96 | let closedThreads = 0 |
| 97 | pool.workers.forEach(w => { |
| 98 | w.on('exit', () => { |
| 99 | closedThreads++ |
| 100 | }) |
| 101 | }) |
| 102 | await pool.destroy() |
| 103 | expect(closedThreads).toBe(numberOfThreads) |
| 104 | }) |
| 105 | |
| 106 | it('Validations test', () => { |
| 107 | let error |
| 108 | try { |
| 109 | const pool1 = new FixedThreadPool() |
| 110 | console.log(pool1) |
| 111 | } catch (e) { |
| 112 | error = e |
| 113 | } |
| 114 | expect(error).toBeTruthy() |
| 115 | expect(error.message).toBeTruthy() |
| 116 | }) |
| 117 | |
| 118 | it('Should work even without opts in input', async () => { |
| 119 | const pool1 = new FixedThreadPool( |
| 120 | 1, |
| 121 | './tests/worker-files/thread/testWorker.js' |
| 122 | ) |
| 123 | const res = await pool1.execute({ test: 'test' }) |
| 124 | expect(res).toBeFalsy() |
| 125 | }) |
| 126 | }) |