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