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