Benchmarks and tests code cleanups. (#257)
[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 const numberOfWorkers = 1
6
7 class StubPoolWithTasksMapClear extends FixedThreadPool {
8 removeAllWorker () {
9 this.tasks.clear()
10 }
11 }
12
13 class StubPoolWithIsMainMethod extends FixedThreadPool {
14 isMain () {
15 return false
16 }
17 }
18
19 describe('Abstract pool test suite', () => {
20 it('Simulate worker not found during increaseWorkersTask', () => {
21 const pool = new StubPoolWithTasksMapClear(
22 numberOfWorkers,
23 './tests/worker-files/thread/testWorker.js'
24 )
25 // Simulate worker not found.
26 pool.removeAllWorker()
27 expect(() => pool.increaseWorkersTask()).toThrowError(expectedError)
28 pool.destroy()
29 })
30
31 it('Simulate worker not found during decreaseWorkersTasks', () => {
32 const pool = new StubPoolWithTasksMapClear(
33 numberOfWorkers,
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 pool.destroy()
43 })
44
45 it('Simulate pool creation from a non main thread/process', () => {
46 expect(
47 () =>
48 new StubPoolWithIsMainMethod(
49 numberOfWorkers,
50 './tests/worker-files/thread/testWorker.js',
51 {
52 errorHandler: e => console.error(e)
53 }
54 )
55 ).toThrowError(new Error('Cannot start a pool from a worker!'))
56 })
57
58 it('Verify that filePath is checked', () => {
59 const expectedError = new Error(
60 'Please specify a file with a worker implementation'
61 )
62 expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError(
63 expectedError
64 )
65 expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError(
66 expectedError
67 )
68 })
69
70 it('Verify that numberOfWorkers is checked', () => {
71 expect(() => new FixedThreadPool()).toThrowError(
72 new Error(
73 'Cannot instantiate a pool without specifying the number of workers'
74 )
75 )
76 })
77
78 it('Verify that a negative number of workers is checked', () => {
79 expect(
80 () =>
81 new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js')
82 ).toThrowError(
83 new Error('Cannot instantiate a pool with a negative number of workers')
84 )
85 })
86
87 it('Verify that a non integer number of workers is checked', () => {
88 expect(
89 () =>
90 new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js')
91 ).toThrowError(
92 new Error(
93 'Cannot instantiate a pool with a non integer number of workers'
94 )
95 )
96 })
97
98 it('Verify that pool options are checked', () => {
99 let pool = new FixedThreadPool(
100 numberOfWorkers,
101 './tests/worker-files/thread/testWorker.js'
102 )
103 expect(pool.opts.enableEvents).toEqual(true)
104 expect(pool.emitter).toBeDefined()
105 pool.destroy()
106 pool = new FixedThreadPool(
107 numberOfWorkers,
108 './tests/worker-files/thread/testWorker.js',
109 {
110 enableEvents: false
111 }
112 )
113 expect(pool.opts.enableEvents).toEqual(false)
114 expect(pool.emitter).toBeUndefined()
115 pool.destroy()
116 })
117
118 it("Verify that pool event emitter 'busy' event can register a callback", () => {
119 const pool = new FixedThreadPool(
120 numberOfWorkers,
121 './tests/worker-files/thread/testWorker.js'
122 )
123 const promises = []
124 let poolBusy = 0
125 pool.emitter.on('busy', () => poolBusy++)
126 for (let i = 0; i < numberOfWorkers * 2; i++) {
127 promises.push(pool.execute({ test: 'test' }))
128 }
129 expect(poolBusy).toEqual(numberOfWorkers)
130 pool.destroy()
131 })
132 })