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