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