Merge pull request #161 from pioardi/issue-70
[poolifier.git] / tests / pools / thread / fixed.test.js
1 const expect = require('expect')
2 const { FixedThreadPool } = require('../../../lib/index')
3 const numberOfThreads = 10
4 const maxTasks = 400
5 const pool = new FixedThreadPool(
6 numberOfThreads,
7 './tests/worker/thread/testWorker.js',
8 {
9 errorHandler: e => console.error(e)
10 }
11 )
12 const emptyPool = new FixedThreadPool(1, './tests/worker/thread/emptyWorker.js')
13 const echoPool = new FixedThreadPool(1, './tests/worker/thread/echoWorker.js')
14 const errorPool = new FixedThreadPool(
15 1,
16 './tests/worker/thread/errorWorker.js',
17 {
18 errorHandler: e => console.error(e),
19 onlineHandler: () => console.log('worker is online')
20 }
21 )
22 const asyncPool = new FixedThreadPool(
23 1,
24 './tests/worker/thread/asyncWorker.js',
25 { maxTasks: maxTasks }
26 )
27
28 describe('Fixed thread pool test suite ', () => {
29 it('Choose worker round robin test', async () => {
30 const results = new Set()
31 for (let i = 0; i < numberOfThreads; i++) {
32 results.add(pool.chooseWorker().threadId)
33 }
34 expect(results.size).toBe(numberOfThreads)
35 })
36
37 it('Verify that the function is executed in a worker thread', async () => {
38 const result = await pool.execute({ test: 'test' })
39 expect(result).toBeDefined()
40 expect(result).toBeFalsy()
41 })
42
43 it('Verify that is possible to invoke the execute method without input', async () => {
44 const result = await pool.execute()
45 expect(result).toBeDefined()
46 expect(result).toBeFalsy()
47 })
48
49 it('Verify that is possible to have a worker that return undefined', async () => {
50 const result = await emptyPool.execute()
51 expect(result).toBeFalsy()
52 })
53
54 it('Verify that data are sent to the worker correctly', async () => {
55 const data = { f: 10 }
56 const result = await echoPool.execute(data)
57 expect(result).toBeTruthy()
58 expect(result.f).toBe(data.f)
59 })
60
61 it('Verify that error handling is working properly', async () => {
62 const data = { f: 10 }
63 let inError
64 try {
65 await errorPool.execute(data)
66 } catch (e) {
67 inError = e
68 }
69 expect(inError).toBeTruthy()
70 expect(inError instanceof Error).toBeTruthy()
71 expect(inError.message).toBeTruthy()
72 })
73
74 it('Verify that async function is working properly', async () => {
75 const data = { f: 10 }
76 const startTime = new Date().getTime()
77 const result = await asyncPool.execute(data)
78 const usedTime = new Date().getTime() - startTime
79 expect(result).toBeTruthy()
80 expect(result.f).toBe(data.f)
81 expect(usedTime).toBeGreaterThanOrEqual(2000)
82 })
83
84 it('Verify that maxTasks is set properly', async () => {
85 const worker = asyncPool.chooseWorker()
86 expect(worker.port2.getMaxListeners()).toBe(maxTasks)
87 })
88
89 it('Shutdown test', async () => {
90 let closedThreads = 0
91 pool.workers.forEach(w => {
92 w.on('exit', () => {
93 closedThreads++
94 })
95 })
96 await pool.destroy()
97 expect(closedThreads).toBe(numberOfThreads)
98 })
99
100 it('Validations test', () => {
101 let error
102 try {
103 const pool1 = new FixedThreadPool()
104 console.log(pool1)
105 } catch (e) {
106 error = e
107 }
108 expect(error).toBeTruthy()
109 expect(error.message).toBeTruthy()
110 })
111
112 it('Should work even without opts in input', async () => {
113 const pool1 = new FixedThreadPool(1, './tests/worker/thread/testWorker.js')
114 const res = await pool1.execute({ test: 'test' })
115 expect(res).toBeFalsy()
116 })
117 })