Removed max tasks (#225)
[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,
1927ee67 21 './tests/worker-files/thread/testWorker.js'
3ec964d6 22 )
0e2503fc 23 // Simulate worker not found.
3ec964d6 24 pool.removeAllWorker()
25 expect(() => pool.increaseWorkersTask()).toThrowError(expectedError)
26 })
27
28 it('Simulate worker not found during decreaseWorkersTasks', () => {
29 const pool = new StubPoolWithTasksMapClear(
30 1,
8d3782fa 31 './tests/worker-files/thread/testWorker.js',
3ec964d6 32 {
33 errorHandler: e => console.error(e)
34 }
35 )
0e2503fc 36 // Simulate worker not found.
3ec964d6 37 pool.removeAllWorker()
38 expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError)
39 })
40
41 it('Simulate pool creation from a non main thread/process', () => {
8d3782fa
JB
42 expect(
43 () =>
44 new StubPoolWithIsMainMethod(
45 1,
46 './tests/worker-files/thread/testWorker.js',
47 {
48 errorHandler: e => console.error(e)
49 }
50 )
51 ).toThrowError(new Error('Cannot start a pool from a worker!'))
3ec964d6 52 })
c510fea7
APA
53
54 it('Verify that filePath is checked', () => {
8d3782fa
JB
55 expect(() => new StubPoolWithIsMainMethod(1)).toThrowError(
56 new Error('Cannot start a pool from a worker!')
57 )
58 expect(() => new StubPoolWithIsMainMethod(1, '')).toThrowError(
59 new Error('Cannot start a pool from a worker!')
60 )
61 })
62
63 it('Verify that numberOfWorkers is checked', () => {
64 expect(() => new FixedThreadPool()).toThrowError(
65 new Error(
66 'Cannot instantiate a pool without specifying the number of workers'
67 )
68 )
69 })
70
71 it('Verify that a negative number of workers is checked', () => {
72 expect(
73 () =>
74 new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
75 ).toThrowError(
76 new Error('Cannot instantiate a pool with a negative number of workers')
77 )
78 })
79
80 it('Verify that a non integer number of workers is checked', () => {
81 expect(
82 () =>
83 new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
84 ).toThrowError(
85 new Error(
86 'Cannot instantiate a pool with a non integer number of workers'
87 )
88 )
c510fea7 89 })
3ec964d6 90})