Stricter tests expectations
[poolifier.git] / tests / pools / thread / fixed.test.js
1 const { expect } = require('expect')
2 const { FixedThreadPool } = require('../../../lib/index')
3 const WorkerFunctions = require('../../test-types')
4 const TestUtils = require('../../test-utils')
5 const numberOfThreads = 10
6 const pool = new FixedThreadPool(
7 numberOfThreads,
8 './tests/worker-files/thread/testWorker.js',
9 {
10 errorHandler: e => console.error(e)
11 }
12 )
13 const emptyPool = new FixedThreadPool(
14 1,
15 './tests/worker-files/thread/emptyWorker.js',
16 { exitHandler: () => console.log('empty pool worker exited') }
17 )
18 const echoPool = new FixedThreadPool(
19 1,
20 './tests/worker-files/thread/echoWorker.js'
21 )
22 const errorPool = new FixedThreadPool(
23 1,
24 './tests/worker-files/thread/errorWorker.js',
25 {
26 errorHandler: e => console.error(e)
27 }
28 )
29 const asyncErrorPool = new FixedThreadPool(
30 1,
31 './tests/worker-files/thread/asyncErrorWorker.js',
32 {
33 errorHandler: e => console.error(e)
34 }
35 )
36 const asyncPool = new FixedThreadPool(
37 1,
38 './tests/worker-files/thread/asyncWorker.js'
39 )
40
41 describe('Fixed thread pool test suite', () => {
42 after('Destroy all pools', async () => {
43 // We need to clean up the resources after our test
44 await echoPool.destroy()
45 await asyncPool.destroy()
46 await errorPool.destroy()
47 await asyncErrorPool.destroy()
48 await emptyPool.destroy()
49 })
50
51 it('Choose worker round robin test', async () => {
52 const results = new Set()
53 for (let i = 0; i < numberOfThreads; i++) {
54 results.add(pool.chooseWorker().threadId)
55 }
56 expect(results.size).toBe(numberOfThreads)
57 })
58
59 it('Verify that the function is executed in a worker thread', async () => {
60 let result = await pool.execute({
61 function: WorkerFunctions.fibonacci
62 })
63 expect(result).toBe(false)
64 result = await pool.execute({
65 function: WorkerFunctions.factorial
66 })
67 expect(result).toBe(false)
68 })
69
70 it('Verify that is possible to invoke the execute method without input', async () => {
71 const result = await pool.execute()
72 expect(result).toBe(false)
73 })
74
75 it('Verify that busy event is emitted', async () => {
76 const promises = []
77 let poolBusy = 0
78 pool.emitter.on('busy', () => poolBusy++)
79 for (let i = 0; i < numberOfThreads * 2; i++) {
80 promises.push(pool.execute())
81 }
82 // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
83 // So in total numberOfThreads + 1 times for a loop submitting up to numberOfThreads * 2 tasks to the fixed pool.
84 expect(poolBusy).toBe(numberOfThreads + 1)
85 })
86
87 it('Verify that is possible to have a worker that return undefined', async () => {
88 const result = await emptyPool.execute()
89 expect(result).toBeUndefined()
90 })
91
92 it('Verify that data are sent to the worker correctly', async () => {
93 const data = { f: 10 }
94 const result = await echoPool.execute(data)
95 expect(result).toEqual(data)
96 })
97
98 it('Verify that error handling is working properly:sync', async () => {
99 const data = { f: 10 }
100 let inError
101 try {
102 await errorPool.execute(data)
103 } catch (e) {
104 inError = e
105 }
106 expect(inError).toBeDefined()
107 expect(inError).toBeInstanceOf(Error)
108 expect(inError.message).toBeDefined()
109 expect(typeof inError.message === 'string').toBe(true)
110 expect(inError.message).toBe('Error Message from ThreadWorker')
111 })
112
113 it('Verify that error handling is working properly:async', async () => {
114 const data = { f: 10 }
115 let inError
116 try {
117 await asyncErrorPool.execute(data)
118 } catch (e) {
119 inError = e
120 }
121 expect(inError).toBeDefined()
122 expect(inError).toBeInstanceOf(Error)
123 expect(inError.message).toBeDefined()
124 expect(typeof inError.message === 'string').toBe(true)
125 expect(inError.message).toBe('Error Message from ThreadWorker:async')
126 })
127
128 it('Verify that async function is working properly', async () => {
129 const data = { f: 10 }
130 const startTime = new Date().getTime()
131 const result = await asyncPool.execute(data)
132 const usedTime = new Date().getTime() - startTime
133 expect(result).toEqual(data)
134 expect(usedTime).toBeGreaterThanOrEqual(2000)
135 })
136
137 it('Shutdown test', async () => {
138 const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
139 await pool.destroy()
140 const numberOfExitEvents = await exitPromise
141 expect(numberOfExitEvents).toBe(numberOfThreads)
142 })
143
144 it('Should work even without opts in input', async () => {
145 const pool1 = new FixedThreadPool(
146 1,
147 './tests/worker-files/thread/testWorker.js'
148 )
149 const res = await pool1.execute()
150 expect(res).toBe(false)
151 // We need to clean up the resources after our test
152 await pool1.destroy()
153 })
154
155 it('Verify that a pool with zero worker fails', async () => {
156 expect(
157 () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
158 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))
159 })
160 })