Merge branch 'master' into add-worker-test
[poolifier.git] / tests / pools / thread / fixed.test.js
CommitLineData
506c2a14 1const expect = require('expect')
325f50bc 2const { FixedThreadPool } = require('../../../lib/index')
5c5a1fb7 3const numberOfThreads = 10
515e5da7 4const maxTasks = 400
325f50bc 5const pool = new FixedThreadPool(
5c5a1fb7 6 numberOfThreads,
76b1e974 7 './tests/worker-files/thread/testWorker.js',
325f50bc
S
8 {
9 errorHandler: e => console.error(e),
10 onlineHandler: () => console.log('worker is online')
11 }
12)
76b1e974
S
13const emptyPool = new FixedThreadPool(
14 1,
15 './tests/worker-files/thread/emptyWorker.js'
16)
17const echoPool = new FixedThreadPool(
18 1,
19 './tests/worker-files/thread/echoWorker.js'
20)
325f50bc
S
21const errorPool = new FixedThreadPool(
22 1,
76b1e974 23 './tests/worker-files/thread/errorWorker.js',
325f50bc
S
24 {
25 errorHandler: e => console.error(e),
26 onlineHandler: () => console.log('worker is online')
27 }
28)
515e5da7
APA
29const asyncPool = new FixedThreadPool(
30 1,
76b1e974 31 './tests/worker-files/thread/asyncWorker.js',
515e5da7
APA
32 { maxTasks: maxTasks }
33)
506c2a14 34
35describe('Fixed thread pool test suite ', () => {
36 it('Choose worker round robin test', async () => {
37 const results = new Set()
5c5a1fb7 38 for (let i = 0; i < numberOfThreads; i++) {
fa0f5b28 39 results.add(pool.chooseWorker().threadId)
506c2a14 40 }
5c5a1fb7 41 expect(results.size).toBe(numberOfThreads)
506c2a14 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
106744f7 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
7784f548 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)
32d490eb 88 expect(usedTime).toBeGreaterThanOrEqual(2000)
7784f548 89 })
90
515e5da7
APA
91 it('Verify that maxTasks is set properly', async () => {
92 const worker = asyncPool.chooseWorker()
93 expect(worker.port2.getMaxListeners()).toBe(maxTasks)
94 })
95
506c2a14 96 it('Shutdown test', async () => {
97 let closedThreads = 0
98 pool.workers.forEach(w => {
99 w.on('exit', () => {
100 closedThreads++
101 })
102 })
1f9a5a44 103 await pool.destroy()
5c5a1fb7 104 expect(closedThreads).toBe(numberOfThreads)
506c2a14 105 })
106
107 it('Validations test', () => {
108 let error
109 try {
110 const pool1 = new FixedThreadPool()
111 console.log(pool1)
112 } catch (e) {
113 error = e
114 }
115 expect(error).toBeTruthy()
116 expect(error.message).toBeTruthy()
117 })
118
119 it('Should work even without opts in input', async () => {
76b1e974
S
120 const pool1 = new FixedThreadPool(
121 1,
122 './tests/worker-files/thread/testWorker.js'
123 )
506c2a14 124 const res = await pool1.execute({ test: 'test' })
125 expect(res).toBeFalsy()
126 })
127})