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