2a7d005b3dcd6d88a53e4e53e1d2216c55483de4
[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 it('Choose worker round robin test', async () => {
37 const results = new Set()
38 for (let i = 0; i < numberOfThreads; i++) {
39 results.add(pool.chooseWorker().threadId)
40 }
41 expect(results.size).toBe(numberOfThreads)
42 })
43
44 it('Verify that the function is executed in a worker thread', async () => {
45 const result = await pool.execute({ test: 'test' })
46 expect(result).toBeDefined()
47 expect(result).toBeFalsy()
48 })
49
50 it('Verify that is possible to invoke the execute method without input', async () => {
51 const result = await pool.execute()
52 expect(result).toBeDefined()
53 expect(result).toBeFalsy()
54 })
55
56 it('Verify that is possible to have a worker that return undefined', async () => {
57 const result = await emptyPool.execute()
58 expect(result).toBeFalsy()
59 })
60
61 it('Verify that data are sent to the worker correctly', async () => {
62 const data = { f: 10 }
63 const result = await echoPool.execute(data)
64 expect(result).toBeTruthy()
65 expect(result.f).toBe(data.f)
66 })
67
68 it('Verify that error handling is working properly', async () => {
69 const data = { f: 10 }
70 let inError
71 try {
72 await errorPool.execute(data)
73 } catch (e) {
74 inError = e
75 }
76 expect(inError).toBeTruthy()
77 expect(inError instanceof Error).toBeTruthy()
78 expect(inError.message).toBeTruthy()
79 })
80
81 it('Verify that async function is working properly', async () => {
82 const data = { f: 10 }
83 const startTime = new Date().getTime()
84 const result = await asyncPool.execute(data)
85 const usedTime = new Date().getTime() - startTime
86 expect(result).toBeTruthy()
87 expect(result.f).toBe(data.f)
88 expect(usedTime).toBeGreaterThanOrEqual(2000)
89 })
90
91 it('Verify that maxTasks is set properly', async () => {
92 const worker = asyncPool.chooseWorker()
93 expect(worker.port2.getMaxListeners()).toBe(maxTasks)
94 })
95
96 it('Shutdown test', async () => {
97 const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
98 await pool.destroy()
99 const res = await exitPromise
100 expect(res).toBe(numberOfThreads)
101 })
102
103 it('Should work even without opts in input', async () => {
104 const pool1 = new FixedThreadPool(
105 1,
106 './tests/worker-files/thread/testWorker.js'
107 )
108 const res = await pool1.execute({ test: 'test' })
109 expect(res).toBeFalsy()
110 })
111 })