| 1 | const expect = require('expect') |
| 2 | const { DynamicThreadPool } = require('../../../lib/index') |
| 3 | const min = 1 |
| 4 | const max = 3 |
| 5 | const pool = new DynamicThreadPool( |
| 6 | min, |
| 7 | max, |
| 8 | './tests/worker-files/thread/testWorker.js', |
| 9 | { |
| 10 | errorHandler: e => console.error(e), |
| 11 | onlineHandler: () => console.log('worker is online') |
| 12 | } |
| 13 | ) |
| 14 | |
| 15 | describe('Dynamic thread pool test suite ', () => { |
| 16 | it('Verify that the function is executed in a worker thread', async () => { |
| 17 | const result = await pool.execute({ test: 'test' }) |
| 18 | expect(result).toBeDefined() |
| 19 | expect(result).toBeFalsy() |
| 20 | }) |
| 21 | |
| 22 | it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => { |
| 23 | const promises = [] |
| 24 | let closedThreads = 0 |
| 25 | let fullPool = 0 |
| 26 | pool.emitter.on('FullPool', () => fullPool++) |
| 27 | for (let i = 0; i < max * 2; i++) { |
| 28 | promises.push(pool.execute({ test: 'test' })) |
| 29 | } |
| 30 | expect(pool.workers.length).toBe(max) |
| 31 | pool.workers.forEach(w => { |
| 32 | w.on('exit', () => { |
| 33 | closedThreads++ |
| 34 | }) |
| 35 | }) |
| 36 | expect(fullPool > 1).toBeTruthy() |
| 37 | await new Promise(resolve => setTimeout(resolve, 2000)) |
| 38 | expect(closedThreads).toBe(max - min) |
| 39 | }) |
| 40 | |
| 41 | it('Verify scale thread up and down is working', async () => { |
| 42 | expect(pool.workers.length).toBe(min) |
| 43 | for (let i = 0; i < max * 10; i++) { |
| 44 | pool.execute({ test: 'test' }) |
| 45 | } |
| 46 | expect(pool.workers.length).toBe(max) |
| 47 | await new Promise(resolve => setTimeout(resolve, 1000)) |
| 48 | expect(pool.workers.length).toBe(min) |
| 49 | for (let i = 0; i < max * 10; i++) { |
| 50 | pool.execute({ test: 'test' }) |
| 51 | } |
| 52 | expect(pool.workers.length).toBe(max) |
| 53 | await new Promise(resolve => setTimeout(resolve, 1000)) |
| 54 | expect(pool.workers.length).toBe(min) |
| 55 | }) |
| 56 | it('Shutdown test', async () => { |
| 57 | let closedThreads = 0 |
| 58 | pool.workers.forEach(w => { |
| 59 | w.on('exit', () => { |
| 60 | closedThreads++ |
| 61 | }) |
| 62 | }) |
| 63 | await pool.destroy() |
| 64 | expect(closedThreads).toBe(min) |
| 65 | }) |
| 66 | |
| 67 | it('Validations test', () => { |
| 68 | let error |
| 69 | try { |
| 70 | const pool1 = new DynamicThreadPool() |
| 71 | console.log(pool1) |
| 72 | } catch (e) { |
| 73 | error = e |
| 74 | } |
| 75 | expect(error).toBeTruthy() |
| 76 | expect(error.message).toBeTruthy() |
| 77 | }) |
| 78 | |
| 79 | it('Should work even without opts in input', async () => { |
| 80 | const pool1 = new DynamicThreadPool( |
| 81 | 1, |
| 82 | 1, |
| 83 | './tests/worker-files/thread/testWorker.js' |
| 84 | ) |
| 85 | const res = await pool1.execute({ test: 'test' }) |
| 86 | expect(res).toBeFalsy() |
| 87 | }) |
| 88 | }) |