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 class StubPoolWithTasksMapClear
extends FixedThreadPool
{
11 class StubPoolWithIsMainMethod
extends FixedThreadPool
{
17 describe('Abstract pool test suite', () => {
18 it('Simulate worker not found during increaseWorkersTask', () => {
19 const pool
= new StubPoolWithTasksMapClear(
21 './tests/worker-files/thread/testWorker.js'
23 // Simulate worker not found.
24 pool
.removeAllWorker()
25 expect(() => pool
.increaseWorkersTask()).toThrowError(expectedError
)
28 it('Simulate worker not found during decreaseWorkersTasks', () => {
29 const pool
= new StubPoolWithTasksMapClear(
31 './tests/worker-files/thread/testWorker.js',
33 errorHandler
: e
=> console
.error(e
)
36 // Simulate worker not found.
37 pool
.removeAllWorker()
38 expect(() => pool
.decreaseWorkersTasks()).toThrowError(expectedError
)
41 it('Simulate pool creation from a non main thread/process', () => {
44 new StubPoolWithIsMainMethod(
46 './tests/worker-files/thread/testWorker.js',
48 errorHandler
: e
=> console
.error(e
)
51 ).toThrowError(new Error('Cannot start a pool from a worker!'))
54 it('Verify that filePath is checked', () => {
55 expect(() => new StubPoolWithIsMainMethod(1)).toThrowError(
56 new Error('Cannot start a pool from a worker!')
58 expect(() => new StubPoolWithIsMainMethod(1, '')).toThrowError(
59 new Error('Cannot start a pool from a worker!')
63 it('Verify that numberOfWorkers is checked', () => {
64 expect(() => new FixedThreadPool()).toThrowError(
66 'Cannot instantiate a pool without specifying the number of workers'
71 it('Verify that a negative number of workers is checked', () => {
74 new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
76 new Error('Cannot instantiate a pool with a negative number of workers')
80 it('Verify that a non integer number of workers is checked', () => {
83 new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
86 'Cannot instantiate a pool with a non integer number of workers'