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