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