9a2d74d30bc2f2bb194effd9494b90bf7252b6ed
[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 maxTasks = 400
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 )
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 onlineHandler: () => console.log('worker is online')
27 }
28 )
29 const asyncPool = new FixedThreadPool(
30 1,
31 './tests/worker-files/thread/asyncWorker.js',
32 { maxTasks: maxTasks }
33 )
34
35 describe('Fixed thread pool test suite', () => {
36 after('Destroy all pools', async () => {
37 // We need to clean up the resources after our test
38 await echoPool.destroy()
39 await asyncPool.destroy()
40 await errorPool.destroy()
41 await emptyPool.destroy()
42 })
43
44 it('Choose worker round robin test', async () => {
45 const results = new Set()
46 for (let i = 0; i < numberOfThreads; i++) {
47 results.add(pool.chooseWorker().threadId)
48 }
49 expect(results.size).toBe(numberOfThreads)
50 })
51
52 it('Verify that the function is executed in a worker thread', async () => {
53 const result = await pool.execute({ test: 'test' })
54 expect(result).toBeDefined()
55 expect(result).toBeFalsy()
56 })
57
58 it('Verify that is possible to invoke the execute method without input', async () => {
59 const result = await pool.execute()
60 expect(result).toBeDefined()
61 expect(result).toBeFalsy()
62 })
63
64 it('Verify that is possible to have a worker that return undefined', async () => {
65 const result = await emptyPool.execute()
66 expect(result).toBeFalsy()
67 })
68
69 it('Verify that data are sent to the worker correctly', async () => {
70 const data = { f: 10 }
71 const result = await echoPool.execute(data)
72 expect(result).toBeTruthy()
73 expect(result.f).toBe(data.f)
74 })
75
76 it('Verify that error handling is working properly', async () => {
77 const data = { f: 10 }
78 let inError
79 try {
80 await errorPool.execute(data)
81 } catch (e) {
82 inError = e
83 }
84 expect(inError).toBeTruthy()
85 expect(inError instanceof Error).toBeTruthy()
86 expect(inError.message).toBeTruthy()
87 })
88
89 it('Verify that async function is working properly', async () => {
90 const data = { f: 10 }
91 const startTime = new Date().getTime()
92 const result = await asyncPool.execute(data)
93 const usedTime = new Date().getTime() - startTime
94 expect(result).toBeTruthy()
95 expect(result.f).toBe(data.f)
96 expect(usedTime).toBeGreaterThanOrEqual(2000)
97 })
98
99 it('Verify that maxTasks is set properly', async () => {
100 const worker = asyncPool.chooseWorker()
101 expect(worker.port2.getMaxListeners()).toBe(maxTasks)
102 })
103
104 it('Shutdown test', async () => {
105 const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
106 await pool.destroy()
107 const res = await exitPromise
108 expect(res).toBe(numberOfThreads)
109 })
110
111 it('Should work even without opts in input', async () => {
112 const pool1 = new FixedThreadPool(
113 1,
114 './tests/worker-files/thread/testWorker.js'
115 )
116 const res = await pool1.execute({ test: 'test' })
117 expect(res).toBeFalsy()
118 // We need to clean up the resources after our test
119 await pool1.destroy()
120 })
121
122 it('Verify that a pool with zero worker fails', async () => {
123 expect(
124 () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
125 ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker'))
126 })
127 })