1 const expect
= require('expect')
2 const { DynamicClusterPool
} = require('../../../lib/index')
3 const TestUtils
= require('../../test-utils')
6 const pool
= new DynamicClusterPool(
9 './tests/worker-files/cluster/testWorker.js',
11 errorHandler
: e
=> console
.error(e
)
15 describe('Dynamic cluster pool test suite ', () => {
16 it('Verify that the function is executed in a worker cluster', async () => {
17 const result
= await pool
.execute({ test
: 'test' })
18 expect(result
).toBeDefined()
19 expect(result
).toBeFalsy()
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 () => {
25 pool
.emitter
.on('FullPool', () => fullPool
++)
26 for (let i
= 0; i
< max
* 2; i
++) {
27 promises
.push(pool
.execute({ test
: 'test' }))
29 expect(pool
.workers
.length
).toBeLessThanOrEqual(max
)
30 expect(pool
.workers
.length
).toBeGreaterThan(min
)
31 expect(fullPool
> 1).toBeTruthy()
32 const numberOfExitEvents
= await TestUtils
.waitExits(pool
, max
- min
)
33 expect(numberOfExitEvents
).toBe(max
- min
)
36 it('Verify scale worker up and down is working', async () => {
37 expect(pool
.workers
.length
).toBe(min
)
38 for (let i
= 0; i
< max
* 10; i
++) {
39 pool
.execute({ test
: 'test' })
41 expect(pool
.workers
.length
).toBeGreaterThan(min
)
42 await TestUtils
.waitExits(pool
, max
- min
)
43 expect(pool
.workers
.length
).toBe(min
)
44 for (let i
= 0; i
< max
* 10; i
++) {
45 pool
.execute({ test
: 'test' })
47 expect(pool
.workers
.length
).toBeGreaterThan(min
)
48 await TestUtils
.waitExits(pool
, max
- min
)
49 expect(pool
.workers
.length
).toBe(min
)
52 it('Shutdown test', async () => {
53 const exitPromise
= TestUtils
.waitExits(pool
, min
)
55 const res
= await exitPromise
59 it('Should work even without opts in input', async () => {
60 const pool1
= new DynamicClusterPool(
63 './tests/worker-files/cluster/testWorker.js'
65 const result
= await pool1
.execute({ test
: 'test' })
66 expect(result
).toBeFalsy()
67 // We need to clean up the resources after our test
71 it('Verify scale processes up and down is working when long running task is used:hard', async () => {
72 const longRunningPool
= new DynamicClusterPool(
75 './tests/worker-files/cluster/longRunningWorkerHardBehavior.js'
77 expect(longRunningPool
.workers
.length
).toBe(min
)
78 for (let i
= 0; i
< max
* 10; i
++) {
79 longRunningPool
.execute({ test
: 'test' })
81 expect(longRunningPool
.workers
.length
).toBe(max
)
82 await TestUtils
.waitExits(longRunningPool
, max
- min
)
83 // Here we expect the workers to be at the max size since that the task is still running
84 expect(longRunningPool
.workers
.length
).toBe(min
)
85 // We need to clean up the resources after our test
86 await longRunningPool
.destroy()
89 it('Verify scale processes up and down is working when long running task is used:soft', async () => {
90 const longRunningPool
= new DynamicClusterPool(
93 './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js'
95 expect(longRunningPool
.workers
.length
).toBe(min
)
96 for (let i
= 0; i
< max
* 10; i
++) {
97 longRunningPool
.execute({ test
: 'test' })
99 expect(longRunningPool
.workers
.length
).toBe(max
)
100 await TestUtils
.sleep(1500)
101 // Here we expect the workers to be at the max size since that the task is still running
102 expect(longRunningPool
.workers
.length
).toBe(max
)
103 // We need to clean up the resources after our test
104 await longRunningPool
.destroy()