1 const { expect
} = require('expect')
6 } = require('../../../lib/index')
7 const numberOfWorkers
= 1
8 const workerNotFoundInTasksUsageMapError
= new Error(
9 'Worker could not be found in worker tasks usage map'
11 class StubPoolWithWorkerTasksUsageMapClear
extends FixedThreadPool
{
13 this.workersTasksUsage
.clear()
16 class StubPoolWithIsMainMethod
extends FixedThreadPool
{
22 describe('Abstract pool test suite', () => {
23 it('Simulate pool creation from a non main thread/process', () => {
26 new StubPoolWithIsMainMethod(
28 './tests/worker-files/thread/testWorker.js',
30 errorHandler
: e
=> console
.error(e
)
33 ).toThrowError(new Error('Cannot start a pool from a worker!'))
36 it('Verify that filePath is checked', () => {
37 const expectedError
= new Error(
38 'Please specify a file with a worker implementation'
40 expect(() => new FixedThreadPool(numberOfWorkers
)).toThrowError(
43 expect(() => new FixedThreadPool(numberOfWorkers
, '')).toThrowError(
48 it('Verify that numberOfWorkers is checked', () => {
49 expect(() => new FixedThreadPool()).toThrowError(
51 'Cannot instantiate a pool without specifying the number of workers'
56 it('Verify that a negative number of workers is checked', () => {
59 new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
61 new Error('Cannot instantiate a pool with a negative number of workers')
65 it('Verify that a non integer number of workers is checked', () => {
68 new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
71 'Cannot instantiate a pool with a non integer number of workers'
76 it('Verify that pool options are checked', () => {
77 let pool
= new FixedThreadPool(
79 './tests/worker-files/thread/testWorker.js'
81 expect(pool
.opts
.enableEvents
).toBe(true)
82 expect(pool
.emitter
).toBeDefined()
83 expect(pool
.opts
.workerChoiceStrategy
).toBe(
84 WorkerChoiceStrategies
.ROUND_ROBIN
86 expect(pool
.opts
.messageHandler
).toBeUndefined()
87 expect(pool
.opts
.errorHandler
).toBeUndefined()
88 expect(pool
.opts
.onlineHandler
).toBeUndefined()
89 expect(pool
.opts
.exitHandler
).toBeUndefined()
91 const testHandler
= () => console
.log('test handler executed')
92 pool
= new FixedThreadPool(
94 './tests/worker-files/thread/testWorker.js',
96 workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
,
98 messageHandler
: testHandler
,
99 errorHandler
: testHandler
,
100 onlineHandler
: testHandler
,
101 exitHandler
: testHandler
104 expect(pool
.opts
.enableEvents
).toBe(false)
105 expect(pool
.emitter
).toBeUndefined()
106 expect(pool
.opts
.workerChoiceStrategy
).toBe(
107 WorkerChoiceStrategies
.LESS_RECENTLY_USED
109 expect(pool
.opts
.messageHandler
).toStrictEqual(testHandler
)
110 expect(pool
.opts
.errorHandler
).toStrictEqual(testHandler
)
111 expect(pool
.opts
.onlineHandler
).toStrictEqual(testHandler
)
112 expect(pool
.opts
.exitHandler
).toStrictEqual(testHandler
)
116 it('Simulate worker not found during increaseWorkerRunningTasks', () => {
117 const pool
= new StubPoolWithWorkerTasksUsageMapClear(
119 './tests/worker-files/cluster/testWorker.js'
121 // Simulate worker not found.
122 pool
.removeAllWorker()
123 expect(() => pool
.increaseWorkerRunningTasks()).toThrowError(
124 workerNotFoundInTasksUsageMapError
129 it('Simulate worker not found during decreaseWorkerRunningTasks', () => {
130 const pool
= new StubPoolWithWorkerTasksUsageMapClear(
132 './tests/worker-files/cluster/testWorker.js',
134 errorHandler
: e
=> console
.error(e
)
137 // Simulate worker not found.
138 pool
.removeAllWorker()
139 expect(() => pool
.decreaseWorkerRunningTasks()).toThrowError(
140 workerNotFoundInTasksUsageMapError
145 it('Simulate worker not found during stepWorkerRunTasks', () => {
146 const pool
= new StubPoolWithWorkerTasksUsageMapClear(
148 './tests/worker-files/cluster/testWorker.js',
150 errorHandler
: e
=> console
.error(e
)
153 // Simulate worker not found.
154 pool
.removeAllWorker()
155 expect(() => pool
.stepWorkerRunTasks()).toThrowError(
156 workerNotFoundInTasksUsageMapError
161 it('Simulate worker not found during updateWorkerTasksRunTime', () => {
162 const pool
= new StubPoolWithWorkerTasksUsageMapClear(
164 './tests/worker-files/cluster/testWorker.js',
166 errorHandler
: e
=> console
.error(e
)
169 // Simulate worker not found.
170 pool
.removeAllWorker()
171 expect(() => pool
.updateWorkerTasksRunTime()).toThrowError(
172 workerNotFoundInTasksUsageMapError
177 it('Verify that worker pool tasks usage are initialized', () => {
178 const pool
= new FixedClusterPool(
180 './tests/worker-files/cluster/testWorker.js'
182 for (const tasksUsage
of pool
.workersTasksUsage
.values()) {
183 expect(tasksUsage
).toBeDefined()
184 expect(tasksUsage
.run
).toBe(0)
185 expect(tasksUsage
.running
).toBe(0)
186 expect(tasksUsage
.runTime
).toBe(0)
187 expect(tasksUsage
.avgRunTime
).toBe(0)
192 it('Verify that worker pool tasks usage are computed', async () => {
193 const pool
= new FixedClusterPool(
195 './tests/worker-files/cluster/testWorker.js'
198 for (let i
= 0; i
< numberOfWorkers
* 2; i
++) {
199 promises
.push(pool
.execute({ test
: 'test' }))
201 for (const tasksUsage
of pool
.workersTasksUsage
.values()) {
202 expect(tasksUsage
).toBeDefined()
203 expect(tasksUsage
.run
).toBe(0)
204 expect(tasksUsage
.running
).toBe(numberOfWorkers
* 2)
205 expect(tasksUsage
.runTime
).toBe(0)
206 expect(tasksUsage
.avgRunTime
).toBe(0)
208 await Promise
.all(promises
)
209 for (const tasksUsage
of pool
.workersTasksUsage
.values()) {
210 expect(tasksUsage
).toBeDefined()
211 expect(tasksUsage
.run
).toBe(numberOfWorkers
* 2)
212 expect(tasksUsage
.running
).toBe(0)
213 expect(tasksUsage
.runTime
).toBeGreaterThanOrEqual(0)
214 expect(tasksUsage
.avgRunTime
).toBeGreaterThanOrEqual(0)
219 it("Verify that pool event emitter 'busy' event can register a callback", async () => {
220 const pool
= new FixedThreadPool(
222 './tests/worker-files/thread/testWorker.js'
226 pool
.emitter
.on('busy', () => poolBusy
++)
227 for (let i
= 0; i
< numberOfWorkers
* 2; i
++) {
228 promises
.push(pool
.execute({ test
: 'test' }))
230 await Promise
.all(promises
)
231 // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
232 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
233 expect(poolBusy
).toBe(numberOfWorkers
+ 1)