Add benchmark for quick select algorithm evaluation. (#255)
[poolifier.git] / tests / pools / thread / fixed.test.js
CommitLineData
506c2a14 1const expect = require('expect')
325f50bc 2const { FixedThreadPool } = require('../../../lib/index')
85a3f8a7 3const TestUtils = require('../../test-utils')
5c5a1fb7 4const numberOfThreads = 10
325f50bc 5const pool = new FixedThreadPool(
5c5a1fb7 6 numberOfThreads,
76b1e974 7 './tests/worker-files/thread/testWorker.js',
325f50bc 8 {
e5177d86 9 errorHandler: e => console.error(e)
325f50bc
S
10 }
11)
76b1e974
S
12const emptyPool = new FixedThreadPool(
13 1,
7fc5cce6
APA
14 './tests/worker-files/thread/emptyWorker.js',
15 { exitHandler: () => console.log('WORKER EXITED') }
76b1e974
S
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,
1927ee67 31 './tests/worker-files/thread/asyncWorker.js'
515e5da7 32)
506c2a14 33
a35560ba 34describe('Fixed thread pool test suite', () => {
0e2503fc
JB
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
506c2a14 43 it('Choose worker round robin test', async () => {
44 const results = new Set()
5c5a1fb7 45 for (let i = 0; i < numberOfThreads; i++) {
fa0f5b28 46 results.add(pool.chooseWorker().threadId)
506c2a14 47 }
5c5a1fb7 48 expect(results.size).toBe(numberOfThreads)
506c2a14 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
106744f7 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
7784f548 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)
32d490eb 95 expect(usedTime).toBeGreaterThanOrEqual(2000)
7784f548 96 })
97
506c2a14 98 it('Shutdown test', async () => {
85a3f8a7 99 const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
1f9a5a44 100 await pool.destroy()
85a3f8a7
APA
101 const res = await exitPromise
102 expect(res).toBe(numberOfThreads)
506c2a14 103 })
104
105 it('Should work even without opts in input', async () => {
76b1e974
S
106 const pool1 = new FixedThreadPool(
107 1,
108 './tests/worker-files/thread/testWorker.js'
109 )
506c2a14 110 const res = await pool1.execute({ test: 'test' })
111 expect(res).toBeFalsy()
0e2503fc
JB
112 // We need to clean up the resources after our test
113 await pool1.destroy()
506c2a14 114 })
8d3782fa
JB
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 })
506c2a14 121})