Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
e843b904 JB |
2 | const { |
3 | FixedClusterPool, | |
4 | FixedThreadPool, | |
5 | WorkerChoiceStrategies | |
6 | } = require('../../../lib/index') | |
3ec964d6 | 7 | const expectedError = new Error('Worker could not be found in tasks map') |
8 | ||
7c0ba920 JB |
9 | const numberOfWorkers = 1 |
10 | ||
3ec964d6 | 11 | class StubPoolWithTasksMapClear extends FixedThreadPool { |
12 | removeAllWorker () { | |
13 | this.tasks.clear() | |
14 | } | |
15 | } | |
16 | ||
17 | class StubPoolWithIsMainMethod extends FixedThreadPool { | |
18 | isMain () { | |
19 | return false | |
20 | } | |
21 | } | |
22 | ||
a35560ba | 23 | describe('Abstract pool test suite', () => { |
3ec964d6 | 24 | it('Simulate worker not found during increaseWorkersTask', () => { |
25 | const pool = new StubPoolWithTasksMapClear( | |
7c0ba920 | 26 | numberOfWorkers, |
1927ee67 | 27 | './tests/worker-files/thread/testWorker.js' |
3ec964d6 | 28 | ) |
0e2503fc | 29 | // Simulate worker not found. |
3ec964d6 | 30 | pool.removeAllWorker() |
31 | expect(() => pool.increaseWorkersTask()).toThrowError(expectedError) | |
7c0ba920 | 32 | pool.destroy() |
3ec964d6 | 33 | }) |
34 | ||
35 | it('Simulate worker not found during decreaseWorkersTasks', () => { | |
36 | const pool = new StubPoolWithTasksMapClear( | |
7c0ba920 | 37 | numberOfWorkers, |
8d3782fa | 38 | './tests/worker-files/thread/testWorker.js', |
3ec964d6 | 39 | { |
40 | errorHandler: e => console.error(e) | |
41 | } | |
42 | ) | |
0e2503fc | 43 | // Simulate worker not found. |
3ec964d6 | 44 | pool.removeAllWorker() |
45 | expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError) | |
7c0ba920 | 46 | pool.destroy() |
3ec964d6 | 47 | }) |
48 | ||
49 | it('Simulate pool creation from a non main thread/process', () => { | |
8d3782fa JB |
50 | expect( |
51 | () => | |
52 | new StubPoolWithIsMainMethod( | |
7c0ba920 | 53 | numberOfWorkers, |
8d3782fa JB |
54 | './tests/worker-files/thread/testWorker.js', |
55 | { | |
56 | errorHandler: e => console.error(e) | |
57 | } | |
58 | ) | |
59 | ).toThrowError(new Error('Cannot start a pool from a worker!')) | |
3ec964d6 | 60 | }) |
c510fea7 APA |
61 | |
62 | it('Verify that filePath is checked', () => { | |
292ad316 JB |
63 | const expectedError = new Error( |
64 | 'Please specify a file with a worker implementation' | |
65 | ) | |
7c0ba920 | 66 | expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError( |
292ad316 | 67 | expectedError |
8d3782fa | 68 | ) |
7c0ba920 | 69 | expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError( |
292ad316 | 70 | expectedError |
8d3782fa JB |
71 | ) |
72 | }) | |
73 | ||
74 | it('Verify that numberOfWorkers is checked', () => { | |
75 | expect(() => new FixedThreadPool()).toThrowError( | |
76 | new Error( | |
77 | 'Cannot instantiate a pool without specifying the number of workers' | |
78 | ) | |
79 | ) | |
80 | }) | |
81 | ||
82 | it('Verify that a negative number of workers is checked', () => { | |
83 | expect( | |
84 | () => | |
85 | new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js') | |
86 | ).toThrowError( | |
87 | new Error('Cannot instantiate a pool with a negative number of workers') | |
88 | ) | |
89 | }) | |
90 | ||
91 | it('Verify that a non integer number of workers is checked', () => { | |
92 | expect( | |
93 | () => | |
94 | new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') | |
95 | ).toThrowError( | |
96 | new Error( | |
97 | 'Cannot instantiate a pool with a non integer number of workers' | |
98 | ) | |
99 | ) | |
c510fea7 | 100 | }) |
7c0ba920 JB |
101 | |
102 | it('Verify that pool options are checked', () => { | |
103 | let pool = new FixedThreadPool( | |
104 | numberOfWorkers, | |
105 | './tests/worker-files/thread/testWorker.js' | |
106 | ) | |
8620fb25 | 107 | expect(pool.opts.enableEvents).toBe(true) |
7c0ba920 | 108 | expect(pool.emitter).toBeDefined() |
e843b904 JB |
109 | expect(pool.opts.workerChoiceStrategy).toBe( |
110 | WorkerChoiceStrategies.ROUND_ROBIN | |
111 | ) | |
35cf1c03 JB |
112 | expect(pool.opts.messageHandler).toBeUndefined() |
113 | expect(pool.opts.errorHandler).toBeUndefined() | |
114 | expect(pool.opts.onlineHandler).toBeUndefined() | |
115 | expect(pool.opts.exitHandler).toBeUndefined() | |
7c0ba920 | 116 | pool.destroy() |
35cf1c03 | 117 | const testHandler = () => console.log('test handler executed') |
7c0ba920 JB |
118 | pool = new FixedThreadPool( |
119 | numberOfWorkers, | |
120 | './tests/worker-files/thread/testWorker.js', | |
121 | { | |
e843b904 | 122 | workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED, |
35cf1c03 JB |
123 | enableEvents: false, |
124 | messageHandler: testHandler, | |
125 | errorHandler: testHandler, | |
126 | onlineHandler: testHandler, | |
127 | exitHandler: testHandler | |
7c0ba920 JB |
128 | } |
129 | ) | |
8620fb25 | 130 | expect(pool.opts.enableEvents).toBe(false) |
7c0ba920 | 131 | expect(pool.emitter).toBeUndefined() |
e843b904 JB |
132 | expect(pool.opts.workerChoiceStrategy).toBe( |
133 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
134 | ) | |
35cf1c03 JB |
135 | expect(pool.opts.messageHandler).toStrictEqual(testHandler) |
136 | expect(pool.opts.errorHandler).toStrictEqual(testHandler) | |
137 | expect(pool.opts.onlineHandler).toStrictEqual(testHandler) | |
138 | expect(pool.opts.exitHandler).toStrictEqual(testHandler) | |
7c0ba920 JB |
139 | pool.destroy() |
140 | }) | |
141 | ||
cf597bc5 | 142 | it("Verify that pool event emitter 'busy' event can register a callback", async () => { |
7c0ba920 JB |
143 | const pool = new FixedThreadPool( |
144 | numberOfWorkers, | |
145 | './tests/worker-files/thread/testWorker.js' | |
146 | ) | |
147 | const promises = [] | |
148 | let poolBusy = 0 | |
149 | pool.emitter.on('busy', () => poolBusy++) | |
150 | for (let i = 0; i < numberOfWorkers * 2; i++) { | |
151 | promises.push(pool.execute({ test: 'test' })) | |
152 | } | |
cf597bc5 | 153 | await Promise.all(promises) |
14916bf9 JB |
154 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. |
155 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. | |
156 | expect(poolBusy).toBe(numberOfWorkers + 1) | |
7c0ba920 JB |
157 | pool.destroy() |
158 | }) | |
3ec964d6 | 159 | }) |