Commit | Line | Data |
---|---|---|
506c2a14 | 1 | const expect = require('expect') |
325f50bc | 2 | const { FixedThreadPool } = require('../../../lib/index') |
85a3f8a7 | 3 | const TestUtils = require('../../test-utils') |
5c5a1fb7 | 4 | const numberOfThreads = 10 |
325f50bc | 5 | const 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 |
12 | const emptyPool = new FixedThreadPool( |
13 | 1, | |
14 | './tests/worker-files/thread/emptyWorker.js' | |
15 | ) | |
16 | const echoPool = new FixedThreadPool( | |
17 | 1, | |
18 | './tests/worker-files/thread/echoWorker.js' | |
19 | ) | |
325f50bc S |
20 | const errorPool = new FixedThreadPool( |
21 | 1, | |
76b1e974 | 22 | './tests/worker-files/thread/errorWorker.js', |
325f50bc S |
23 | { |
24 | errorHandler: e => console.error(e), | |
25 | onlineHandler: () => console.log('worker is online') | |
26 | } | |
27 | ) | |
515e5da7 APA |
28 | const asyncPool = new FixedThreadPool( |
29 | 1, | |
1927ee67 | 30 | './tests/worker-files/thread/asyncWorker.js' |
515e5da7 | 31 | ) |
506c2a14 | 32 | |
a35560ba | 33 | describe('Fixed thread pool test suite', () => { |
0e2503fc JB |
34 | after('Destroy all pools', async () => { |
35 | // We need to clean up the resources after our test | |
36 | await echoPool.destroy() | |
37 | await asyncPool.destroy() | |
38 | await errorPool.destroy() | |
39 | await emptyPool.destroy() | |
40 | }) | |
41 | ||
506c2a14 | 42 | it('Choose worker round robin test', async () => { |
43 | const results = new Set() | |
5c5a1fb7 | 44 | for (let i = 0; i < numberOfThreads; i++) { |
fa0f5b28 | 45 | results.add(pool.chooseWorker().threadId) |
506c2a14 | 46 | } |
5c5a1fb7 | 47 | expect(results.size).toBe(numberOfThreads) |
506c2a14 | 48 | }) |
49 | ||
50 | it('Verify that the function is executed in a worker thread', async () => { | |
51 | const result = await pool.execute({ test: 'test' }) | |
52 | expect(result).toBeDefined() | |
53 | expect(result).toBeFalsy() | |
54 | }) | |
55 | ||
106744f7 | 56 | it('Verify that is possible to invoke the execute method without input', async () => { |
57 | const result = await pool.execute() | |
58 | expect(result).toBeDefined() | |
59 | expect(result).toBeFalsy() | |
60 | }) | |
61 | ||
62 | it('Verify that is possible to have a worker that return undefined', async () => { | |
63 | const result = await emptyPool.execute() | |
64 | expect(result).toBeFalsy() | |
65 | }) | |
66 | ||
67 | it('Verify that data are sent to the worker correctly', async () => { | |
68 | const data = { f: 10 } | |
69 | const result = await echoPool.execute(data) | |
70 | expect(result).toBeTruthy() | |
71 | expect(result.f).toBe(data.f) | |
72 | }) | |
73 | ||
74 | it('Verify that error handling is working properly', async () => { | |
75 | const data = { f: 10 } | |
76 | let inError | |
77 | try { | |
78 | await errorPool.execute(data) | |
79 | } catch (e) { | |
80 | inError = e | |
81 | } | |
82 | expect(inError).toBeTruthy() | |
83 | expect(inError instanceof Error).toBeTruthy() | |
84 | expect(inError.message).toBeTruthy() | |
85 | }) | |
86 | ||
7784f548 | 87 | it('Verify that async function is working properly', async () => { |
88 | const data = { f: 10 } | |
89 | const startTime = new Date().getTime() | |
90 | const result = await asyncPool.execute(data) | |
91 | const usedTime = new Date().getTime() - startTime | |
92 | expect(result).toBeTruthy() | |
93 | expect(result.f).toBe(data.f) | |
32d490eb | 94 | expect(usedTime).toBeGreaterThanOrEqual(2000) |
7784f548 | 95 | }) |
96 | ||
506c2a14 | 97 | it('Shutdown test', async () => { |
85a3f8a7 | 98 | const exitPromise = TestUtils.waitExits(pool, numberOfThreads) |
1f9a5a44 | 99 | await pool.destroy() |
85a3f8a7 APA |
100 | const res = await exitPromise |
101 | expect(res).toBe(numberOfThreads) | |
506c2a14 | 102 | }) |
103 | ||
104 | it('Should work even without opts in input', async () => { | |
76b1e974 S |
105 | const pool1 = new FixedThreadPool( |
106 | 1, | |
107 | './tests/worker-files/thread/testWorker.js' | |
108 | ) | |
506c2a14 | 109 | const res = await pool1.execute({ test: 'test' }) |
110 | expect(res).toBeFalsy() | |
0e2503fc JB |
111 | // We need to clean up the resources after our test |
112 | await pool1.destroy() | |
506c2a14 | 113 | }) |
8d3782fa JB |
114 | |
115 | it('Verify that a pool with zero worker fails', async () => { | |
116 | expect( | |
117 | () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js') | |
118 | ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker')) | |
119 | }) | |
506c2a14 | 120 | }) |