Ensure we always clean up resources in ut. (#214)
[poolifier.git] / tests / pools / thread / dynamic.test.js
1 const expect = require('expect')
2 const { DynamicThreadPool } = require('../../../lib/index')
3 const TestUtils = require('../../test-utils')
4 const min = 1
5 const max = 3
6 const pool = new DynamicThreadPool(
7 min,
8 max,
9 './tests/worker-files/thread/testWorker.js',
10 {
11 errorHandler: e => console.error(e)
12 }
13 )
14
15 describe('Dynamic thread pool test suite', () => {
16 it('Verify that the function is executed in a worker thread', async () => {
17 const result = await pool.execute({ test: 'test' })
18 expect(result).toBeDefined()
19 expect(result).toBeFalsy()
20 })
21
22 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
23 const promises = []
24 let fullPool = 0
25 pool.emitter.on('FullPool', () => fullPool++)
26 for (let i = 0; i < max * 2; i++) {
27 promises.push(pool.execute({ test: 'test' }))
28 }
29 expect(pool.workers.length).toBe(max)
30 expect(fullPool > 1).toBeTruthy()
31 const res = await TestUtils.waitExits(pool, max - min)
32 expect(res).toBe(max - min)
33 })
34
35 it('Verify scale thread up and down is working', async () => {
36 expect(pool.workers.length).toBe(min)
37 for (let i = 0; i < max * 10; i++) {
38 pool.execute({ test: 'test' })
39 }
40 expect(pool.workers.length).toBe(max)
41 await TestUtils.waitExits(pool, max - min)
42 expect(pool.workers.length).toBe(min)
43 for (let i = 0; i < max * 10; i++) {
44 pool.execute({ test: 'test' })
45 }
46 expect(pool.workers.length).toBe(max)
47 await TestUtils.waitExits(pool, max - min)
48 expect(pool.workers.length).toBe(min)
49 })
50
51 it('Shutdown test', async () => {
52 let closedThreads = 0
53 pool.workers.forEach(w => {
54 w.on('exit', () => {
55 closedThreads++
56 })
57 })
58 await pool.destroy()
59 expect(closedThreads).toBe(min)
60 })
61
62 it('Validations test', () => {
63 expect(() => {
64 const pool1 = new DynamicThreadPool()
65 }).toThrowError(
66 new Error('Please specify a file with a worker implementation')
67 )
68 })
69
70 it('Should work even without opts in input', async () => {
71 const pool1 = new DynamicThreadPool(
72 1,
73 1,
74 './tests/worker-files/thread/testWorker.js'
75 )
76 const res = await pool1.execute({ test: 'test' })
77 expect(res).toBeFalsy()
78 // We need to clean up the resources after our test
79 await pool1.destroy()
80 })
81
82 it('Verify scale thread up and down is working when long running task is used:hard', async () => {
83 const longRunningPool = new DynamicThreadPool(
84 min,
85 max,
86 './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
87 {
88 errorHandler: e => console.error(e),
89 onlineHandler: () => console.log('worker is online')
90 }
91 )
92 expect(longRunningPool.workers.length).toBe(min)
93 for (let i = 0; i < max * 10; i++) {
94 longRunningPool.execute({ test: 'test' })
95 }
96 expect(longRunningPool.workers.length).toBe(max)
97 await TestUtils.waitExits(longRunningPool, max - min)
98 expect(longRunningPool.workers.length).toBe(min)
99 // We need to clean up the resources after our test
100 await longRunningPool.destroy()
101 })
102
103 it('Verify scale thread up and down is working when long running task is used:soft', async () => {
104 const longRunningPool = new DynamicThreadPool(
105 min,
106 max,
107 './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
108 {
109 errorHandler: e => console.error(e),
110 onlineHandler: () => console.log('worker is online')
111 }
112 )
113 expect(longRunningPool.workers.length).toBe(min)
114 for (let i = 0; i < max * 10; i++) {
115 longRunningPool.execute({ test: 'test' })
116 }
117 expect(longRunningPool.workers.length).toBe(max)
118 await TestUtils.sleep(1500)
119 // Here we expect the workers to be at the max size since that the task is still running
120 expect(longRunningPool.workers.length).toBe(max)
121 // We need to clean up the resources after our test
122 await longRunningPool.destroy()
123 })
124 })