| 1 | const { expect } = require('expect') |
| 2 | const sinon = require('sinon') |
| 3 | const { |
| 4 | DynamicClusterPool, |
| 5 | DynamicThreadPool, |
| 6 | FixedClusterPool, |
| 7 | FixedThreadPool, |
| 8 | PoolEvents, |
| 9 | PoolTypes, |
| 10 | WorkerChoiceStrategies, |
| 11 | WorkerTypes |
| 12 | } = require('../../../lib') |
| 13 | const { CircularArray } = require('../../../lib/circular-array') |
| 14 | const { Deque } = require('../../../lib/deque') |
| 15 | const { DEFAULT_TASK_NAME } = require('../../../lib/utils') |
| 16 | const { version } = require('../../../package.json') |
| 17 | const { waitPoolEvents } = require('../../test-utils') |
| 18 | |
| 19 | describe('Abstract pool test suite', () => { |
| 20 | const numberOfWorkers = 2 |
| 21 | class StubPoolWithIsMain extends FixedThreadPool { |
| 22 | isMain () { |
| 23 | return false |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | afterEach(() => { |
| 28 | sinon.restore() |
| 29 | }) |
| 30 | |
| 31 | it('Simulate pool creation from a non main thread/process', () => { |
| 32 | expect( |
| 33 | () => |
| 34 | new StubPoolWithIsMain( |
| 35 | numberOfWorkers, |
| 36 | './tests/worker-files/thread/testWorker.js', |
| 37 | { |
| 38 | errorHandler: (e) => console.error(e) |
| 39 | } |
| 40 | ) |
| 41 | ).toThrowError( |
| 42 | new Error( |
| 43 | 'Cannot start a pool from a worker with the same type as the pool' |
| 44 | ) |
| 45 | ) |
| 46 | }) |
| 47 | |
| 48 | it('Verify that pool statuses properties are set', async () => { |
| 49 | const pool = new FixedThreadPool( |
| 50 | numberOfWorkers, |
| 51 | './tests/worker-files/thread/testWorker.js' |
| 52 | ) |
| 53 | expect(pool.starting).toBe(false) |
| 54 | expect(pool.started).toBe(true) |
| 55 | await pool.destroy() |
| 56 | expect(pool.started).toBe(false) |
| 57 | }) |
| 58 | |
| 59 | it('Verify that filePath is checked', () => { |
| 60 | const expectedError = new Error( |
| 61 | 'Please specify a file with a worker implementation' |
| 62 | ) |
| 63 | expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError( |
| 64 | expectedError |
| 65 | ) |
| 66 | expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError( |
| 67 | expectedError |
| 68 | ) |
| 69 | expect(() => new FixedThreadPool(numberOfWorkers, 0)).toThrowError( |
| 70 | expectedError |
| 71 | ) |
| 72 | expect(() => new FixedThreadPool(numberOfWorkers, true)).toThrowError( |
| 73 | expectedError |
| 74 | ) |
| 75 | expect( |
| 76 | () => new FixedThreadPool(numberOfWorkers, './dummyWorker.ts') |
| 77 | ).toThrowError(new Error("Cannot find the worker file './dummyWorker.ts'")) |
| 78 | }) |
| 79 | |
| 80 | it('Verify that numberOfWorkers is checked', () => { |
| 81 | expect(() => new FixedThreadPool()).toThrowError( |
| 82 | new Error( |
| 83 | 'Cannot instantiate a pool without specifying the number of workers' |
| 84 | ) |
| 85 | ) |
| 86 | }) |
| 87 | |
| 88 | it('Verify that a negative number of workers is checked', () => { |
| 89 | expect( |
| 90 | () => |
| 91 | new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js') |
| 92 | ).toThrowError( |
| 93 | new RangeError( |
| 94 | 'Cannot instantiate a pool with a negative number of workers' |
| 95 | ) |
| 96 | ) |
| 97 | }) |
| 98 | |
| 99 | it('Verify that a non integer number of workers is checked', () => { |
| 100 | expect( |
| 101 | () => |
| 102 | new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') |
| 103 | ).toThrowError( |
| 104 | new TypeError( |
| 105 | 'Cannot instantiate a pool with a non safe integer number of workers' |
| 106 | ) |
| 107 | ) |
| 108 | }) |
| 109 | |
| 110 | it('Verify that dynamic pool sizing is checked', () => { |
| 111 | expect( |
| 112 | () => |
| 113 | new DynamicClusterPool( |
| 114 | 1, |
| 115 | undefined, |
| 116 | './tests/worker-files/cluster/testWorker.js' |
| 117 | ) |
| 118 | ).toThrowError( |
| 119 | new TypeError( |
| 120 | 'Cannot instantiate a dynamic pool without specifying the maximum pool size' |
| 121 | ) |
| 122 | ) |
| 123 | expect( |
| 124 | () => |
| 125 | new DynamicThreadPool( |
| 126 | 0.5, |
| 127 | 1, |
| 128 | './tests/worker-files/thread/testWorker.js' |
| 129 | ) |
| 130 | ).toThrowError( |
| 131 | new TypeError( |
| 132 | 'Cannot instantiate a pool with a non safe integer number of workers' |
| 133 | ) |
| 134 | ) |
| 135 | expect( |
| 136 | () => |
| 137 | new DynamicClusterPool( |
| 138 | 0, |
| 139 | 0.5, |
| 140 | './tests/worker-files/cluster/testWorker.js' |
| 141 | ) |
| 142 | ).toThrowError( |
| 143 | new TypeError( |
| 144 | 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size' |
| 145 | ) |
| 146 | ) |
| 147 | expect( |
| 148 | () => |
| 149 | new DynamicThreadPool(2, 1, './tests/worker-files/thread/testWorker.js') |
| 150 | ).toThrowError( |
| 151 | new RangeError( |
| 152 | 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size' |
| 153 | ) |
| 154 | ) |
| 155 | expect( |
| 156 | () => |
| 157 | new DynamicClusterPool( |
| 158 | 1, |
| 159 | 1, |
| 160 | './tests/worker-files/cluster/testWorker.js' |
| 161 | ) |
| 162 | ).toThrowError( |
| 163 | new RangeError( |
| 164 | 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead' |
| 165 | ) |
| 166 | ) |
| 167 | expect( |
| 168 | () => |
| 169 | new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js') |
| 170 | ).toThrowError( |
| 171 | new RangeError( |
| 172 | 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero' |
| 173 | ) |
| 174 | ) |
| 175 | }) |
| 176 | |
| 177 | it('Verify that pool options are checked', async () => { |
| 178 | let pool = new FixedThreadPool( |
| 179 | numberOfWorkers, |
| 180 | './tests/worker-files/thread/testWorker.js' |
| 181 | ) |
| 182 | expect(pool.emitter).toBeDefined() |
| 183 | expect(pool.opts.enableEvents).toBe(true) |
| 184 | expect(pool.opts.restartWorkerOnError).toBe(true) |
| 185 | expect(pool.opts.enableTasksQueue).toBe(false) |
| 186 | expect(pool.opts.tasksQueueOptions).toBeUndefined() |
| 187 | expect(pool.opts.workerChoiceStrategy).toBe( |
| 188 | WorkerChoiceStrategies.ROUND_ROBIN |
| 189 | ) |
| 190 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
| 191 | choiceRetries: 6, |
| 192 | runTime: { median: false }, |
| 193 | waitTime: { median: false }, |
| 194 | elu: { median: false } |
| 195 | }) |
| 196 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ |
| 197 | choiceRetries: 6, |
| 198 | runTime: { median: false }, |
| 199 | waitTime: { median: false }, |
| 200 | elu: { median: false } |
| 201 | }) |
| 202 | expect(pool.opts.messageHandler).toBeUndefined() |
| 203 | expect(pool.opts.errorHandler).toBeUndefined() |
| 204 | expect(pool.opts.onlineHandler).toBeUndefined() |
| 205 | expect(pool.opts.exitHandler).toBeUndefined() |
| 206 | await pool.destroy() |
| 207 | const testHandler = () => console.info('test handler executed') |
| 208 | pool = new FixedThreadPool( |
| 209 | numberOfWorkers, |
| 210 | './tests/worker-files/thread/testWorker.js', |
| 211 | { |
| 212 | workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED, |
| 213 | workerChoiceStrategyOptions: { |
| 214 | runTime: { median: true }, |
| 215 | weights: { 0: 300, 1: 200 } |
| 216 | }, |
| 217 | enableEvents: false, |
| 218 | restartWorkerOnError: false, |
| 219 | enableTasksQueue: true, |
| 220 | tasksQueueOptions: { concurrency: 2 }, |
| 221 | messageHandler: testHandler, |
| 222 | errorHandler: testHandler, |
| 223 | onlineHandler: testHandler, |
| 224 | exitHandler: testHandler |
| 225 | } |
| 226 | ) |
| 227 | expect(pool.emitter).toBeUndefined() |
| 228 | expect(pool.opts.enableEvents).toBe(false) |
| 229 | expect(pool.opts.restartWorkerOnError).toBe(false) |
| 230 | expect(pool.opts.enableTasksQueue).toBe(true) |
| 231 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
| 232 | concurrency: 2, |
| 233 | size: 4 |
| 234 | }) |
| 235 | expect(pool.opts.workerChoiceStrategy).toBe( |
| 236 | WorkerChoiceStrategies.LEAST_USED |
| 237 | ) |
| 238 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
| 239 | choiceRetries: 6, |
| 240 | runTime: { median: true }, |
| 241 | waitTime: { median: false }, |
| 242 | elu: { median: false }, |
| 243 | weights: { 0: 300, 1: 200 } |
| 244 | }) |
| 245 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ |
| 246 | choiceRetries: 6, |
| 247 | runTime: { median: true }, |
| 248 | waitTime: { median: false }, |
| 249 | elu: { median: false }, |
| 250 | weights: { 0: 300, 1: 200 } |
| 251 | }) |
| 252 | expect(pool.opts.messageHandler).toStrictEqual(testHandler) |
| 253 | expect(pool.opts.errorHandler).toStrictEqual(testHandler) |
| 254 | expect(pool.opts.onlineHandler).toStrictEqual(testHandler) |
| 255 | expect(pool.opts.exitHandler).toStrictEqual(testHandler) |
| 256 | await pool.destroy() |
| 257 | }) |
| 258 | |
| 259 | it('Verify that pool options are validated', async () => { |
| 260 | expect( |
| 261 | () => |
| 262 | new FixedThreadPool( |
| 263 | numberOfWorkers, |
| 264 | './tests/worker-files/thread/testWorker.js', |
| 265 | { |
| 266 | workerChoiceStrategy: 'invalidStrategy' |
| 267 | } |
| 268 | ) |
| 269 | ).toThrowError( |
| 270 | new Error("Invalid worker choice strategy 'invalidStrategy'") |
| 271 | ) |
| 272 | expect( |
| 273 | () => |
| 274 | new FixedThreadPool( |
| 275 | numberOfWorkers, |
| 276 | './tests/worker-files/thread/testWorker.js', |
| 277 | { |
| 278 | workerChoiceStrategyOptions: { |
| 279 | choiceRetries: 'invalidChoiceRetries' |
| 280 | } |
| 281 | } |
| 282 | ) |
| 283 | ).toThrowError( |
| 284 | new TypeError( |
| 285 | 'Invalid worker choice strategy options: choice retries must be an integer' |
| 286 | ) |
| 287 | ) |
| 288 | expect( |
| 289 | () => |
| 290 | new FixedThreadPool( |
| 291 | numberOfWorkers, |
| 292 | './tests/worker-files/thread/testWorker.js', |
| 293 | { |
| 294 | workerChoiceStrategyOptions: { |
| 295 | choiceRetries: -1 |
| 296 | } |
| 297 | } |
| 298 | ) |
| 299 | ).toThrowError( |
| 300 | new RangeError( |
| 301 | "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero" |
| 302 | ) |
| 303 | ) |
| 304 | expect( |
| 305 | () => |
| 306 | new FixedThreadPool( |
| 307 | numberOfWorkers, |
| 308 | './tests/worker-files/thread/testWorker.js', |
| 309 | { |
| 310 | workerChoiceStrategyOptions: { weights: {} } |
| 311 | } |
| 312 | ) |
| 313 | ).toThrowError( |
| 314 | new Error( |
| 315 | 'Invalid worker choice strategy options: must have a weight for each worker node' |
| 316 | ) |
| 317 | ) |
| 318 | expect( |
| 319 | () => |
| 320 | new FixedThreadPool( |
| 321 | numberOfWorkers, |
| 322 | './tests/worker-files/thread/testWorker.js', |
| 323 | { |
| 324 | workerChoiceStrategyOptions: { measurement: 'invalidMeasurement' } |
| 325 | } |
| 326 | ) |
| 327 | ).toThrowError( |
| 328 | new Error( |
| 329 | "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" |
| 330 | ) |
| 331 | ) |
| 332 | expect( |
| 333 | () => |
| 334 | new FixedThreadPool( |
| 335 | numberOfWorkers, |
| 336 | './tests/worker-files/thread/testWorker.js', |
| 337 | { |
| 338 | enableTasksQueue: true, |
| 339 | tasksQueueOptions: 'invalidTasksQueueOptions' |
| 340 | } |
| 341 | ) |
| 342 | ).toThrowError( |
| 343 | new TypeError('Invalid tasks queue options: must be a plain object') |
| 344 | ) |
| 345 | expect( |
| 346 | () => |
| 347 | new FixedThreadPool( |
| 348 | numberOfWorkers, |
| 349 | './tests/worker-files/thread/testWorker.js', |
| 350 | { |
| 351 | enableTasksQueue: true, |
| 352 | tasksQueueOptions: { concurrency: 0 } |
| 353 | } |
| 354 | ) |
| 355 | ).toThrowError( |
| 356 | new RangeError( |
| 357 | 'Invalid worker node tasks concurrency: 0 is a negative integer or zero' |
| 358 | ) |
| 359 | ) |
| 360 | expect( |
| 361 | () => |
| 362 | new FixedThreadPool( |
| 363 | numberOfWorkers, |
| 364 | './tests/worker-files/thread/testWorker.js', |
| 365 | { |
| 366 | enableTasksQueue: true, |
| 367 | tasksQueueOptions: { concurrency: -1 } |
| 368 | } |
| 369 | ) |
| 370 | ).toThrowError( |
| 371 | new RangeError( |
| 372 | 'Invalid worker node tasks concurrency: -1 is a negative integer or zero' |
| 373 | ) |
| 374 | ) |
| 375 | expect( |
| 376 | () => |
| 377 | new FixedThreadPool( |
| 378 | numberOfWorkers, |
| 379 | './tests/worker-files/thread/testWorker.js', |
| 380 | { |
| 381 | enableTasksQueue: true, |
| 382 | tasksQueueOptions: { concurrency: 0.2 } |
| 383 | } |
| 384 | ) |
| 385 | ).toThrowError( |
| 386 | new TypeError('Invalid worker node tasks concurrency: must be an integer') |
| 387 | ) |
| 388 | expect( |
| 389 | () => |
| 390 | new FixedThreadPool( |
| 391 | numberOfWorkers, |
| 392 | './tests/worker-files/thread/testWorker.js', |
| 393 | { |
| 394 | enableTasksQueue: true, |
| 395 | tasksQueueOptions: { queueMaxSize: 2 } |
| 396 | } |
| 397 | ) |
| 398 | ).toThrowError( |
| 399 | new Error( |
| 400 | 'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead' |
| 401 | ) |
| 402 | ) |
| 403 | expect( |
| 404 | () => |
| 405 | new FixedThreadPool( |
| 406 | numberOfWorkers, |
| 407 | './tests/worker-files/thread/testWorker.js', |
| 408 | { |
| 409 | enableTasksQueue: true, |
| 410 | tasksQueueOptions: { size: 0 } |
| 411 | } |
| 412 | ) |
| 413 | ).toThrowError( |
| 414 | new RangeError( |
| 415 | 'Invalid worker node tasks queue size: 0 is a negative integer or zero' |
| 416 | ) |
| 417 | ) |
| 418 | expect( |
| 419 | () => |
| 420 | new FixedThreadPool( |
| 421 | numberOfWorkers, |
| 422 | './tests/worker-files/thread/testWorker.js', |
| 423 | { |
| 424 | enableTasksQueue: true, |
| 425 | tasksQueueOptions: { size: -1 } |
| 426 | } |
| 427 | ) |
| 428 | ).toThrowError( |
| 429 | new RangeError( |
| 430 | 'Invalid worker node tasks queue size: -1 is a negative integer or zero' |
| 431 | ) |
| 432 | ) |
| 433 | expect( |
| 434 | () => |
| 435 | new FixedThreadPool( |
| 436 | numberOfWorkers, |
| 437 | './tests/worker-files/thread/testWorker.js', |
| 438 | { |
| 439 | enableTasksQueue: true, |
| 440 | tasksQueueOptions: { size: 0.2 } |
| 441 | } |
| 442 | ) |
| 443 | ).toThrowError( |
| 444 | new TypeError('Invalid worker node tasks queue size: must be an integer') |
| 445 | ) |
| 446 | }) |
| 447 | |
| 448 | it('Verify that pool worker choice strategy options can be set', async () => { |
| 449 | const pool = new FixedThreadPool( |
| 450 | numberOfWorkers, |
| 451 | './tests/worker-files/thread/testWorker.js', |
| 452 | { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } |
| 453 | ) |
| 454 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
| 455 | choiceRetries: 6, |
| 456 | runTime: { median: false }, |
| 457 | waitTime: { median: false }, |
| 458 | elu: { median: false } |
| 459 | }) |
| 460 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ |
| 461 | choiceRetries: 6, |
| 462 | runTime: { median: false }, |
| 463 | waitTime: { median: false }, |
| 464 | elu: { median: false } |
| 465 | }) |
| 466 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext |
| 467 | .workerChoiceStrategies) { |
| 468 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
| 469 | choiceRetries: 6, |
| 470 | runTime: { median: false }, |
| 471 | waitTime: { median: false }, |
| 472 | elu: { median: false } |
| 473 | }) |
| 474 | } |
| 475 | expect( |
| 476 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() |
| 477 | ).toStrictEqual({ |
| 478 | runTime: { |
| 479 | aggregate: true, |
| 480 | average: true, |
| 481 | median: false |
| 482 | }, |
| 483 | waitTime: { |
| 484 | aggregate: false, |
| 485 | average: false, |
| 486 | median: false |
| 487 | }, |
| 488 | elu: { |
| 489 | aggregate: true, |
| 490 | average: true, |
| 491 | median: false |
| 492 | } |
| 493 | }) |
| 494 | pool.setWorkerChoiceStrategyOptions({ |
| 495 | runTime: { median: true }, |
| 496 | elu: { median: true } |
| 497 | }) |
| 498 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
| 499 | choiceRetries: 6, |
| 500 | runTime: { median: true }, |
| 501 | waitTime: { median: false }, |
| 502 | elu: { median: true } |
| 503 | }) |
| 504 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ |
| 505 | choiceRetries: 6, |
| 506 | runTime: { median: true }, |
| 507 | waitTime: { median: false }, |
| 508 | elu: { median: true } |
| 509 | }) |
| 510 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext |
| 511 | .workerChoiceStrategies) { |
| 512 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
| 513 | choiceRetries: 6, |
| 514 | runTime: { median: true }, |
| 515 | waitTime: { median: false }, |
| 516 | elu: { median: true } |
| 517 | }) |
| 518 | } |
| 519 | expect( |
| 520 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() |
| 521 | ).toStrictEqual({ |
| 522 | runTime: { |
| 523 | aggregate: true, |
| 524 | average: false, |
| 525 | median: true |
| 526 | }, |
| 527 | waitTime: { |
| 528 | aggregate: false, |
| 529 | average: false, |
| 530 | median: false |
| 531 | }, |
| 532 | elu: { |
| 533 | aggregate: true, |
| 534 | average: false, |
| 535 | median: true |
| 536 | } |
| 537 | }) |
| 538 | pool.setWorkerChoiceStrategyOptions({ |
| 539 | runTime: { median: false }, |
| 540 | elu: { median: false } |
| 541 | }) |
| 542 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
| 543 | choiceRetries: 6, |
| 544 | runTime: { median: false }, |
| 545 | waitTime: { median: false }, |
| 546 | elu: { median: false } |
| 547 | }) |
| 548 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ |
| 549 | choiceRetries: 6, |
| 550 | runTime: { median: false }, |
| 551 | waitTime: { median: false }, |
| 552 | elu: { median: false } |
| 553 | }) |
| 554 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext |
| 555 | .workerChoiceStrategies) { |
| 556 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
| 557 | choiceRetries: 6, |
| 558 | runTime: { median: false }, |
| 559 | waitTime: { median: false }, |
| 560 | elu: { median: false } |
| 561 | }) |
| 562 | } |
| 563 | expect( |
| 564 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() |
| 565 | ).toStrictEqual({ |
| 566 | runTime: { |
| 567 | aggregate: true, |
| 568 | average: true, |
| 569 | median: false |
| 570 | }, |
| 571 | waitTime: { |
| 572 | aggregate: false, |
| 573 | average: false, |
| 574 | median: false |
| 575 | }, |
| 576 | elu: { |
| 577 | aggregate: true, |
| 578 | average: true, |
| 579 | median: false |
| 580 | } |
| 581 | }) |
| 582 | expect(() => |
| 583 | pool.setWorkerChoiceStrategyOptions('invalidWorkerChoiceStrategyOptions') |
| 584 | ).toThrowError( |
| 585 | new TypeError( |
| 586 | 'Invalid worker choice strategy options: must be a plain object' |
| 587 | ) |
| 588 | ) |
| 589 | expect(() => |
| 590 | pool.setWorkerChoiceStrategyOptions({ |
| 591 | choiceRetries: 'invalidChoiceRetries' |
| 592 | }) |
| 593 | ).toThrowError( |
| 594 | new TypeError( |
| 595 | 'Invalid worker choice strategy options: choice retries must be an integer' |
| 596 | ) |
| 597 | ) |
| 598 | expect(() => |
| 599 | pool.setWorkerChoiceStrategyOptions({ choiceRetries: -1 }) |
| 600 | ).toThrowError( |
| 601 | new RangeError( |
| 602 | "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero" |
| 603 | ) |
| 604 | ) |
| 605 | expect(() => |
| 606 | pool.setWorkerChoiceStrategyOptions({ weights: {} }) |
| 607 | ).toThrowError( |
| 608 | new Error( |
| 609 | 'Invalid worker choice strategy options: must have a weight for each worker node' |
| 610 | ) |
| 611 | ) |
| 612 | expect(() => |
| 613 | pool.setWorkerChoiceStrategyOptions({ measurement: 'invalidMeasurement' }) |
| 614 | ).toThrowError( |
| 615 | new Error( |
| 616 | "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" |
| 617 | ) |
| 618 | ) |
| 619 | await pool.destroy() |
| 620 | }) |
| 621 | |
| 622 | it('Verify that pool tasks queue can be enabled/disabled', async () => { |
| 623 | const pool = new FixedThreadPool( |
| 624 | numberOfWorkers, |
| 625 | './tests/worker-files/thread/testWorker.js' |
| 626 | ) |
| 627 | expect(pool.opts.enableTasksQueue).toBe(false) |
| 628 | expect(pool.opts.tasksQueueOptions).toBeUndefined() |
| 629 | pool.enableTasksQueue(true) |
| 630 | expect(pool.opts.enableTasksQueue).toBe(true) |
| 631 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
| 632 | concurrency: 1, |
| 633 | size: 4 |
| 634 | }) |
| 635 | pool.enableTasksQueue(true, { concurrency: 2 }) |
| 636 | expect(pool.opts.enableTasksQueue).toBe(true) |
| 637 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
| 638 | concurrency: 2, |
| 639 | size: 4 |
| 640 | }) |
| 641 | pool.enableTasksQueue(false) |
| 642 | expect(pool.opts.enableTasksQueue).toBe(false) |
| 643 | expect(pool.opts.tasksQueueOptions).toBeUndefined() |
| 644 | await pool.destroy() |
| 645 | }) |
| 646 | |
| 647 | it('Verify that pool tasks queue options can be set', async () => { |
| 648 | const pool = new FixedThreadPool( |
| 649 | numberOfWorkers, |
| 650 | './tests/worker-files/thread/testWorker.js', |
| 651 | { enableTasksQueue: true } |
| 652 | ) |
| 653 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
| 654 | concurrency: 1, |
| 655 | size: 4 |
| 656 | }) |
| 657 | pool.setTasksQueueOptions({ concurrency: 2 }) |
| 658 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
| 659 | concurrency: 2, |
| 660 | size: 4 |
| 661 | }) |
| 662 | expect(() => |
| 663 | pool.setTasksQueueOptions('invalidTasksQueueOptions') |
| 664 | ).toThrowError( |
| 665 | new TypeError('Invalid tasks queue options: must be a plain object') |
| 666 | ) |
| 667 | expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError( |
| 668 | new RangeError( |
| 669 | 'Invalid worker node tasks concurrency: 0 is a negative integer or zero' |
| 670 | ) |
| 671 | ) |
| 672 | expect(() => pool.setTasksQueueOptions({ concurrency: -1 })).toThrowError( |
| 673 | new RangeError( |
| 674 | 'Invalid worker node tasks concurrency: -1 is a negative integer or zero' |
| 675 | ) |
| 676 | ) |
| 677 | expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError( |
| 678 | new TypeError('Invalid worker node tasks concurrency: must be an integer') |
| 679 | ) |
| 680 | expect(() => pool.setTasksQueueOptions({ queueMaxSize: 2 })).toThrowError( |
| 681 | new Error( |
| 682 | 'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead' |
| 683 | ) |
| 684 | ) |
| 685 | expect(() => pool.setTasksQueueOptions({ size: 0 })).toThrowError( |
| 686 | new RangeError( |
| 687 | 'Invalid worker node tasks queue size: 0 is a negative integer or zero' |
| 688 | ) |
| 689 | ) |
| 690 | expect(() => pool.setTasksQueueOptions({ size: -1 })).toThrowError( |
| 691 | new RangeError( |
| 692 | 'Invalid worker node tasks queue size: -1 is a negative integer or zero' |
| 693 | ) |
| 694 | ) |
| 695 | expect(() => pool.setTasksQueueOptions({ size: 0.2 })).toThrowError( |
| 696 | new TypeError('Invalid worker node tasks queue size: must be an integer') |
| 697 | ) |
| 698 | await pool.destroy() |
| 699 | }) |
| 700 | |
| 701 | it('Verify that pool info is set', async () => { |
| 702 | let pool = new FixedThreadPool( |
| 703 | numberOfWorkers, |
| 704 | './tests/worker-files/thread/testWorker.js' |
| 705 | ) |
| 706 | expect(pool.info).toStrictEqual({ |
| 707 | version, |
| 708 | type: PoolTypes.fixed, |
| 709 | worker: WorkerTypes.thread, |
| 710 | ready: true, |
| 711 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, |
| 712 | minSize: numberOfWorkers, |
| 713 | maxSize: numberOfWorkers, |
| 714 | workerNodes: numberOfWorkers, |
| 715 | idleWorkerNodes: numberOfWorkers, |
| 716 | busyWorkerNodes: 0, |
| 717 | executedTasks: 0, |
| 718 | executingTasks: 0, |
| 719 | failedTasks: 0 |
| 720 | }) |
| 721 | await pool.destroy() |
| 722 | pool = new DynamicClusterPool( |
| 723 | Math.floor(numberOfWorkers / 2), |
| 724 | numberOfWorkers, |
| 725 | './tests/worker-files/cluster/testWorker.js' |
| 726 | ) |
| 727 | expect(pool.info).toStrictEqual({ |
| 728 | version, |
| 729 | type: PoolTypes.dynamic, |
| 730 | worker: WorkerTypes.cluster, |
| 731 | ready: true, |
| 732 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, |
| 733 | minSize: Math.floor(numberOfWorkers / 2), |
| 734 | maxSize: numberOfWorkers, |
| 735 | workerNodes: Math.floor(numberOfWorkers / 2), |
| 736 | idleWorkerNodes: Math.floor(numberOfWorkers / 2), |
| 737 | busyWorkerNodes: 0, |
| 738 | executedTasks: 0, |
| 739 | executingTasks: 0, |
| 740 | failedTasks: 0 |
| 741 | }) |
| 742 | await pool.destroy() |
| 743 | }) |
| 744 | |
| 745 | it('Verify that pool worker tasks usage are initialized', async () => { |
| 746 | const pool = new FixedClusterPool( |
| 747 | numberOfWorkers, |
| 748 | './tests/worker-files/cluster/testWorker.js' |
| 749 | ) |
| 750 | for (const workerNode of pool.workerNodes) { |
| 751 | expect(workerNode.usage).toStrictEqual({ |
| 752 | tasks: { |
| 753 | executed: 0, |
| 754 | executing: 0, |
| 755 | queued: 0, |
| 756 | maxQueued: 0, |
| 757 | stolen: 0, |
| 758 | failed: 0 |
| 759 | }, |
| 760 | runTime: { |
| 761 | history: expect.any(CircularArray) |
| 762 | }, |
| 763 | waitTime: { |
| 764 | history: expect.any(CircularArray) |
| 765 | }, |
| 766 | elu: { |
| 767 | idle: { |
| 768 | history: expect.any(CircularArray) |
| 769 | }, |
| 770 | active: { |
| 771 | history: expect.any(CircularArray) |
| 772 | } |
| 773 | } |
| 774 | }) |
| 775 | } |
| 776 | await pool.destroy() |
| 777 | }) |
| 778 | |
| 779 | it('Verify that pool worker tasks queue are initialized', async () => { |
| 780 | let pool = new FixedClusterPool( |
| 781 | numberOfWorkers, |
| 782 | './tests/worker-files/cluster/testWorker.js' |
| 783 | ) |
| 784 | for (const workerNode of pool.workerNodes) { |
| 785 | expect(workerNode.tasksQueue).toBeDefined() |
| 786 | expect(workerNode.tasksQueue).toBeInstanceOf(Deque) |
| 787 | expect(workerNode.tasksQueue.size).toBe(0) |
| 788 | expect(workerNode.tasksQueue.maxSize).toBe(0) |
| 789 | } |
| 790 | await pool.destroy() |
| 791 | pool = new DynamicThreadPool( |
| 792 | Math.floor(numberOfWorkers / 2), |
| 793 | numberOfWorkers, |
| 794 | './tests/worker-files/thread/testWorker.js' |
| 795 | ) |
| 796 | for (const workerNode of pool.workerNodes) { |
| 797 | expect(workerNode.tasksQueue).toBeDefined() |
| 798 | expect(workerNode.tasksQueue).toBeInstanceOf(Deque) |
| 799 | expect(workerNode.tasksQueue.size).toBe(0) |
| 800 | expect(workerNode.tasksQueue.maxSize).toBe(0) |
| 801 | } |
| 802 | }) |
| 803 | |
| 804 | it('Verify that pool worker info are initialized', async () => { |
| 805 | let pool = new FixedClusterPool( |
| 806 | numberOfWorkers, |
| 807 | './tests/worker-files/cluster/testWorker.js' |
| 808 | ) |
| 809 | for (const workerNode of pool.workerNodes) { |
| 810 | expect(workerNode.info).toStrictEqual({ |
| 811 | id: expect.any(Number), |
| 812 | type: WorkerTypes.cluster, |
| 813 | dynamic: false, |
| 814 | ready: true |
| 815 | }) |
| 816 | } |
| 817 | await pool.destroy() |
| 818 | pool = new DynamicThreadPool( |
| 819 | Math.floor(numberOfWorkers / 2), |
| 820 | numberOfWorkers, |
| 821 | './tests/worker-files/thread/testWorker.js' |
| 822 | ) |
| 823 | for (const workerNode of pool.workerNodes) { |
| 824 | expect(workerNode.info).toStrictEqual({ |
| 825 | id: expect.any(Number), |
| 826 | type: WorkerTypes.thread, |
| 827 | dynamic: false, |
| 828 | ready: true |
| 829 | }) |
| 830 | } |
| 831 | }) |
| 832 | |
| 833 | it('Verify that pool execute() arguments are checked', async () => { |
| 834 | const pool = new FixedClusterPool( |
| 835 | numberOfWorkers, |
| 836 | './tests/worker-files/cluster/testWorker.js' |
| 837 | ) |
| 838 | await expect(pool.execute(undefined, 0)).rejects.toThrowError( |
| 839 | new TypeError('name argument must be a string') |
| 840 | ) |
| 841 | await expect(pool.execute(undefined, '')).rejects.toThrowError( |
| 842 | new TypeError('name argument must not be an empty string') |
| 843 | ) |
| 844 | await expect(pool.execute(undefined, undefined, {})).rejects.toThrowError( |
| 845 | new TypeError('transferList argument must be an array') |
| 846 | ) |
| 847 | await expect(pool.execute(undefined, 'unknown')).rejects.toBe( |
| 848 | "Task function 'unknown' not found" |
| 849 | ) |
| 850 | await pool.destroy() |
| 851 | await expect(pool.execute(undefined, undefined, {})).rejects.toThrowError( |
| 852 | new Error('Cannot execute a task on destroyed pool') |
| 853 | ) |
| 854 | }) |
| 855 | |
| 856 | it('Verify that pool worker tasks usage are computed', async () => { |
| 857 | const pool = new FixedClusterPool( |
| 858 | numberOfWorkers, |
| 859 | './tests/worker-files/cluster/testWorker.js' |
| 860 | ) |
| 861 | const promises = new Set() |
| 862 | const maxMultiplier = 2 |
| 863 | for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { |
| 864 | promises.add(pool.execute()) |
| 865 | } |
| 866 | for (const workerNode of pool.workerNodes) { |
| 867 | expect(workerNode.usage).toStrictEqual({ |
| 868 | tasks: { |
| 869 | executed: 0, |
| 870 | executing: maxMultiplier, |
| 871 | queued: 0, |
| 872 | maxQueued: 0, |
| 873 | stolen: 0, |
| 874 | failed: 0 |
| 875 | }, |
| 876 | runTime: { |
| 877 | history: expect.any(CircularArray) |
| 878 | }, |
| 879 | waitTime: { |
| 880 | history: expect.any(CircularArray) |
| 881 | }, |
| 882 | elu: { |
| 883 | idle: { |
| 884 | history: expect.any(CircularArray) |
| 885 | }, |
| 886 | active: { |
| 887 | history: expect.any(CircularArray) |
| 888 | } |
| 889 | } |
| 890 | }) |
| 891 | } |
| 892 | await Promise.all(promises) |
| 893 | for (const workerNode of pool.workerNodes) { |
| 894 | expect(workerNode.usage).toStrictEqual({ |
| 895 | tasks: { |
| 896 | executed: maxMultiplier, |
| 897 | executing: 0, |
| 898 | queued: 0, |
| 899 | maxQueued: 0, |
| 900 | stolen: 0, |
| 901 | failed: 0 |
| 902 | }, |
| 903 | runTime: { |
| 904 | history: expect.any(CircularArray) |
| 905 | }, |
| 906 | waitTime: { |
| 907 | history: expect.any(CircularArray) |
| 908 | }, |
| 909 | elu: { |
| 910 | idle: { |
| 911 | history: expect.any(CircularArray) |
| 912 | }, |
| 913 | active: { |
| 914 | history: expect.any(CircularArray) |
| 915 | } |
| 916 | } |
| 917 | }) |
| 918 | } |
| 919 | await pool.destroy() |
| 920 | }) |
| 921 | |
| 922 | it('Verify that pool worker tasks usage are reset at worker choice strategy change', async () => { |
| 923 | const pool = new DynamicThreadPool( |
| 924 | Math.floor(numberOfWorkers / 2), |
| 925 | numberOfWorkers, |
| 926 | './tests/worker-files/thread/testWorker.js' |
| 927 | ) |
| 928 | const promises = new Set() |
| 929 | const maxMultiplier = 2 |
| 930 | for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { |
| 931 | promises.add(pool.execute()) |
| 932 | } |
| 933 | await Promise.all(promises) |
| 934 | for (const workerNode of pool.workerNodes) { |
| 935 | expect(workerNode.usage).toStrictEqual({ |
| 936 | tasks: { |
| 937 | executed: expect.any(Number), |
| 938 | executing: 0, |
| 939 | queued: 0, |
| 940 | maxQueued: 0, |
| 941 | stolen: 0, |
| 942 | failed: 0 |
| 943 | }, |
| 944 | runTime: { |
| 945 | history: expect.any(CircularArray) |
| 946 | }, |
| 947 | waitTime: { |
| 948 | history: expect.any(CircularArray) |
| 949 | }, |
| 950 | elu: { |
| 951 | idle: { |
| 952 | history: expect.any(CircularArray) |
| 953 | }, |
| 954 | active: { |
| 955 | history: expect.any(CircularArray) |
| 956 | } |
| 957 | } |
| 958 | }) |
| 959 | expect(workerNode.usage.tasks.executed).toBeGreaterThan(0) |
| 960 | expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual( |
| 961 | numberOfWorkers * maxMultiplier |
| 962 | ) |
| 963 | expect(workerNode.usage.runTime.history.length).toBe(0) |
| 964 | expect(workerNode.usage.waitTime.history.length).toBe(0) |
| 965 | expect(workerNode.usage.elu.idle.history.length).toBe(0) |
| 966 | expect(workerNode.usage.elu.active.history.length).toBe(0) |
| 967 | } |
| 968 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) |
| 969 | for (const workerNode of pool.workerNodes) { |
| 970 | expect(workerNode.usage).toStrictEqual({ |
| 971 | tasks: { |
| 972 | executed: 0, |
| 973 | executing: 0, |
| 974 | queued: 0, |
| 975 | maxQueued: 0, |
| 976 | stolen: 0, |
| 977 | failed: 0 |
| 978 | }, |
| 979 | runTime: { |
| 980 | history: expect.any(CircularArray) |
| 981 | }, |
| 982 | waitTime: { |
| 983 | history: expect.any(CircularArray) |
| 984 | }, |
| 985 | elu: { |
| 986 | idle: { |
| 987 | history: expect.any(CircularArray) |
| 988 | }, |
| 989 | active: { |
| 990 | history: expect.any(CircularArray) |
| 991 | } |
| 992 | } |
| 993 | }) |
| 994 | expect(workerNode.usage.runTime.history.length).toBe(0) |
| 995 | expect(workerNode.usage.waitTime.history.length).toBe(0) |
| 996 | expect(workerNode.usage.elu.idle.history.length).toBe(0) |
| 997 | expect(workerNode.usage.elu.active.history.length).toBe(0) |
| 998 | } |
| 999 | await pool.destroy() |
| 1000 | }) |
| 1001 | |
| 1002 | it("Verify that pool event emitter 'ready' event can register a callback", async () => { |
| 1003 | const pool = new DynamicClusterPool( |
| 1004 | Math.floor(numberOfWorkers / 2), |
| 1005 | numberOfWorkers, |
| 1006 | './tests/worker-files/cluster/testWorker.js' |
| 1007 | ) |
| 1008 | let poolInfo |
| 1009 | let poolReady = 0 |
| 1010 | pool.emitter.on(PoolEvents.ready, (info) => { |
| 1011 | ++poolReady |
| 1012 | poolInfo = info |
| 1013 | }) |
| 1014 | await waitPoolEvents(pool, PoolEvents.ready, 1) |
| 1015 | expect(poolReady).toBe(1) |
| 1016 | expect(poolInfo).toStrictEqual({ |
| 1017 | version, |
| 1018 | type: PoolTypes.dynamic, |
| 1019 | worker: WorkerTypes.cluster, |
| 1020 | ready: true, |
| 1021 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, |
| 1022 | minSize: expect.any(Number), |
| 1023 | maxSize: expect.any(Number), |
| 1024 | workerNodes: expect.any(Number), |
| 1025 | idleWorkerNodes: expect.any(Number), |
| 1026 | busyWorkerNodes: expect.any(Number), |
| 1027 | executedTasks: expect.any(Number), |
| 1028 | executingTasks: expect.any(Number), |
| 1029 | failedTasks: expect.any(Number) |
| 1030 | }) |
| 1031 | await pool.destroy() |
| 1032 | }) |
| 1033 | |
| 1034 | it("Verify that pool event emitter 'busy' event can register a callback", async () => { |
| 1035 | const pool = new FixedThreadPool( |
| 1036 | numberOfWorkers, |
| 1037 | './tests/worker-files/thread/testWorker.js' |
| 1038 | ) |
| 1039 | const promises = new Set() |
| 1040 | let poolBusy = 0 |
| 1041 | let poolInfo |
| 1042 | pool.emitter.on(PoolEvents.busy, (info) => { |
| 1043 | ++poolBusy |
| 1044 | poolInfo = info |
| 1045 | }) |
| 1046 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
| 1047 | promises.add(pool.execute()) |
| 1048 | } |
| 1049 | await Promise.all(promises) |
| 1050 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. |
| 1051 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. |
| 1052 | expect(poolBusy).toBe(numberOfWorkers + 1) |
| 1053 | expect(poolInfo).toStrictEqual({ |
| 1054 | version, |
| 1055 | type: PoolTypes.fixed, |
| 1056 | worker: WorkerTypes.thread, |
| 1057 | ready: expect.any(Boolean), |
| 1058 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, |
| 1059 | minSize: expect.any(Number), |
| 1060 | maxSize: expect.any(Number), |
| 1061 | workerNodes: expect.any(Number), |
| 1062 | idleWorkerNodes: expect.any(Number), |
| 1063 | busyWorkerNodes: expect.any(Number), |
| 1064 | executedTasks: expect.any(Number), |
| 1065 | executingTasks: expect.any(Number), |
| 1066 | failedTasks: expect.any(Number) |
| 1067 | }) |
| 1068 | await pool.destroy() |
| 1069 | }) |
| 1070 | |
| 1071 | it("Verify that pool event emitter 'full' event can register a callback", async () => { |
| 1072 | const pool = new DynamicThreadPool( |
| 1073 | Math.floor(numberOfWorkers / 2), |
| 1074 | numberOfWorkers, |
| 1075 | './tests/worker-files/thread/testWorker.js' |
| 1076 | ) |
| 1077 | const promises = new Set() |
| 1078 | let poolFull = 0 |
| 1079 | let poolInfo |
| 1080 | pool.emitter.on(PoolEvents.full, (info) => { |
| 1081 | ++poolFull |
| 1082 | poolInfo = info |
| 1083 | }) |
| 1084 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
| 1085 | promises.add(pool.execute()) |
| 1086 | } |
| 1087 | await Promise.all(promises) |
| 1088 | expect(poolFull).toBe(1) |
| 1089 | expect(poolInfo).toStrictEqual({ |
| 1090 | version, |
| 1091 | type: PoolTypes.dynamic, |
| 1092 | worker: WorkerTypes.thread, |
| 1093 | ready: expect.any(Boolean), |
| 1094 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, |
| 1095 | minSize: expect.any(Number), |
| 1096 | maxSize: expect.any(Number), |
| 1097 | workerNodes: expect.any(Number), |
| 1098 | idleWorkerNodes: expect.any(Number), |
| 1099 | busyWorkerNodes: expect.any(Number), |
| 1100 | executedTasks: expect.any(Number), |
| 1101 | executingTasks: expect.any(Number), |
| 1102 | failedTasks: expect.any(Number) |
| 1103 | }) |
| 1104 | await pool.destroy() |
| 1105 | }) |
| 1106 | |
| 1107 | it("Verify that pool event emitter 'backPressure' event can register a callback", async () => { |
| 1108 | const pool = new FixedThreadPool( |
| 1109 | numberOfWorkers, |
| 1110 | './tests/worker-files/thread/testWorker.js', |
| 1111 | { |
| 1112 | enableTasksQueue: true |
| 1113 | } |
| 1114 | ) |
| 1115 | sinon.stub(pool, 'hasBackPressure').returns(true) |
| 1116 | const promises = new Set() |
| 1117 | let poolBackPressure = 0 |
| 1118 | let poolInfo |
| 1119 | pool.emitter.on(PoolEvents.backPressure, (info) => { |
| 1120 | ++poolBackPressure |
| 1121 | poolInfo = info |
| 1122 | }) |
| 1123 | for (let i = 0; i < numberOfWorkers + 1; i++) { |
| 1124 | promises.add(pool.execute()) |
| 1125 | } |
| 1126 | await Promise.all(promises) |
| 1127 | expect(poolBackPressure).toBe(1) |
| 1128 | expect(poolInfo).toStrictEqual({ |
| 1129 | version, |
| 1130 | type: PoolTypes.fixed, |
| 1131 | worker: WorkerTypes.thread, |
| 1132 | ready: expect.any(Boolean), |
| 1133 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, |
| 1134 | minSize: expect.any(Number), |
| 1135 | maxSize: expect.any(Number), |
| 1136 | workerNodes: expect.any(Number), |
| 1137 | idleWorkerNodes: expect.any(Number), |
| 1138 | busyWorkerNodes: expect.any(Number), |
| 1139 | executedTasks: expect.any(Number), |
| 1140 | executingTasks: expect.any(Number), |
| 1141 | maxQueuedTasks: expect.any(Number), |
| 1142 | queuedTasks: expect.any(Number), |
| 1143 | backPressure: true, |
| 1144 | stolenTasks: expect.any(Number), |
| 1145 | failedTasks: expect.any(Number) |
| 1146 | }) |
| 1147 | expect(pool.hasBackPressure.called).toBe(true) |
| 1148 | await pool.destroy() |
| 1149 | }) |
| 1150 | |
| 1151 | it('Verify that listTaskFunctions() is working', async () => { |
| 1152 | const dynamicThreadPool = new DynamicThreadPool( |
| 1153 | Math.floor(numberOfWorkers / 2), |
| 1154 | numberOfWorkers, |
| 1155 | './tests/worker-files/thread/testMultipleTaskFunctionsWorker.js' |
| 1156 | ) |
| 1157 | await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1) |
| 1158 | expect(dynamicThreadPool.listTaskFunctions()).toStrictEqual([ |
| 1159 | DEFAULT_TASK_NAME, |
| 1160 | 'jsonIntegerSerialization', |
| 1161 | 'factorial', |
| 1162 | 'fibonacci' |
| 1163 | ]) |
| 1164 | const fixedClusterPool = new FixedClusterPool( |
| 1165 | numberOfWorkers, |
| 1166 | './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js' |
| 1167 | ) |
| 1168 | await waitPoolEvents(fixedClusterPool, PoolEvents.ready, 1) |
| 1169 | expect(fixedClusterPool.listTaskFunctions()).toStrictEqual([ |
| 1170 | DEFAULT_TASK_NAME, |
| 1171 | 'jsonIntegerSerialization', |
| 1172 | 'factorial', |
| 1173 | 'fibonacci' |
| 1174 | ]) |
| 1175 | }) |
| 1176 | |
| 1177 | it('Verify that multiple task functions worker is working', async () => { |
| 1178 | const pool = new DynamicClusterPool( |
| 1179 | Math.floor(numberOfWorkers / 2), |
| 1180 | numberOfWorkers, |
| 1181 | './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js' |
| 1182 | ) |
| 1183 | const data = { n: 10 } |
| 1184 | const result0 = await pool.execute(data) |
| 1185 | expect(result0).toStrictEqual({ ok: 1 }) |
| 1186 | const result1 = await pool.execute(data, 'jsonIntegerSerialization') |
| 1187 | expect(result1).toStrictEqual({ ok: 1 }) |
| 1188 | const result2 = await pool.execute(data, 'factorial') |
| 1189 | expect(result2).toBe(3628800) |
| 1190 | const result3 = await pool.execute(data, 'fibonacci') |
| 1191 | expect(result3).toBe(55) |
| 1192 | expect(pool.info.executingTasks).toBe(0) |
| 1193 | expect(pool.info.executedTasks).toBe(4) |
| 1194 | for (const workerNode of pool.workerNodes) { |
| 1195 | expect(workerNode.info.taskFunctions).toStrictEqual([ |
| 1196 | DEFAULT_TASK_NAME, |
| 1197 | 'jsonIntegerSerialization', |
| 1198 | 'factorial', |
| 1199 | 'fibonacci' |
| 1200 | ]) |
| 1201 | expect(workerNode.taskFunctionsUsage.size).toBe(3) |
| 1202 | for (const name of pool.listTaskFunctions()) { |
| 1203 | expect(workerNode.getTaskFunctionWorkerUsage(name)).toStrictEqual({ |
| 1204 | tasks: { |
| 1205 | executed: expect.any(Number), |
| 1206 | executing: expect.any(Number), |
| 1207 | failed: 0, |
| 1208 | queued: 0, |
| 1209 | stolen: 0 |
| 1210 | }, |
| 1211 | runTime: { |
| 1212 | history: expect.any(CircularArray) |
| 1213 | }, |
| 1214 | waitTime: { |
| 1215 | history: expect.any(CircularArray) |
| 1216 | }, |
| 1217 | elu: { |
| 1218 | idle: { |
| 1219 | history: expect.any(CircularArray) |
| 1220 | }, |
| 1221 | active: { |
| 1222 | history: expect.any(CircularArray) |
| 1223 | } |
| 1224 | } |
| 1225 | }) |
| 1226 | expect( |
| 1227 | workerNode.getTaskFunctionWorkerUsage(name).tasks.executing |
| 1228 | ).toBeGreaterThanOrEqual(0) |
| 1229 | } |
| 1230 | } |
| 1231 | }) |
| 1232 | }) |