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