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