Commit | Line | Data |
---|---|---|
3ec964d6 | 1 | const expect = require('expect') |
8d3782fa | 2 | const { FixedClusterPool, FixedThreadPool } = require('../../../lib/index') |
3ec964d6 | 3 | const expectedError = new Error('Worker could not be found in tasks map') |
4 | ||
7c0ba920 JB |
5 | const numberOfWorkers = 1 |
6 | ||
3ec964d6 | 7 | class StubPoolWithTasksMapClear extends FixedThreadPool { |
8 | removeAllWorker () { | |
9 | this.tasks.clear() | |
10 | } | |
11 | } | |
12 | ||
13 | class StubPoolWithIsMainMethod extends FixedThreadPool { | |
14 | isMain () { | |
15 | return false | |
16 | } | |
17 | } | |
18 | ||
a35560ba | 19 | describe('Abstract pool test suite', () => { |
3ec964d6 | 20 | it('Simulate worker not found during increaseWorkersTask', () => { |
21 | const pool = new StubPoolWithTasksMapClear( | |
7c0ba920 | 22 | numberOfWorkers, |
1927ee67 | 23 | './tests/worker-files/thread/testWorker.js' |
3ec964d6 | 24 | ) |
0e2503fc | 25 | // Simulate worker not found. |
3ec964d6 | 26 | pool.removeAllWorker() |
27 | expect(() => pool.increaseWorkersTask()).toThrowError(expectedError) | |
7c0ba920 | 28 | pool.destroy() |
3ec964d6 | 29 | }) |
30 | ||
31 | it('Simulate worker not found during decreaseWorkersTasks', () => { | |
32 | const pool = new StubPoolWithTasksMapClear( | |
7c0ba920 | 33 | numberOfWorkers, |
8d3782fa | 34 | './tests/worker-files/thread/testWorker.js', |
3ec964d6 | 35 | { |
36 | errorHandler: e => console.error(e) | |
37 | } | |
38 | ) | |
0e2503fc | 39 | // Simulate worker not found. |
3ec964d6 | 40 | pool.removeAllWorker() |
41 | expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError) | |
7c0ba920 | 42 | pool.destroy() |
3ec964d6 | 43 | }) |
44 | ||
45 | it('Simulate pool creation from a non main thread/process', () => { | |
8d3782fa JB |
46 | expect( |
47 | () => | |
48 | new StubPoolWithIsMainMethod( | |
7c0ba920 | 49 | numberOfWorkers, |
8d3782fa JB |
50 | './tests/worker-files/thread/testWorker.js', |
51 | { | |
52 | errorHandler: e => console.error(e) | |
53 | } | |
54 | ) | |
55 | ).toThrowError(new Error('Cannot start a pool from a worker!')) | |
3ec964d6 | 56 | }) |
c510fea7 APA |
57 | |
58 | it('Verify that filePath is checked', () => { | |
7c0ba920 JB |
59 | expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError( |
60 | new Error('Please specify a file with a worker implementation') | |
8d3782fa | 61 | ) |
7c0ba920 JB |
62 | expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError( |
63 | new Error('Please specify a file with a worker implementation') | |
8d3782fa JB |
64 | ) |
65 | }) | |
66 | ||
67 | it('Verify that numberOfWorkers is checked', () => { | |
68 | expect(() => new FixedThreadPool()).toThrowError( | |
69 | new Error( | |
70 | 'Cannot instantiate a pool without specifying the number of workers' | |
71 | ) | |
72 | ) | |
73 | }) | |
74 | ||
75 | it('Verify that a negative number of workers is checked', () => { | |
76 | expect( | |
77 | () => | |
78 | new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js') | |
79 | ).toThrowError( | |
80 | new Error('Cannot instantiate a pool with a negative number of workers') | |
81 | ) | |
82 | }) | |
83 | ||
84 | it('Verify that a non integer number of workers is checked', () => { | |
85 | expect( | |
86 | () => | |
87 | new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') | |
88 | ).toThrowError( | |
89 | new Error( | |
90 | 'Cannot instantiate a pool with a non integer number of workers' | |
91 | ) | |
92 | ) | |
c510fea7 | 93 | }) |
7c0ba920 JB |
94 | |
95 | it('Verify that pool options are checked', () => { | |
96 | let pool = new FixedThreadPool( | |
97 | numberOfWorkers, | |
98 | './tests/worker-files/thread/testWorker.js' | |
99 | ) | |
100 | expect(pool.opts.enableEvents).toEqual(true) | |
101 | expect(pool.emitter).toBeDefined() | |
102 | pool.destroy() | |
103 | pool = new FixedThreadPool( | |
104 | numberOfWorkers, | |
105 | './tests/worker-files/thread/testWorker.js', | |
106 | { | |
107 | enableEvents: false | |
108 | } | |
109 | ) | |
110 | expect(pool.opts.enableEvents).toEqual(false) | |
111 | expect(pool.emitter).toBeUndefined() | |
112 | pool.destroy() | |
113 | }) | |
114 | ||
115 | it("Verify that pool event emitter 'busy' event can register a callback", () => { | |
116 | const pool = new FixedThreadPool( | |
117 | numberOfWorkers, | |
118 | './tests/worker-files/thread/testWorker.js' | |
119 | ) | |
120 | const promises = [] | |
121 | let poolBusy = 0 | |
122 | pool.emitter.on('busy', () => poolBusy++) | |
123 | for (let i = 0; i < numberOfWorkers * 2; i++) { | |
124 | promises.push(pool.execute({ test: 'test' })) | |
125 | } | |
126 | expect(poolBusy).toEqual(numberOfWorkers) | |
127 | pool.destroy() | |
128 | }) | |
3ec964d6 | 129 | }) |