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