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