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