1 const { expect
} = require('expect')
7 } = require('../../../lib/index')
9 describe('Abstract pool test suite', () => {
10 const numberOfWorkers
= 1
11 const workerNotFoundInTasksUsageMapError
= new Error(
12 'Worker could not be found in workers tasks usage map'
14 class StubPoolWithRemoveAllWorker
extends FixedThreadPool
{
17 this.workersTasksUsage
.clear()
18 this.promiseMap
.clear()
21 class StubPoolWithIsMain
extends FixedThreadPool
{
27 it('Simulate pool creation from a non main thread/process', () => {
30 new StubPoolWithIsMain(
32 './tests/worker-files/thread/testWorker.js',
34 errorHandler
: e
=> console
.error(e
)
37 ).toThrowError(new Error('Cannot start a pool from a worker!'))
40 it('Verify that filePath is checked', () => {
41 const expectedError
= new Error(
42 'Please specify a file with a worker implementation'
44 expect(() => new FixedThreadPool(numberOfWorkers
)).toThrowError(
47 expect(() => new FixedThreadPool(numberOfWorkers
, '')).toThrowError(
52 it('Verify that numberOfWorkers is checked', () => {
53 expect(() => new FixedThreadPool()).toThrowError(
55 'Cannot instantiate a pool without specifying the number of workers'
60 it('Verify that a negative number of workers is checked', () => {
63 new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
65 new Error('Cannot instantiate a pool with a negative number of workers')
69 it('Verify that a non integer number of workers is checked', () => {
72 new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
75 'Cannot instantiate a pool with a non integer number of workers'
80 it('Verify that pool options are checked', async () => {
81 let pool
= new FixedThreadPool(
83 './tests/worker-files/thread/testWorker.js'
85 expect(pool
.opts
.enableEvents
).toBe(true)
86 expect(pool
.emitter
).toBeDefined()
87 expect(pool
.opts
.workerChoiceStrategy
).toBe(
88 WorkerChoiceStrategies
.ROUND_ROBIN
90 expect(pool
.opts
.messageHandler
).toBeUndefined()
91 expect(pool
.opts
.errorHandler
).toBeUndefined()
92 expect(pool
.opts
.onlineHandler
).toBeUndefined()
93 expect(pool
.opts
.exitHandler
).toBeUndefined()
95 const testHandler
= () => console
.log('test handler executed')
96 pool
= new FixedThreadPool(
98 './tests/worker-files/thread/testWorker.js',
100 workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
,
102 messageHandler
: testHandler
,
103 errorHandler
: testHandler
,
104 onlineHandler
: testHandler
,
105 exitHandler
: testHandler
108 expect(pool
.opts
.enableEvents
).toBe(false)
109 expect(pool
.emitter
).toBeUndefined()
110 expect(pool
.opts
.workerChoiceStrategy
).toBe(
111 WorkerChoiceStrategies
.LESS_RECENTLY_USED
113 expect(pool
.opts
.messageHandler
).toStrictEqual(testHandler
)
114 expect(pool
.opts
.errorHandler
).toStrictEqual(testHandler
)
115 expect(pool
.opts
.onlineHandler
).toStrictEqual(testHandler
)
116 expect(pool
.opts
.exitHandler
).toStrictEqual(testHandler
)
120 it('Simulate worker not found during increaseWorkerRunningTasks', async () => {
121 const pool
= new StubPoolWithRemoveAllWorker(
123 './tests/worker-files/cluster/testWorker.js'
125 // Simulate worker not found.
126 pool
.removeAllWorker()
127 expect(() => pool
.increaseWorkerRunningTasks()).toThrowError(
128 workerNotFoundInTasksUsageMapError
133 it('Simulate worker not found during decreaseWorkerRunningTasks', async () => {
134 const pool
= new StubPoolWithRemoveAllWorker(
136 './tests/worker-files/cluster/testWorker.js',
138 errorHandler
: e
=> console
.error(e
)
141 // Simulate worker not found.
142 pool
.removeAllWorker()
143 expect(() => pool
.decreaseWorkerRunningTasks()).toThrowError(
144 workerNotFoundInTasksUsageMapError
149 it('Simulate worker not found during stepWorkerRunTasks', async () => {
150 const pool
= new StubPoolWithRemoveAllWorker(
152 './tests/worker-files/cluster/testWorker.js',
154 errorHandler
: e
=> console
.error(e
)
157 // Simulate worker not found.
158 pool
.removeAllWorker()
159 expect(() => pool
.stepWorkerRunTasks()).toThrowError(
160 workerNotFoundInTasksUsageMapError
165 it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', async () => {
166 const pool
= new StubPoolWithRemoveAllWorker(
168 './tests/worker-files/cluster/testWorker.js',
170 errorHandler
: e
=> console
.error(e
)
173 // Simulate worker not found.
174 pool
.removeAllWorker()
175 expect(() => pool
.updateWorkerTasksRunTime()).not
.toThrowError()
179 it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', async () => {
180 const pool
= new StubPoolWithRemoveAllWorker(
182 './tests/worker-files/cluster/testWorker.js',
184 workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
,
185 errorHandler
: e
=> console
.error(e
)
188 // Simulate worker not found.
189 pool
.removeAllWorker()
190 expect(() => pool
.updateWorkerTasksRunTime()).toThrowError(
191 workerNotFoundInTasksUsageMapError
196 it('Verify that worker pool tasks usage are initialized', async () => {
197 const pool
= new FixedClusterPool(
199 './tests/worker-files/cluster/testWorker.js'
201 for (const tasksUsage
of pool
.workersTasksUsage
.values()) {
202 expect(tasksUsage
).toBeDefined()
203 expect(tasksUsage
.run
).toBe(0)
204 expect(tasksUsage
.running
).toBe(0)
205 expect(tasksUsage
.runTime
).toBe(0)
206 expect(tasksUsage
.avgRunTime
).toBe(0)
211 it('Verify that worker pool tasks usage are computed', async () => {
212 const pool
= new FixedClusterPool(
214 './tests/worker-files/cluster/testWorker.js'
217 for (let i
= 0; i
< numberOfWorkers
* 2; i
++) {
218 promises
.push(pool
.execute())
220 for (const tasksUsage
of pool
.workersTasksUsage
.values()) {
221 expect(tasksUsage
).toBeDefined()
222 expect(tasksUsage
.run
).toBe(0)
223 expect(tasksUsage
.running
).toBe(numberOfWorkers
* 2)
224 expect(tasksUsage
.runTime
).toBe(0)
225 expect(tasksUsage
.avgRunTime
).toBe(0)
227 await Promise
.all(promises
)
228 for (const tasksUsage
of pool
.workersTasksUsage
.values()) {
229 expect(tasksUsage
).toBeDefined()
230 expect(tasksUsage
.run
).toBe(numberOfWorkers
* 2)
231 expect(tasksUsage
.running
).toBe(0)
232 expect(tasksUsage
.runTime
).toBeGreaterThanOrEqual(0)
233 expect(tasksUsage
.avgRunTime
).toBeGreaterThanOrEqual(0)
238 it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => {
239 const pool
= new DynamicThreadPool(
242 './tests/worker-files/thread/testWorker.js'
245 for (let i
= 0; i
< numberOfWorkers
* 2; i
++) {
246 promises
.push(pool
.execute())
248 await Promise
.all(promises
)
249 for (const tasksUsage
of pool
.workersTasksUsage
.values()) {
250 expect(tasksUsage
).toBeDefined()
251 expect(tasksUsage
.run
).toBe(numberOfWorkers
* 2)
252 expect(tasksUsage
.running
).toBe(0)
253 expect(tasksUsage
.runTime
).toBeGreaterThanOrEqual(0)
254 expect(tasksUsage
.avgRunTime
).toBeGreaterThanOrEqual(0)
256 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
257 for (const tasksUsage
of pool
.workersTasksUsage
.values()) {
258 expect(tasksUsage
).toBeDefined()
259 expect(tasksUsage
.run
).toBe(0)
260 expect(tasksUsage
.running
).toBe(0)
261 expect(tasksUsage
.runTime
).toBe(0)
262 expect(tasksUsage
.avgRunTime
).toBe(0)
267 it("Verify that pool event emitter 'busy' event can register a callback", async () => {
268 const pool
= new FixedThreadPool(
270 './tests/worker-files/thread/testWorker.js'
274 pool
.emitter
.on('busy', () => poolBusy
++)
275 for (let i
= 0; i
< numberOfWorkers
* 2; i
++) {
276 promises
.push(pool
.execute())
278 await Promise
.all(promises
)
279 // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
280 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
281 expect(poolBusy
).toBe(numberOfWorkers
+ 1)