482ce5e39db81c3eb90b92808052a99790049873
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
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')
4
5 class StubPoolWithTasksMapClear extends FixedThreadPool {
6 removeAllWorker () {
7 this.tasks.clear()
8 }
9 }
10
11 class StubPoolWithIsMainMethod extends FixedThreadPool {
12 isMain () {
13 return false
14 }
15 }
16
17 describe('Abstract pool test suite', () => {
18 it('Simulate worker not found during increaseWorkersTask', () => {
19 const pool = new StubPoolWithTasksMapClear(
20 1,
21 './tests/worker-files/thread/testWorker.js',
22 {
23 errorHandler: e => console.error(e)
24 }
25 )
26 // Simulate worker not found.
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,
34 './tests/worker-files/thread/testWorker.js',
35 {
36 errorHandler: e => console.error(e)
37 }
38 )
39 // Simulate worker not found.
40 pool.removeAllWorker()
41 expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError)
42 })
43
44 it('Simulate pool creation from a non main thread/process', () => {
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!'))
55 })
56
57 it('Verify that filePath is checked', () => {
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 )
92 })
93 })