Commit | Line | Data |
---|---|---|
a35560ba S |
1 | const expect = require('expect') |
2 | const { | |
3 | WorkerChoiceStrategies, | |
4 | DynamicThreadPool, | |
5 | FixedThreadPool | |
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 | ||
b98ec2e6 | 45 | it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => { |
a35560ba S |
46 | const max = 3 |
47 | const pool = new FixedThreadPool( | |
48 | max, | |
49 | './tests/worker-files/thread/testWorker.js', | |
50 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } | |
51 | ) | |
b98ec2e6 JB |
52 | expect(pool.opts.workerChoiceStrategy).toBe( |
53 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
54 | ) | |
55 | // We need to clean up the resources after our test | |
56 | await pool.destroy() | |
57 | }) | |
a35560ba | 58 | |
b98ec2e6 JB |
59 | it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => { |
60 | const max = 3 | |
61 | const pool = new FixedThreadPool( | |
62 | max, | |
63 | './tests/worker-files/thread/testWorker.js' | |
64 | ) | |
65 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) | |
a35560ba S |
66 | expect(pool.opts.workerChoiceStrategy).toBe( |
67 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
68 | ) | |
b98ec2e6 JB |
69 | // We need to clean up the resources after our test |
70 | await pool.destroy() | |
71 | }) | |
a35560ba | 72 | |
ff5e76e1 | 73 | it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => { |
b98ec2e6 JB |
74 | const max = 3 |
75 | const pool = new FixedThreadPool( | |
76 | max, | |
77 | './tests/worker-files/thread/testWorker.js', | |
78 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } | |
79 | ) | |
a35560ba S |
80 | // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` |
81 | const promises = [] | |
82 | for (let i = 0; i < max * 2; i++) { | |
83 | promises.push(pool.execute({ test: 'test' })) | |
84 | } | |
85 | await Promise.all(promises) | |
a35560ba S |
86 | // We need to clean up the resources after our test |
87 | await pool.destroy() | |
88 | }) | |
89 | ||
ff5e76e1 JB |
90 | it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => { |
91 | const min = 0 | |
92 | const max = 3 | |
93 | const pool = new DynamicThreadPool( | |
94 | min, | |
95 | max, | |
96 | './tests/worker-files/thread/testWorker.js', | |
97 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } | |
98 | ) | |
99 | // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` | |
100 | const promises = [] | |
101 | for (let i = 0; i < max * 2; i++) { | |
102 | promises.push(pool.execute({ test: 'test' })) | |
103 | } | |
104 | await Promise.all(promises) | |
ff5e76e1 JB |
105 | // We need to clean up the resources after our test |
106 | await pool.destroy() | |
107 | }) | |
108 | ||
a35560ba S |
109 | it('Verify unknown strategies throw error', () => { |
110 | const min = 1 | |
111 | const max = 3 | |
112 | expect( | |
113 | () => | |
114 | new DynamicThreadPool( | |
115 | min, | |
116 | max, | |
117 | './tests/worker-files/thread/testWorker.js', | |
1927ee67 | 118 | { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } |
a35560ba S |
119 | ) |
120 | ).toThrowError( | |
121 | new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") | |
122 | ) | |
123 | }) | |
124 | }) |