1 const expect
= require('expect')
6 } = require('../../../lib/index')
7 const expectedError
= new Error('Worker could not be found in tasks map')
9 const numberOfWorkers
= 1
11 class StubPoolWithTasksMapClear
extends FixedThreadPool
{
17 class StubPoolWithIsMainMethod
extends FixedThreadPool
{
23 describe('Abstract pool test suite', () => {
24 it('Simulate worker not found during increaseWorkersTask', () => {
25 const pool
= new StubPoolWithTasksMapClear(
27 './tests/worker-files/thread/testWorker.js'
29 // Simulate worker not found.
30 pool
.removeAllWorker()
31 expect(() => pool
.increaseWorkersTask()).toThrowError(expectedError
)
35 it('Simulate worker not found during decreaseWorkersTasks', () => {
36 const pool
= new StubPoolWithTasksMapClear(
38 './tests/worker-files/thread/testWorker.js',
40 errorHandler
: e
=> console
.error(e
)
43 // Simulate worker not found.
44 pool
.removeAllWorker()
45 expect(() => pool
.decreaseWorkersTasks()).toThrowError(expectedError
)
49 it('Simulate pool creation from a non main thread/process', () => {
52 new StubPoolWithIsMainMethod(
54 './tests/worker-files/thread/testWorker.js',
56 errorHandler
: e
=> console
.error(e
)
59 ).toThrowError(new Error('Cannot start a pool from a worker!'))
62 it('Verify that filePath is checked', () => {
63 const expectedError
= new Error(
64 'Please specify a file with a worker implementation'
66 expect(() => new FixedThreadPool(numberOfWorkers
)).toThrowError(
69 expect(() => new FixedThreadPool(numberOfWorkers
, '')).toThrowError(
74 it('Verify that numberOfWorkers is checked', () => {
75 expect(() => new FixedThreadPool()).toThrowError(
77 'Cannot instantiate a pool without specifying the number of workers'
82 it('Verify that a negative number of workers is checked', () => {
85 new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
87 new Error('Cannot instantiate a pool with a negative number of workers')
91 it('Verify that a non integer number of workers is checked', () => {
94 new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
97 'Cannot instantiate a pool with a non integer number of workers'
102 it('Verify that pool options are checked', () => {
103 let pool
= new FixedThreadPool(
105 './tests/worker-files/thread/testWorker.js'
107 expect(pool
.opts
.enableEvents
).toBe(true)
108 expect(pool
.emitter
).toBeDefined()
109 expect(pool
.opts
.workerChoiceStrategy
).toBe(
110 WorkerChoiceStrategies
.ROUND_ROBIN
112 expect(pool
.opts
.messageHandler
).toBeUndefined()
113 expect(pool
.opts
.errorHandler
).toBeUndefined()
114 expect(pool
.opts
.onlineHandler
).toBeUndefined()
115 expect(pool
.opts
.exitHandler
).toBeUndefined()
117 const testHandler
= () => console
.log('test handler executed')
118 pool
= new FixedThreadPool(
120 './tests/worker-files/thread/testWorker.js',
122 workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
,
124 messageHandler
: testHandler
,
125 errorHandler
: testHandler
,
126 onlineHandler
: testHandler
,
127 exitHandler
: testHandler
130 expect(pool
.opts
.enableEvents
).toBe(false)
131 expect(pool
.emitter
).toBeUndefined()
132 expect(pool
.opts
.workerChoiceStrategy
).toBe(
133 WorkerChoiceStrategies
.LESS_RECENTLY_USED
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
)
142 it("Verify that pool event emitter 'busy' event can register a callback", () => {
143 const pool
= new FixedThreadPool(
145 './tests/worker-files/thread/testWorker.js'
149 pool
.emitter
.on('busy', () => poolBusy
++)
150 for (let i
= 0; i
< numberOfWorkers
* 2; i
++) {
151 promises
.push(pool
.execute({ test
: 'test' }))
153 // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
154 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
155 expect(poolBusy
).toBe(numberOfWorkers
+ 1)