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