1 const expect
= require('expect')
2 const { FixedClusterPool
, FixedThreadPool
} = require('../../../lib/index')
3 const expectedError
= new Error('Worker could not be found in tasks map')
5 const numberOfWorkers
= 1
7 class StubPoolWithTasksMapClear
extends FixedThreadPool
{
13 class StubPoolWithIsMainMethod
extends FixedThreadPool
{
19 describe('Abstract pool test suite', () => {
20 it('Simulate worker not found during increaseWorkersTask', () => {
21 const pool
= new StubPoolWithTasksMapClear(
23 './tests/worker-files/thread/testWorker.js'
25 // Simulate worker not found.
26 pool
.removeAllWorker()
27 expect(() => pool
.increaseWorkersTask()).toThrowError(expectedError
)
31 it('Simulate worker not found during decreaseWorkersTasks', () => {
32 const pool
= new StubPoolWithTasksMapClear(
34 './tests/worker-files/thread/testWorker.js',
36 errorHandler
: e
=> console
.error(e
)
39 // Simulate worker not found.
40 pool
.removeAllWorker()
41 expect(() => pool
.decreaseWorkersTasks()).toThrowError(expectedError
)
45 it('Simulate pool creation from a non main thread/process', () => {
48 new StubPoolWithIsMainMethod(
50 './tests/worker-files/thread/testWorker.js',
52 errorHandler
: e
=> console
.error(e
)
55 ).toThrowError(new Error('Cannot start a pool from a worker!'))
58 it('Verify that filePath is checked', () => {
59 expect(() => new FixedThreadPool(numberOfWorkers
)).toThrowError(
60 new Error('Please specify a file with a worker implementation')
62 expect(() => new FixedThreadPool(numberOfWorkers
, '')).toThrowError(
63 new Error('Please specify a file with a worker implementation')
67 it('Verify that numberOfWorkers is checked', () => {
68 expect(() => new FixedThreadPool()).toThrowError(
70 'Cannot instantiate a pool without specifying the number of workers'
75 it('Verify that a negative number of workers is checked', () => {
78 new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
80 new Error('Cannot instantiate a pool with a negative number of workers')
84 it('Verify that a non integer number of workers is checked', () => {
87 new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
90 'Cannot instantiate a pool with a non integer number of workers'
95 it('Verify that pool options are checked', () => {
96 let pool
= new FixedThreadPool(
98 './tests/worker-files/thread/testWorker.js'
100 expect(pool
.opts
.enableEvents
).toEqual(true)
101 expect(pool
.emitter
).toBeDefined()
103 pool
= new FixedThreadPool(
105 './tests/worker-files/thread/testWorker.js',
110 expect(pool
.opts
.enableEvents
).toEqual(false)
111 expect(pool
.emitter
).toBeUndefined()
115 it("Verify that pool event emitter 'busy' event can register a callback", () => {
116 const pool
= new FixedThreadPool(
118 './tests/worker-files/thread/testWorker.js'
122 pool
.emitter
.on('busy', () => poolBusy
++)
123 for (let i
= 0; i
< numberOfWorkers
* 2; i
++) {
124 promises
.push(pool
.execute({ test
: 'test' }))
126 expect(poolBusy
).toEqual(numberOfWorkers
)