| 1 | import { expect } from 'expect' |
| 2 | import { restore, stub } from 'sinon' |
| 3 | |
| 4 | import { |
| 5 | ClusterWorker, |
| 6 | KillBehaviors, |
| 7 | ThreadWorker, |
| 8 | WorkerChoiceStrategies |
| 9 | } from '../../lib/index.cjs' |
| 10 | import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.cjs' |
| 11 | |
| 12 | describe('Abstract worker test suite', () => { |
| 13 | class StubWorkerWithMainWorker extends ThreadWorker { |
| 14 | constructor (fn, opts) { |
| 15 | super(fn, opts) |
| 16 | delete this.mainWorker |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | afterEach(() => { |
| 21 | restore() |
| 22 | }) |
| 23 | |
| 24 | it('Verify worker options default values', () => { |
| 25 | const worker = new ThreadWorker(() => {}) |
| 26 | expect(worker.opts).toStrictEqual({ |
| 27 | killBehavior: KillBehaviors.SOFT, |
| 28 | maxInactiveTime: 60000, |
| 29 | killHandler: EMPTY_FUNCTION |
| 30 | }) |
| 31 | }) |
| 32 | |
| 33 | it('Verify that worker options are checked at worker creation', () => { |
| 34 | expect(() => new ClusterWorker(() => {}, '')).toThrow( |
| 35 | new TypeError('opts worker options parameter is not a plain object') |
| 36 | ) |
| 37 | expect(() => new ClusterWorker(() => {}, { killBehavior: '' })).toThrow( |
| 38 | new TypeError("killBehavior option '' is not valid") |
| 39 | ) |
| 40 | expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrow( |
| 41 | new TypeError("killBehavior option '0' is not valid") |
| 42 | ) |
| 43 | expect(() => new ThreadWorker(() => {}, { maxInactiveTime: '' })).toThrow( |
| 44 | new TypeError('maxInactiveTime option is not an integer') |
| 45 | ) |
| 46 | expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0.5 })).toThrow( |
| 47 | new TypeError('maxInactiveTime option is not an integer') |
| 48 | ) |
| 49 | expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0 })).toThrow( |
| 50 | new TypeError( |
| 51 | 'maxInactiveTime option is not a positive integer greater or equal than 5' |
| 52 | ) |
| 53 | ) |
| 54 | expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 4 })).toThrow( |
| 55 | new TypeError( |
| 56 | 'maxInactiveTime option is not a positive integer greater or equal than 5' |
| 57 | ) |
| 58 | ) |
| 59 | expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrow( |
| 60 | new TypeError('killHandler option is not a function') |
| 61 | ) |
| 62 | expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrow( |
| 63 | new TypeError('killHandler option is not a function') |
| 64 | ) |
| 65 | }) |
| 66 | |
| 67 | it('Verify that worker options are set at worker creation', () => { |
| 68 | const killHandler = () => { |
| 69 | console.info('Worker received kill message') |
| 70 | } |
| 71 | const worker = new ClusterWorker(() => {}, { |
| 72 | killBehavior: KillBehaviors.HARD, |
| 73 | maxInactiveTime: 6000, |
| 74 | killHandler |
| 75 | }) |
| 76 | expect(worker.opts).toStrictEqual({ |
| 77 | killBehavior: KillBehaviors.HARD, |
| 78 | maxInactiveTime: 6000, |
| 79 | killHandler |
| 80 | }) |
| 81 | }) |
| 82 | |
| 83 | it('Verify that taskFunctions parameter is mandatory', () => { |
| 84 | expect(() => new ClusterWorker()).toThrow( |
| 85 | new Error('taskFunctions parameter is mandatory') |
| 86 | ) |
| 87 | }) |
| 88 | |
| 89 | it('Verify that taskFunctions parameter is a function or a plain object', () => { |
| 90 | expect(() => new ClusterWorker(0)).toThrow( |
| 91 | new TypeError( |
| 92 | 'taskFunctions parameter is not a function or a plain object' |
| 93 | ) |
| 94 | ) |
| 95 | expect(() => new ClusterWorker('')).toThrow( |
| 96 | new TypeError( |
| 97 | 'taskFunctions parameter is not a function or a plain object' |
| 98 | ) |
| 99 | ) |
| 100 | expect(() => new ClusterWorker(true)).toThrow( |
| 101 | new TypeError( |
| 102 | 'taskFunctions parameter is not a function or a plain object' |
| 103 | ) |
| 104 | ) |
| 105 | expect(() => new ClusterWorker([])).toThrow( |
| 106 | new TypeError( |
| 107 | 'taskFunctions parameter is not a function or a plain object' |
| 108 | ) |
| 109 | ) |
| 110 | expect(() => new ClusterWorker(new Map())).toThrow( |
| 111 | new TypeError( |
| 112 | 'taskFunctions parameter is not a function or a plain object' |
| 113 | ) |
| 114 | ) |
| 115 | expect(() => new ClusterWorker(new Set())).toThrow( |
| 116 | new TypeError( |
| 117 | 'taskFunctions parameter is not a function or a plain object' |
| 118 | ) |
| 119 | ) |
| 120 | expect(() => new ClusterWorker(new WeakMap())).toThrow( |
| 121 | new TypeError( |
| 122 | 'taskFunctions parameter is not a function or a plain object' |
| 123 | ) |
| 124 | ) |
| 125 | expect(() => new ClusterWorker(new WeakSet())).toThrow( |
| 126 | new TypeError( |
| 127 | 'taskFunctions parameter is not a function or a plain object' |
| 128 | ) |
| 129 | ) |
| 130 | }) |
| 131 | |
| 132 | it('Verify that taskFunctions parameter is not an empty object', () => { |
| 133 | expect(() => new ClusterWorker({})).toThrow( |
| 134 | new Error('taskFunctions parameter object is empty') |
| 135 | ) |
| 136 | }) |
| 137 | |
| 138 | it('Verify that taskFunctions parameter with unique function is taken', () => { |
| 139 | const worker = new ThreadWorker(() => {}) |
| 140 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object) |
| 141 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object) |
| 142 | expect(worker.taskFunctions.size).toBe(2) |
| 143 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 144 | worker.taskFunctions.get('fn1') |
| 145 | ) |
| 146 | }) |
| 147 | |
| 148 | it('Verify that taskFunctions parameter with multiple task functions is checked', () => { |
| 149 | const fn1 = () => { |
| 150 | return 1 |
| 151 | } |
| 152 | const fn2 = '' |
| 153 | expect(() => new ThreadWorker({ '': fn1 })).toThrow( |
| 154 | new TypeError('A taskFunctions parameter object key is an empty string') |
| 155 | ) |
| 156 | expect(() => new ThreadWorker({ fn1, fn2 })).toThrow( |
| 157 | new TypeError( |
| 158 | "taskFunction object 'taskFunction' property 'undefined' is not a function" |
| 159 | ) |
| 160 | ) |
| 161 | expect(() => new ThreadWorker({ fn1: { fn1 } })).toThrow( |
| 162 | new TypeError( |
| 163 | "taskFunction object 'taskFunction' property 'undefined' is not a function" |
| 164 | ) |
| 165 | ) |
| 166 | expect(() => new ThreadWorker({ fn2: { taskFunction: fn2 } })).toThrow( |
| 167 | new TypeError( |
| 168 | "taskFunction object 'taskFunction' property '' is not a function" |
| 169 | ) |
| 170 | ) |
| 171 | expect( |
| 172 | () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: '' } }) |
| 173 | ).toThrow(new TypeError("Invalid priority ''")) |
| 174 | expect( |
| 175 | () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: -21 } }) |
| 176 | ).toThrow(new TypeError('Property priority must be between -20 and 19')) |
| 177 | expect( |
| 178 | () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: 20 } }) |
| 179 | ).toThrow(new RangeError('Property priority must be between -20 and 19')) |
| 180 | expect( |
| 181 | () => |
| 182 | new ThreadWorker({ |
| 183 | fn1: { taskFunction: fn1, strategy: 'invalidStrategy' } |
| 184 | }) |
| 185 | ).toThrow( |
| 186 | new RangeError("Invalid worker choice strategy 'invalidStrategy'") |
| 187 | ) |
| 188 | }) |
| 189 | |
| 190 | it('Verify that taskFunctions parameter with multiple task functions is taken', () => { |
| 191 | const fn1 = () => { |
| 192 | return 1 |
| 193 | } |
| 194 | const fn2 = () => { |
| 195 | return 2 |
| 196 | } |
| 197 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 198 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object) |
| 199 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object) |
| 200 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object) |
| 201 | expect(worker.taskFunctions.size).toBe(3) |
| 202 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 203 | worker.taskFunctions.get('fn1') |
| 204 | ) |
| 205 | }) |
| 206 | |
| 207 | it('Verify that taskFunctions parameter with multiple task functions object is taken', () => { |
| 208 | const fn1Obj = { |
| 209 | taskFunction: () => { |
| 210 | return 1 |
| 211 | }, |
| 212 | priority: 5 |
| 213 | } |
| 214 | const fn2Obj = { |
| 215 | taskFunction: () => { |
| 216 | return 2 |
| 217 | }, |
| 218 | priority: 6, |
| 219 | strategy: WorkerChoiceStrategies.LESS_BUSY |
| 220 | } |
| 221 | const worker = new ThreadWorker({ |
| 222 | fn1: fn1Obj, |
| 223 | fn2: fn2Obj |
| 224 | }) |
| 225 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(fn1Obj) |
| 226 | expect(worker.taskFunctions.get('fn1')).toStrictEqual(fn1Obj) |
| 227 | expect(worker.taskFunctions.get('fn2')).toStrictEqual(fn2Obj) |
| 228 | expect(worker.taskFunctions.size).toBe(3) |
| 229 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 230 | worker.taskFunctions.get('fn1') |
| 231 | ) |
| 232 | }) |
| 233 | |
| 234 | it('Verify that async kill handler is called when worker is killed', () => { |
| 235 | const killHandlerStub = stub().returns() |
| 236 | const worker = new ClusterWorker(() => {}, { |
| 237 | killHandler: async () => await Promise.resolve(killHandlerStub()) |
| 238 | }) |
| 239 | worker.isMain = false |
| 240 | worker.handleKillMessage() |
| 241 | expect(killHandlerStub.calledOnce).toBe(true) |
| 242 | }) |
| 243 | |
| 244 | it('Verify that getMainWorker() throw error if main worker is not set', () => { |
| 245 | expect(() => |
| 246 | new StubWorkerWithMainWorker(() => {}).getMainWorker() |
| 247 | ).toThrow('Main worker not set') |
| 248 | }) |
| 249 | |
| 250 | it('Verify that hasTaskFunction() is working', () => { |
| 251 | const fn1 = () => { |
| 252 | return 1 |
| 253 | } |
| 254 | const fn2 = () => { |
| 255 | return 2 |
| 256 | } |
| 257 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 258 | expect(worker.hasTaskFunction(0)).toStrictEqual({ |
| 259 | status: false, |
| 260 | error: new TypeError('name parameter is not a string') |
| 261 | }) |
| 262 | expect(worker.hasTaskFunction('')).toStrictEqual({ |
| 263 | status: false, |
| 264 | error: new TypeError('name parameter is an empty string') |
| 265 | }) |
| 266 | expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({ |
| 267 | status: true |
| 268 | }) |
| 269 | expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true }) |
| 270 | expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true }) |
| 271 | expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false }) |
| 272 | }) |
| 273 | |
| 274 | it('Verify that addTaskFunction() is working', () => { |
| 275 | const fn1 = () => { |
| 276 | return 1 |
| 277 | } |
| 278 | const fn2 = () => { |
| 279 | return 2 |
| 280 | } |
| 281 | const fn1Replacement = () => { |
| 282 | return 3 |
| 283 | } |
| 284 | const worker = new ThreadWorker(fn1) |
| 285 | expect(worker.addTaskFunction(0, fn1)).toStrictEqual({ |
| 286 | status: false, |
| 287 | error: new TypeError('name parameter is not a string') |
| 288 | }) |
| 289 | expect(worker.addTaskFunction('', fn1)).toStrictEqual({ |
| 290 | status: false, |
| 291 | error: new TypeError('name parameter is an empty string') |
| 292 | }) |
| 293 | expect(worker.addTaskFunction('fn3', '')).toStrictEqual({ |
| 294 | status: false, |
| 295 | error: new TypeError( |
| 296 | "taskFunction object 'taskFunction' property 'undefined' is not a function" |
| 297 | ) |
| 298 | }) |
| 299 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object) |
| 300 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object) |
| 301 | expect(worker.taskFunctions.size).toBe(2) |
| 302 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 303 | worker.taskFunctions.get('fn1') |
| 304 | ) |
| 305 | expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({ |
| 306 | status: false, |
| 307 | error: new Error( |
| 308 | 'Cannot add a task function with the default reserved name' |
| 309 | ) |
| 310 | }) |
| 311 | worker.addTaskFunction('fn2', fn2) |
| 312 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object) |
| 313 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object) |
| 314 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object) |
| 315 | expect(worker.taskFunctions.size).toBe(3) |
| 316 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 317 | worker.taskFunctions.get('fn1') |
| 318 | ) |
| 319 | worker.addTaskFunction('fn1', fn1Replacement) |
| 320 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object) |
| 321 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object) |
| 322 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object) |
| 323 | expect(worker.taskFunctions.size).toBe(3) |
| 324 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 325 | worker.taskFunctions.get('fn1') |
| 326 | ) |
| 327 | }) |
| 328 | |
| 329 | it('Verify that listTaskFunctionNames() is working', () => { |
| 330 | const fn1 = () => { |
| 331 | return 1 |
| 332 | } |
| 333 | const fn2 = () => { |
| 334 | return 2 |
| 335 | } |
| 336 | const worker = new ClusterWorker({ fn1, fn2 }) |
| 337 | expect(worker.listTaskFunctionsProperties()).toStrictEqual([ |
| 338 | { name: DEFAULT_TASK_NAME }, |
| 339 | { name: 'fn1' }, |
| 340 | { name: 'fn2' } |
| 341 | ]) |
| 342 | }) |
| 343 | |
| 344 | it('Verify that setDefaultTaskFunction() is working', () => { |
| 345 | const fn1 = () => { |
| 346 | return 1 |
| 347 | } |
| 348 | const fn2 = () => { |
| 349 | return 2 |
| 350 | } |
| 351 | const worker = new ThreadWorker({ fn1, fn2 }) |
| 352 | expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({ |
| 353 | status: false, |
| 354 | error: new TypeError('name parameter is not a string') |
| 355 | }) |
| 356 | expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({ |
| 357 | status: false, |
| 358 | error: new TypeError('name parameter is an empty string') |
| 359 | }) |
| 360 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object) |
| 361 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object) |
| 362 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object) |
| 363 | expect(worker.taskFunctions.size).toBe(3) |
| 364 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 365 | worker.taskFunctions.get('fn1') |
| 366 | ) |
| 367 | expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({ |
| 368 | status: false, |
| 369 | error: new Error( |
| 370 | 'Cannot set the default task function reserved name as the default task function' |
| 371 | ) |
| 372 | }) |
| 373 | expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({ |
| 374 | status: false, |
| 375 | error: new Error( |
| 376 | 'Cannot set the default task function to a non-existing task function' |
| 377 | ) |
| 378 | }) |
| 379 | worker.setDefaultTaskFunction('fn1') |
| 380 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 381 | worker.taskFunctions.get('fn1') |
| 382 | ) |
| 383 | worker.setDefaultTaskFunction('fn2') |
| 384 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
| 385 | worker.taskFunctions.get('fn2') |
| 386 | ) |
| 387 | }) |
| 388 | }) |