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