Commit | Line | Data |
---|---|---|
506c2a14 | 1 | const expect = require('expect') |
325f50bc | 2 | const { DynamicThreadPool } = require('../../../lib/index') |
85a3f8a7 | 3 | const TestUtils = require('../../test-utils') |
506c2a14 | 4 | const min = 1 |
c719859c | 5 | const max = 3 |
325f50bc S |
6 | const pool = new DynamicThreadPool( |
7 | min, | |
8 | max, | |
76b1e974 | 9 | './tests/worker-files/thread/testWorker.js', |
325f50bc | 10 | { |
e5177d86 | 11 | errorHandler: e => console.error(e) |
325f50bc S |
12 | } |
13 | ) | |
506c2a14 | 14 | |
a35560ba | 15 | describe('Dynamic thread pool test suite', () => { |
506c2a14 | 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 = [] | |
7c0ba920 JB |
24 | let poolBusy = 0 |
25 | pool.emitter.on('busy', () => poolBusy++) | |
cf9aa6c3 | 26 | for (let i = 0; i < max * 2; i++) { |
506c2a14 | 27 | promises.push(pool.execute({ test: 'test' })) |
28 | } | |
29 | expect(pool.workers.length).toBe(max) | |
14916bf9 JB |
30 | // The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool. |
31 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool. | |
8620fb25 | 32 | expect(poolBusy).toBe(max + 1) |
85a3f8a7 APA |
33 | const res = await TestUtils.waitExits(pool, max - min) |
34 | expect(res).toBe(max - min) | |
506c2a14 | 35 | }) |
36 | ||
bcf04003 | 37 | it('Verify scale thread up and down is working', async () => { |
38 | expect(pool.workers.length).toBe(min) | |
39 | for (let i = 0; i < max * 10; i++) { | |
40 | pool.execute({ test: 'test' }) | |
41 | } | |
42 | expect(pool.workers.length).toBe(max) | |
85a3f8a7 | 43 | await TestUtils.waitExits(pool, max - min) |
bcf04003 | 44 | expect(pool.workers.length).toBe(min) |
45 | for (let i = 0; i < max * 10; i++) { | |
46 | pool.execute({ test: 'test' }) | |
47 | } | |
48 | expect(pool.workers.length).toBe(max) | |
85a3f8a7 | 49 | await TestUtils.waitExits(pool, max - min) |
bcf04003 | 50 | expect(pool.workers.length).toBe(min) |
51 | }) | |
c01733f1 | 52 | |
506c2a14 | 53 | it('Shutdown test', async () => { |
54 | let closedThreads = 0 | |
55 | pool.workers.forEach(w => { | |
56 | w.on('exit', () => { | |
57 | closedThreads++ | |
58 | }) | |
59 | }) | |
1f9a5a44 | 60 | await pool.destroy() |
506c2a14 | 61 | expect(closedThreads).toBe(min) |
62 | }) | |
63 | ||
8d3782fa JB |
64 | it('Validation of inputs test', () => { |
65 | expect(() => new DynamicThreadPool(min)).toThrowError( | |
85a3f8a7 APA |
66 | new Error('Please specify a file with a worker implementation') |
67 | ) | |
506c2a14 | 68 | }) |
69 | ||
70 | it('Should work even without opts in input', async () => { | |
325f50bc S |
71 | const pool1 = new DynamicThreadPool( |
72 | 1, | |
73 | 1, | |
76b1e974 | 74 | './tests/worker-files/thread/testWorker.js' |
325f50bc | 75 | ) |
506c2a14 | 76 | const res = await pool1.execute({ test: 'test' }) |
8620fb25 | 77 | expect(res).toBeDefined() |
506c2a14 | 78 | expect(res).toBeFalsy() |
0e2503fc JB |
79 | // We need to clean up the resources after our test |
80 | await pool1.destroy() | |
506c2a14 | 81 | }) |
c01733f1 | 82 | |
4c35177b | 83 | it('Verify scale thread up and down is working when long running task is used:hard', async () => { |
c01733f1 | 84 | const longRunningPool = new DynamicThreadPool( |
85 | min, | |
86 | max, | |
85a3f8a7 | 87 | './tests/worker-files/thread/longRunningWorkerHardBehavior.js', |
4c35177b | 88 | { |
89 | errorHandler: e => console.error(e), | |
292ad316 JB |
90 | onlineHandler: () => console.log('long running worker is online'), |
91 | exitHandler: () => console.log('long running worker exited') | |
4c35177b | 92 | } |
93 | ) | |
94 | expect(longRunningPool.workers.length).toBe(min) | |
95 | for (let i = 0; i < max * 10; i++) { | |
96 | longRunningPool.execute({ test: 'test' }) | |
97 | } | |
98 | expect(longRunningPool.workers.length).toBe(max) | |
85a3f8a7 | 99 | await TestUtils.waitExits(longRunningPool, max - min) |
4c35177b | 100 | expect(longRunningPool.workers.length).toBe(min) |
0e2503fc JB |
101 | // We need to clean up the resources after our test |
102 | await longRunningPool.destroy() | |
4c35177b | 103 | }) |
104 | ||
105 | it('Verify scale thread up and down is working when long running task is used:soft', async () => { | |
106 | const longRunningPool = new DynamicThreadPool( | |
107 | min, | |
108 | max, | |
85a3f8a7 | 109 | './tests/worker-files/thread/longRunningWorkerSoftBehavior.js', |
c01733f1 | 110 | { |
111 | errorHandler: e => console.error(e), | |
292ad316 JB |
112 | onlineHandler: () => console.log('long running worker is online'), |
113 | exitHandler: () => console.log('long running worker exited') | |
c01733f1 | 114 | } |
115 | ) | |
116 | expect(longRunningPool.workers.length).toBe(min) | |
117 | for (let i = 0; i < max * 10; i++) { | |
118 | longRunningPool.execute({ test: 'test' }) | |
119 | } | |
120 | expect(longRunningPool.workers.length).toBe(max) | |
85a3f8a7 | 121 | await TestUtils.sleep(1500) |
c01733f1 | 122 | // Here we expect the workers to be at the max size since that the task is still running |
123 | expect(longRunningPool.workers.length).toBe(max) | |
0e2503fc JB |
124 | // We need to clean up the resources after our test |
125 | await longRunningPool.destroy() | |
c01733f1 | 126 | }) |
8d3782fa JB |
127 | |
128 | it('Verify that a pool with zero worker can be instantiated', async () => { | |
129 | const pool = new DynamicThreadPool( | |
130 | 0, | |
131 | max, | |
132 | './tests/worker-files/thread/testWorker.js' | |
133 | ) | |
134 | expect(pool).toBeInstanceOf(DynamicThreadPool) | |
135 | // We need to clean up the resources after our test | |
136 | await pool.destroy() | |
137 | }) | |
506c2a14 | 138 | }) |