| 1 | const { expect } = require('expect') |
| 2 | const sinon = require('sinon') |
| 3 | const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib') |
| 4 | const { EMPTY_FUNCTION } = require('../../lib/utils') |
| 5 | |
| 6 | describe('Abstract worker test suite', () => { |
| 7 | class StubWorkerWithMainWorker extends ThreadWorker { |
| 8 | constructor (fn, opts) { |
| 9 | super(fn, opts) |
| 10 | this.mainWorker = undefined |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | it('Verify worker options default values', () => { |
| 15 | const worker = new ThreadWorker(() => {}) |
| 16 | expect(worker.opts.maxInactiveTime).toStrictEqual(60000) |
| 17 | expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT) |
| 18 | expect(worker.opts.killHandler).toStrictEqual(EMPTY_FUNCTION) |
| 19 | expect(worker.opts.async).toBe(undefined) |
| 20 | }) |
| 21 | |
| 22 | it('Verify that worker options are set at worker creation', () => { |
| 23 | const killHandler = () => { |
| 24 | console.info('Worker received kill message') |
| 25 | } |
| 26 | const worker = new ClusterWorker(() => {}, { |
| 27 | maxInactiveTime: 6000, |
| 28 | killBehavior: KillBehaviors.HARD, |
| 29 | killHandler, |
| 30 | async: true |
| 31 | }) |
| 32 | expect(worker.opts.maxInactiveTime).toStrictEqual(6000) |
| 33 | expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD) |
| 34 | expect(worker.opts.killHandler).toStrictEqual(killHandler) |
| 35 | expect(worker.opts.async).toBe(undefined) |
| 36 | }) |
| 37 | |
| 38 | it('Verify that taskFunctions parameter is mandatory', () => { |
| 39 | expect(() => new ClusterWorker()).toThrowError( |
| 40 | 'taskFunctions parameter is mandatory' |
| 41 | ) |
| 42 | }) |
| 43 | |
| 44 | it('Verify that taskFunctions parameter is a function or a plain object', () => { |
| 45 | expect(() => new ClusterWorker(0)).toThrowError( |
| 46 | new TypeError( |
| 47 | 'taskFunctions parameter is not a function or a plain object' |
| 48 | ) |
| 49 | ) |
| 50 | expect(() => new ClusterWorker('')).toThrowError( |
| 51 | new TypeError( |
| 52 | 'taskFunctions parameter is not a function or a plain object' |
| 53 | ) |
| 54 | ) |
| 55 | expect(() => new ClusterWorker(true)).toThrowError( |
| 56 | new TypeError( |
| 57 | 'taskFunctions parameter is not a function or a plain object' |
| 58 | ) |
| 59 | ) |
| 60 | expect(() => new ClusterWorker([])).toThrowError( |
| 61 | new TypeError( |
| 62 | 'taskFunctions parameter is not a function or a plain object' |
| 63 | ) |
| 64 | ) |
| 65 | expect(() => new ClusterWorker(new Map())).toThrowError( |
| 66 | new TypeError( |
| 67 | 'taskFunctions parameter is not a function or a plain object' |
| 68 | ) |
| 69 | ) |
| 70 | expect(() => new ClusterWorker(new Set())).toThrowError( |
| 71 | new TypeError( |
| 72 | 'taskFunctions parameter is not a function or a plain object' |
| 73 | ) |
| 74 | ) |
| 75 | expect(() => new ClusterWorker(new WeakMap())).toThrowError( |
| 76 | new TypeError( |
| 77 | 'taskFunctions parameter is not a function or a plain object' |
| 78 | ) |
| 79 | ) |
| 80 | expect(() => new ClusterWorker(new WeakSet())).toThrowError( |
| 81 | new TypeError( |
| 82 | 'taskFunctions parameter is not a function or a plain object' |
| 83 | ) |
| 84 | ) |
| 85 | }) |
| 86 | |
| 87 | it('Verify that taskFunctions parameter is not an empty object', () => { |
| 88 | expect(() => new ClusterWorker({})).toThrowError( |
| 89 | new Error('taskFunctions parameter object is empty') |
| 90 | ) |
| 91 | }) |
| 92 | |
| 93 | it('Verify that taskFunctions parameter with unique function is taken', () => { |
| 94 | const worker = new ThreadWorker(() => {}) |
| 95 | expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) |
| 96 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 97 | expect(worker.taskFunctions.size).toBe(2) |
| 98 | expect(worker.taskFunctions.get('default')).toStrictEqual( |
| 99 | worker.taskFunctions.get('fn1') |
| 100 | ) |
| 101 | }) |
| 102 | |
| 103 | it('Verify that taskFunctions parameter with multiple task functions contains function', () => { |
| 104 | const fn1 = () => { |
| 105 | return 1 |
| 106 | } |
| 107 | const fn2 = '' |
| 108 | expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError( |
| 109 | new TypeError('A taskFunctions parameter object value is not a function') |
| 110 | ) |
| 111 | }) |
| 112 | |
| 113 | it('Verify that taskFunctions parameter with multiple task functions is taken', () => { |
| 114 | const fn1 = () => { |
| 115 | return 1 |
| 116 | } |
| 117 | const fn2 = () => { |
| 118 | return 2 |
| 119 | } |
| 120 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 121 | expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) |
| 122 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 123 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 124 | expect(worker.taskFunctions.size).toBe(3) |
| 125 | expect(worker.taskFunctions.get('default')).toStrictEqual( |
| 126 | worker.taskFunctions.get('fn1') |
| 127 | ) |
| 128 | }) |
| 129 | |
| 130 | it('Verify that sync kill handler is called when worker is killed', () => { |
| 131 | const worker = new ClusterWorker(() => {}, { |
| 132 | killHandler: sinon.stub().returns() |
| 133 | }) |
| 134 | worker.isMain = false |
| 135 | worker.handleKillMessage() |
| 136 | expect(worker.opts.killHandler.calledOnce).toBe(true) |
| 137 | }) |
| 138 | |
| 139 | // it('Verify that async kill handler is called when worker is killed', () => { |
| 140 | // const worker = new ClusterWorker(() => {}, { |
| 141 | // killHandler: sinon.stub().resolves() |
| 142 | // }) |
| 143 | // worker.isMain = false |
| 144 | // worker.handleKillMessage() |
| 145 | // expect(worker.opts.killHandler.calledOnce).toBe(true) |
| 146 | // }) |
| 147 | |
| 148 | it('Verify that handleError() method works properly', () => { |
| 149 | const error = new Error('Error as an error') |
| 150 | const worker = new ClusterWorker(() => {}) |
| 151 | expect(worker.handleError(error)).not.toBeInstanceOf(Error) |
| 152 | expect(worker.handleError(error)).toStrictEqual(error.message) |
| 153 | const errorMessage = 'Error as a string' |
| 154 | expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage) |
| 155 | }) |
| 156 | |
| 157 | it('Verify that getMainWorker() throw error if main worker is not set', () => { |
| 158 | expect(() => |
| 159 | new StubWorkerWithMainWorker(() => {}).getMainWorker() |
| 160 | ).toThrowError('Main worker not set') |
| 161 | }) |
| 162 | |
| 163 | it('Verify that hasTaskFunction() works', () => { |
| 164 | const fn1 = () => { |
| 165 | return 1 |
| 166 | } |
| 167 | const fn2 = () => { |
| 168 | return 2 |
| 169 | } |
| 170 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 171 | expect(worker.hasTaskFunction('default')).toBe(true) |
| 172 | expect(worker.hasTaskFunction('fn1')).toBe(true) |
| 173 | expect(worker.hasTaskFunction('fn2')).toBe(true) |
| 174 | expect(worker.hasTaskFunction('fn3')).toBe(false) |
| 175 | }) |
| 176 | |
| 177 | it('Verify that addTaskFunction() works', () => { |
| 178 | const fn1 = () => { |
| 179 | return 1 |
| 180 | } |
| 181 | const fn2 = () => { |
| 182 | return 2 |
| 183 | } |
| 184 | const fn1Replacement = () => { |
| 185 | return 3 |
| 186 | } |
| 187 | const worker = new ThreadWorker(fn1) |
| 188 | expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) |
| 189 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 190 | expect(worker.taskFunctions.size).toBe(2) |
| 191 | expect(worker.taskFunctions.get('default')).toStrictEqual( |
| 192 | worker.taskFunctions.get('fn1') |
| 193 | ) |
| 194 | expect(() => worker.addTaskFunction('default', fn2)).toThrowError( |
| 195 | new Error('Cannot add a task function with the default reserved name') |
| 196 | ) |
| 197 | worker.addTaskFunction('fn2', fn2) |
| 198 | expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) |
| 199 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 200 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 201 | expect(worker.taskFunctions.size).toBe(3) |
| 202 | expect(worker.taskFunctions.get('default')).toStrictEqual( |
| 203 | worker.taskFunctions.get('fn1') |
| 204 | ) |
| 205 | worker.addTaskFunction('fn1', fn1Replacement) |
| 206 | expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) |
| 207 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 208 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 209 | expect(worker.taskFunctions.size).toBe(3) |
| 210 | expect(worker.taskFunctions.get('default')).toStrictEqual( |
| 211 | worker.taskFunctions.get('fn1') |
| 212 | ) |
| 213 | }) |
| 214 | |
| 215 | it('Verify that removeTaskFunction() works', () => { |
| 216 | const fn1 = () => { |
| 217 | return 1 |
| 218 | } |
| 219 | const fn2 = () => { |
| 220 | return 2 |
| 221 | } |
| 222 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 223 | expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) |
| 224 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 225 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 226 | expect(worker.taskFunctions.size).toBe(3) |
| 227 | expect(worker.taskFunctions.get('default')).toStrictEqual( |
| 228 | worker.taskFunctions.get('fn1') |
| 229 | ) |
| 230 | expect(() => worker.removeTaskFunction('default')).toThrowError( |
| 231 | new Error( |
| 232 | 'Cannot remove the task function with the default reserved name' |
| 233 | ) |
| 234 | ) |
| 235 | expect(() => worker.removeTaskFunction('fn1')).toThrowError( |
| 236 | new Error( |
| 237 | 'Cannot remove the task function used as the default task function' |
| 238 | ) |
| 239 | ) |
| 240 | worker.removeTaskFunction('fn2') |
| 241 | expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) |
| 242 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 243 | expect(worker.taskFunctions.get('fn2')).toBeUndefined() |
| 244 | expect(worker.taskFunctions.size).toBe(2) |
| 245 | }) |
| 246 | |
| 247 | it('Verify that listTaskFunctions() works', () => { |
| 248 | const fn1 = () => { |
| 249 | return 1 |
| 250 | } |
| 251 | const fn2 = () => { |
| 252 | return 2 |
| 253 | } |
| 254 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 255 | expect(worker.listTaskFunctions()).toStrictEqual(['default', 'fn1', 'fn2']) |
| 256 | }) |
| 257 | |
| 258 | it('Verify that setDefaultTaskFunction() works', () => { |
| 259 | const fn1 = () => { |
| 260 | return 1 |
| 261 | } |
| 262 | const fn2 = () => { |
| 263 | return 2 |
| 264 | } |
| 265 | const worker = new ThreadWorker({ fn1, fn2 }) |
| 266 | expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function) |
| 267 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
| 268 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) |
| 269 | expect(worker.taskFunctions.size).toBe(3) |
| 270 | expect(worker.taskFunctions.get('default')).toStrictEqual( |
| 271 | worker.taskFunctions.get('fn1') |
| 272 | ) |
| 273 | expect(() => worker.setDefaultTaskFunction('default')).toThrowError( |
| 274 | new Error( |
| 275 | 'Cannot set the default task function reserved name as the default task function' |
| 276 | ) |
| 277 | ) |
| 278 | worker.setDefaultTaskFunction('fn1') |
| 279 | expect(worker.taskFunctions.get('default')).toStrictEqual( |
| 280 | worker.taskFunctions.get('fn1') |
| 281 | ) |
| 282 | worker.setDefaultTaskFunction('fn2') |
| 283 | expect(worker.taskFunctions.get('default')).toStrictEqual( |
| 284 | worker.taskFunctions.get('fn2') |
| 285 | ) |
| 286 | }) |
| 287 | }) |