Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
325f50bc | 2 | const { FixedThreadPool } = require('../../../lib/index') |
6db75ad9 | 3 | const WorkerFunctions = require('../../test-types') |
85a3f8a7 | 4 | const TestUtils = require('../../test-utils') |
506c2a14 | 5 | |
a35560ba | 6 | describe('Fixed thread pool test suite', () => { |
e1ffb94f JB |
7 | const numberOfThreads = 6 |
8 | const pool = new FixedThreadPool( | |
9 | numberOfThreads, | |
10 | './tests/worker-files/thread/testWorker.js', | |
11 | { | |
12 | errorHandler: e => console.error(e) | |
13 | } | |
14 | ) | |
15 | const emptyPool = new FixedThreadPool( | |
16 | numberOfThreads, | |
17 | './tests/worker-files/thread/emptyWorker.js', | |
18 | { exitHandler: () => console.log('empty pool worker exited') } | |
19 | ) | |
20 | const echoPool = new FixedThreadPool( | |
21 | numberOfThreads, | |
22 | './tests/worker-files/thread/echoWorker.js' | |
23 | ) | |
24 | const errorPool = new FixedThreadPool( | |
25 | numberOfThreads, | |
26 | './tests/worker-files/thread/errorWorker.js', | |
27 | { | |
28 | errorHandler: e => console.error(e) | |
29 | } | |
30 | ) | |
31 | const asyncErrorPool = new FixedThreadPool( | |
32 | numberOfThreads, | |
33 | './tests/worker-files/thread/asyncErrorWorker.js', | |
34 | { | |
35 | errorHandler: e => console.error(e) | |
36 | } | |
37 | ) | |
38 | const asyncPool = new FixedThreadPool( | |
39 | numberOfThreads, | |
40 | './tests/worker-files/thread/asyncWorker.js' | |
41 | ) | |
42 | ||
0e2503fc JB |
43 | after('Destroy all pools', async () => { |
44 | // We need to clean up the resources after our test | |
45 | await echoPool.destroy() | |
46 | await asyncPool.destroy() | |
47 | await errorPool.destroy() | |
7c0ba920 | 48 | await asyncErrorPool.destroy() |
0e2503fc JB |
49 | await emptyPool.destroy() |
50 | }) | |
51 | ||
506c2a14 | 52 | it('Choose worker round robin test', async () => { |
53 | const results = new Set() | |
5c5a1fb7 | 54 | for (let i = 0; i < numberOfThreads; i++) { |
fa0f5b28 | 55 | results.add(pool.chooseWorker().threadId) |
506c2a14 | 56 | } |
5c5a1fb7 | 57 | expect(results.size).toBe(numberOfThreads) |
506c2a14 | 58 | }) |
59 | ||
60 | it('Verify that the function is executed in a worker thread', async () => { | |
6db75ad9 JB |
61 | let result = await pool.execute({ |
62 | function: WorkerFunctions.fibonacci | |
63 | }) | |
64 | expect(result).toBe(false) | |
65 | result = await pool.execute({ | |
66 | function: WorkerFunctions.factorial | |
67 | }) | |
68 | expect(result).toBe(false) | |
506c2a14 | 69 | }) |
70 | ||
106744f7 | 71 | it('Verify that is possible to invoke the execute method without input', async () => { |
72 | const result = await pool.execute() | |
6db75ad9 | 73 | expect(result).toBe(false) |
106744f7 | 74 | }) |
75 | ||
7c0ba920 JB |
76 | it('Verify that busy event is emitted', async () => { |
77 | const promises = [] | |
78 | let poolBusy = 0 | |
79 | pool.emitter.on('busy', () => poolBusy++) | |
80 | for (let i = 0; i < numberOfThreads * 2; i++) { | |
6db75ad9 | 81 | promises.push(pool.execute()) |
7c0ba920 | 82 | } |
14916bf9 JB |
83 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. |
84 | // So in total numberOfThreads + 1 times for a loop submitting up to numberOfThreads * 2 tasks to the fixed pool. | |
85 | expect(poolBusy).toBe(numberOfThreads + 1) | |
7c0ba920 JB |
86 | }) |
87 | ||
106744f7 | 88 | it('Verify that is possible to have a worker that return undefined', async () => { |
89 | const result = await emptyPool.execute() | |
6db75ad9 | 90 | expect(result).toBeUndefined() |
106744f7 | 91 | }) |
92 | ||
93 | it('Verify that data are sent to the worker correctly', async () => { | |
94 | const data = { f: 10 } | |
95 | const result = await echoPool.execute(data) | |
e1ffb94f | 96 | expect(result).toStrictEqual(data) |
106744f7 | 97 | }) |
98 | ||
7c0ba920 | 99 | it('Verify that error handling is working properly:sync', async () => { |
106744f7 | 100 | const data = { f: 10 } |
101 | let inError | |
102 | try { | |
103 | await errorPool.execute(data) | |
104 | } catch (e) { | |
105 | inError = e | |
106 | } | |
7c0ba920 JB |
107 | expect(inError).toBeDefined() |
108 | expect(inError).toBeInstanceOf(Error) | |
109 | expect(inError.message).toBeDefined() | |
8620fb25 | 110 | expect(typeof inError.message === 'string').toBe(true) |
0302f8ec | 111 | expect(inError.message).toBe('Error Message from ThreadWorker') |
7c0ba920 JB |
112 | }) |
113 | ||
114 | it('Verify that error handling is working properly:async', async () => { | |
115 | const data = { f: 10 } | |
116 | let inError | |
117 | try { | |
118 | await asyncErrorPool.execute(data) | |
119 | } catch (e) { | |
120 | inError = e | |
121 | } | |
122 | expect(inError).toBeDefined() | |
123 | expect(inError).toBeInstanceOf(Error) | |
124 | expect(inError.message).toBeDefined() | |
8620fb25 | 125 | expect(typeof inError.message === 'string').toBe(true) |
0302f8ec | 126 | expect(inError.message).toBe('Error Message from ThreadWorker:async') |
106744f7 | 127 | }) |
128 | ||
7784f548 | 129 | it('Verify that async function is working properly', async () => { |
130 | const data = { f: 10 } | |
131 | const startTime = new Date().getTime() | |
132 | const result = await asyncPool.execute(data) | |
133 | const usedTime = new Date().getTime() - startTime | |
e1ffb94f | 134 | expect(result).toStrictEqual(data) |
32d490eb | 135 | expect(usedTime).toBeGreaterThanOrEqual(2000) |
7784f548 | 136 | }) |
137 | ||
506c2a14 | 138 | it('Shutdown test', async () => { |
85a3f8a7 | 139 | const exitPromise = TestUtils.waitExits(pool, numberOfThreads) |
1f9a5a44 | 140 | await pool.destroy() |
bdacc2d2 JB |
141 | const numberOfExitEvents = await exitPromise |
142 | expect(numberOfExitEvents).toBe(numberOfThreads) | |
506c2a14 | 143 | }) |
144 | ||
145 | it('Should work even without opts in input', async () => { | |
76b1e974 | 146 | const pool1 = new FixedThreadPool( |
e1ffb94f | 147 | numberOfThreads, |
76b1e974 S |
148 | './tests/worker-files/thread/testWorker.js' |
149 | ) | |
6db75ad9 JB |
150 | const res = await pool1.execute() |
151 | expect(res).toBe(false) | |
0e2503fc JB |
152 | // We need to clean up the resources after our test |
153 | await pool1.destroy() | |
506c2a14 | 154 | }) |
8d3782fa JB |
155 | |
156 | it('Verify that a pool with zero worker fails', async () => { | |
157 | expect( | |
158 | () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js') | |
159 | ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker')) | |
160 | }) | |
506c2a14 | 161 | }) |