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