| 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 | } |
| 12 | ) |
| 13 | |
| 14 | describe('Dynamic thread pool test suite ', () => { |
| 15 | it('Verify that the function is executed in a worker thread', async () => { |
| 16 | const result = await pool.execute({ test: 'test' }) |
| 17 | expect(result).toBeDefined() |
| 18 | expect(result).toBeFalsy() |
| 19 | }) |
| 20 | |
| 21 | it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => { |
| 22 | const promises = [] |
| 23 | let closedThreads = 0 |
| 24 | let fullPool = 0 |
| 25 | pool.emitter.on('FullPool', () => fullPool++) |
| 26 | for (let i = 0; i < max * 2; i++) { |
| 27 | promises.push(pool.execute({ test: 'test' })) |
| 28 | } |
| 29 | expect(pool.workers.length).toBe(max) |
| 30 | pool.workers.forEach(w => { |
| 31 | w.on('exit', () => { |
| 32 | closedThreads++ |
| 33 | }) |
| 34 | }) |
| 35 | expect(fullPool > 1).toBeTruthy() |
| 36 | await new Promise(resolve => setTimeout(resolve, 2000)) |
| 37 | expect(closedThreads).toBe(max - min) |
| 38 | }) |
| 39 | |
| 40 | it('Verify scale thread up and down is working', async () => { |
| 41 | expect(pool.workers.length).toBe(min) |
| 42 | for (let i = 0; i < max * 10; i++) { |
| 43 | pool.execute({ test: 'test' }) |
| 44 | } |
| 45 | expect(pool.workers.length).toBe(max) |
| 46 | await new Promise(resolve => setTimeout(resolve, 1000)) |
| 47 | expect(pool.workers.length).toBe(min) |
| 48 | for (let i = 0; i < max * 10; i++) { |
| 49 | pool.execute({ test: 'test' }) |
| 50 | } |
| 51 | expect(pool.workers.length).toBe(max) |
| 52 | await new Promise(resolve => setTimeout(resolve, 1500)) |
| 53 | expect(pool.workers.length).toBe(min) |
| 54 | }) |
| 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 | |
| 89 | it('Verify scale thread up and down is working when long running task is used:hard', async () => { |
| 90 | const longRunningPool = new DynamicThreadPool( |
| 91 | min, |
| 92 | max, |
| 93 | './tests/worker/thread/longRunningWorkerHardBehavior.js', |
| 94 | { |
| 95 | errorHandler: e => console.error(e), |
| 96 | onlineHandler: () => console.log('worker is online') |
| 97 | } |
| 98 | ) |
| 99 | expect(longRunningPool.workers.length).toBe(min) |
| 100 | for (let i = 0; i < max * 10; i++) { |
| 101 | longRunningPool.execute({ test: 'test' }) |
| 102 | } |
| 103 | expect(longRunningPool.workers.length).toBe(max) |
| 104 | await new Promise(resolve => setTimeout(resolve, 1500)) |
| 105 | expect(longRunningPool.workers.length).toBe(min) |
| 106 | }) |
| 107 | |
| 108 | it('Verify scale thread up and down is working when long running task is used:soft', async () => { |
| 109 | const longRunningPool = new DynamicThreadPool( |
| 110 | min, |
| 111 | max, |
| 112 | './tests/worker/thread/longRunningWorkerSoftBehavior.js', |
| 113 | { |
| 114 | errorHandler: e => console.error(e), |
| 115 | onlineHandler: () => console.log('worker is online') |
| 116 | } |
| 117 | ) |
| 118 | expect(longRunningPool.workers.length).toBe(min) |
| 119 | for (let i = 0; i < max * 10; i++) { |
| 120 | longRunningPool.execute({ test: 'test' }) |
| 121 | } |
| 122 | expect(longRunningPool.workers.length).toBe(max) |
| 123 | await new Promise(resolve => setTimeout(resolve, 1500)) |
| 124 | // Here we expect the workers to be at the max size since that the task is still running |
| 125 | expect(longRunningPool.workers.length).toBe(max) |
| 126 | }) |
| 127 | }) |