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