Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
a35560ba S |
2 | const { |
3 | WorkerChoiceStrategies, | |
4 | DynamicThreadPool, | |
5 | FixedThreadPool | |
15d56315 | 6 | } = require('../../../lib/index') |
a35560ba S |
7 | |
8 | describe('Selection strategies test suite', () => { | |
9 | it('Verify that WorkerChoiceStrategies enumeration provides string values', () => { | |
10 | expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN') | |
11 | expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED') | |
12 | }) | |
13 | ||
e843b904 JB |
14 | it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => { |
15 | const min = 0 | |
16 | const max = 3 | |
17 | const pool = new DynamicThreadPool( | |
18 | min, | |
19 | max, | |
20 | './tests/worker-files/thread/testWorker.js' | |
21 | ) | |
22 | expect(pool.opts.workerChoiceStrategy).toBe( | |
23 | WorkerChoiceStrategies.ROUND_ROBIN | |
24 | ) | |
25 | // We need to clean up the resources after our test | |
26 | await pool.destroy() | |
27 | }) | |
28 | ||
29 | it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => { | |
30 | const min = 0 | |
31 | const max = 3 | |
32 | const pool = new DynamicThreadPool( | |
33 | min, | |
34 | max, | |
35 | './tests/worker-files/thread/testWorker.js' | |
36 | ) | |
37 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) | |
38 | expect(pool.opts.workerChoiceStrategy).toBe( | |
39 | WorkerChoiceStrategies.ROUND_ROBIN | |
40 | ) | |
41 | // We need to clean up the resources after our test | |
42 | await pool.destroy() | |
43 | }) | |
44 | ||
bdaf31cd JB |
45 | it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => { |
46 | const max = 3 | |
47 | const pool = new FixedThreadPool( | |
48 | max, | |
49 | './tests/worker-files/thread/testWorker.js', | |
50 | { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } | |
51 | ) | |
52 | expect(pool.opts.workerChoiceStrategy).toBe( | |
53 | WorkerChoiceStrategies.ROUND_ROBIN | |
54 | ) | |
55 | // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` | |
56 | const promises = [] | |
57 | for (let i = 0; i < max * 2; i++) { | |
58 | promises.push(pool.execute({ test: 'test' })) | |
59 | } | |
60 | await Promise.all(promises) | |
61 | // We need to clean up the resources after our test | |
62 | await pool.destroy() | |
63 | }) | |
64 | ||
65 | it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => { | |
66 | const min = 0 | |
67 | const max = 3 | |
68 | const pool = new DynamicThreadPool( | |
69 | min, | |
70 | max, | |
71 | './tests/worker-files/thread/testWorker.js', | |
72 | { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } | |
73 | ) | |
74 | expect(pool.opts.workerChoiceStrategy).toBe( | |
75 | WorkerChoiceStrategies.ROUND_ROBIN | |
76 | ) | |
77 | // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` | |
78 | const promises = [] | |
79 | for (let i = 0; i < max * 2; i++) { | |
80 | promises.push(pool.execute({ test: 'test' })) | |
81 | } | |
82 | await Promise.all(promises) | |
83 | // We need to clean up the resources after our test | |
84 | await pool.destroy() | |
85 | }) | |
86 | ||
b98ec2e6 | 87 | it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => { |
a35560ba S |
88 | const max = 3 |
89 | const pool = new FixedThreadPool( | |
90 | max, | |
91 | './tests/worker-files/thread/testWorker.js', | |
92 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } | |
93 | ) | |
b98ec2e6 JB |
94 | expect(pool.opts.workerChoiceStrategy).toBe( |
95 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
96 | ) | |
97 | // We need to clean up the resources after our test | |
98 | await pool.destroy() | |
99 | }) | |
a35560ba | 100 | |
b98ec2e6 JB |
101 | it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => { |
102 | const max = 3 | |
103 | const pool = new FixedThreadPool( | |
104 | max, | |
105 | './tests/worker-files/thread/testWorker.js' | |
106 | ) | |
107 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) | |
a35560ba S |
108 | expect(pool.opts.workerChoiceStrategy).toBe( |
109 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
110 | ) | |
b98ec2e6 JB |
111 | // We need to clean up the resources after our test |
112 | await pool.destroy() | |
113 | }) | |
a35560ba | 114 | |
ff5e76e1 | 115 | it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => { |
b98ec2e6 JB |
116 | const max = 3 |
117 | const pool = new FixedThreadPool( | |
118 | max, | |
119 | './tests/worker-files/thread/testWorker.js', | |
120 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } | |
121 | ) | |
a35560ba S |
122 | // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` |
123 | const promises = [] | |
124 | for (let i = 0; i < max * 2; i++) { | |
125 | promises.push(pool.execute({ test: 'test' })) | |
126 | } | |
127 | await Promise.all(promises) | |
a35560ba S |
128 | // We need to clean up the resources after our test |
129 | await pool.destroy() | |
130 | }) | |
131 | ||
ff5e76e1 JB |
132 | it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => { |
133 | const min = 0 | |
134 | const max = 3 | |
135 | const pool = new DynamicThreadPool( | |
136 | min, | |
137 | max, | |
138 | './tests/worker-files/thread/testWorker.js', | |
139 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } | |
140 | ) | |
141 | // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` | |
142 | const promises = [] | |
143 | for (let i = 0; i < max * 2; i++) { | |
144 | promises.push(pool.execute({ test: 'test' })) | |
145 | } | |
146 | await Promise.all(promises) | |
ff5e76e1 JB |
147 | // We need to clean up the resources after our test |
148 | await pool.destroy() | |
149 | }) | |
150 | ||
a35560ba S |
151 | it('Verify unknown strategies throw error', () => { |
152 | const min = 1 | |
153 | const max = 3 | |
154 | expect( | |
155 | () => | |
156 | new DynamicThreadPool( | |
157 | min, | |
158 | max, | |
159 | './tests/worker-files/thread/testWorker.js', | |
1927ee67 | 160 | { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } |
a35560ba S |
161 | ) |
162 | ).toThrowError( | |
163 | new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") | |
164 | ) | |
165 | }) | |
166 | }) |