Improve worker selection strategies coverage. (#220)
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
CommitLineData
3ec964d6 1const expect = require('expect')
8d3782fa 2const { FixedClusterPool, FixedThreadPool } = require('../../../lib/index')
3ec964d6 3const expectedError = new Error('Worker could not be found in tasks map')
4
5class StubPoolWithTasksMapClear extends FixedThreadPool {
6 removeAllWorker () {
7 this.tasks.clear()
8 }
9}
10
11class StubPoolWithIsMainMethod extends FixedThreadPool {
12 isMain () {
13 return false
14 }
15}
16
a35560ba 17describe('Abstract pool test suite', () => {
3ec964d6 18 it('Simulate worker not found during increaseWorkersTask', () => {
19 const pool = new StubPoolWithTasksMapClear(
20 1,
8d3782fa 21 './tests/worker-files/thread/testWorker.js',
3ec964d6 22 {
23 errorHandler: e => console.error(e)
24 }
25 )
0e2503fc 26 // Simulate worker not found.
3ec964d6 27 pool.removeAllWorker()
28 expect(() => pool.increaseWorkersTask()).toThrowError(expectedError)
29 })
30
31 it('Simulate worker not found during decreaseWorkersTasks', () => {
32 const pool = new StubPoolWithTasksMapClear(
33 1,
8d3782fa 34 './tests/worker-files/thread/testWorker.js',
3ec964d6 35 {
36 errorHandler: e => console.error(e)
37 }
38 )
0e2503fc 39 // Simulate worker not found.
3ec964d6 40 pool.removeAllWorker()
41 expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError)
42 })
43
44 it('Simulate pool creation from a non main thread/process', () => {
8d3782fa
JB
45 expect(
46 () =>
47 new StubPoolWithIsMainMethod(
48 1,
49 './tests/worker-files/thread/testWorker.js',
50 {
51 errorHandler: e => console.error(e)
52 }
53 )
54 ).toThrowError(new Error('Cannot start a pool from a worker!'))
3ec964d6 55 })
c510fea7
APA
56
57 it('Verify that filePath is checked', () => {
8d3782fa
JB
58 expect(() => new StubPoolWithIsMainMethod(1)).toThrowError(
59 new Error('Cannot start a pool from a worker!')
60 )
61 expect(() => new StubPoolWithIsMainMethod(1, '')).toThrowError(
62 new Error('Cannot start a pool from a worker!')
63 )
64 })
65
66 it('Verify that numberOfWorkers is checked', () => {
67 expect(() => new FixedThreadPool()).toThrowError(
68 new Error(
69 'Cannot instantiate a pool without specifying the number of workers'
70 )
71 )
72 })
73
74 it('Verify that a negative number of workers is checked', () => {
75 expect(
76 () =>
77 new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
78 ).toThrowError(
79 new Error('Cannot instantiate a pool with a negative number of workers')
80 )
81 })
82
83 it('Verify that a non integer number of workers is checked', () => {
84 expect(
85 () =>
86 new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
87 ).toThrowError(
88 new Error(
89 'Cannot instantiate a pool with a non integer number of workers'
90 )
91 )
c510fea7 92 })
3ec964d6 93})