| 1 | const { expect } = require('expect') |
| 2 | const { |
| 3 | WorkerChoiceStrategies, |
| 4 | DynamicThreadPool, |
| 5 | FixedThreadPool, |
| 6 | FixedClusterPool |
| 7 | } = require('../../../lib') |
| 8 | const { CircularArray } = require('../../../lib/circular-array') |
| 9 | |
| 10 | describe('Selection strategies test suite', () => { |
| 11 | const min = 0 |
| 12 | const max = 3 |
| 13 | |
| 14 | it('Verify that WorkerChoiceStrategies enumeration provides string values', () => { |
| 15 | expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN') |
| 16 | expect(WorkerChoiceStrategies.LEAST_USED).toBe('LEAST_USED') |
| 17 | expect(WorkerChoiceStrategies.LEAST_ELU).toBe('LEAST_ELU') |
| 18 | expect(WorkerChoiceStrategies.LEAST_BUSY).toBe('LEAST_BUSY') |
| 19 | expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE') |
| 20 | expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe( |
| 21 | 'WEIGHTED_ROUND_ROBIN' |
| 22 | ) |
| 23 | expect(WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN).toBe( |
| 24 | 'INTERLEAVED_WEIGHTED_ROUND_ROBIN' |
| 25 | ) |
| 26 | }) |
| 27 | |
| 28 | it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => { |
| 29 | const pool = new DynamicThreadPool( |
| 30 | min, |
| 31 | max, |
| 32 | './tests/worker-files/thread/testWorker.js' |
| 33 | ) |
| 34 | expect(pool.opts.workerChoiceStrategy).toBe( |
| 35 | WorkerChoiceStrategies.ROUND_ROBIN |
| 36 | ) |
| 37 | // We need to clean up the resources after our test |
| 38 | await pool.destroy() |
| 39 | }) |
| 40 | |
| 41 | it('Verify available strategies are taken at pool creation', async () => { |
| 42 | for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) { |
| 43 | const pool = new FixedThreadPool( |
| 44 | max, |
| 45 | './tests/worker-files/thread/testWorker.js', |
| 46 | { workerChoiceStrategy } |
| 47 | ) |
| 48 | expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy) |
| 49 | expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
| 50 | workerChoiceStrategy |
| 51 | ) |
| 52 | await pool.destroy() |
| 53 | } |
| 54 | }) |
| 55 | |
| 56 | it('Verify available strategies can be set after pool creation', async () => { |
| 57 | for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) { |
| 58 | const pool = new DynamicThreadPool( |
| 59 | min, |
| 60 | max, |
| 61 | './tests/worker-files/thread/testWorker.js' |
| 62 | ) |
| 63 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 64 | expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy) |
| 65 | expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
| 66 | workerChoiceStrategy |
| 67 | ) |
| 68 | await pool.destroy() |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | it('Verify available strategies default internals at pool creation', async () => { |
| 73 | const pool = new FixedThreadPool( |
| 74 | max, |
| 75 | './tests/worker-files/thread/testWorker.js' |
| 76 | ) |
| 77 | for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) { |
| 78 | if (workerChoiceStrategy === WorkerChoiceStrategies.ROUND_ROBIN) { |
| 79 | expect( |
| 80 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 81 | workerChoiceStrategy |
| 82 | ).nextWorkerNodeId |
| 83 | ).toBe(0) |
| 84 | } else if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) { |
| 85 | expect( |
| 86 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 87 | workerChoiceStrategy |
| 88 | ).workersVirtualTaskEndTimestamp |
| 89 | ).toBeInstanceOf(Array) |
| 90 | expect( |
| 91 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 92 | workerChoiceStrategy |
| 93 | ).workersVirtualTaskEndTimestamp.length |
| 94 | ).toBe(0) |
| 95 | } else if ( |
| 96 | workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 97 | ) { |
| 98 | expect( |
| 99 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 100 | workerChoiceStrategy |
| 101 | ).currentWorkerNodeId |
| 102 | ).toBe(0) |
| 103 | expect( |
| 104 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 105 | workerChoiceStrategy |
| 106 | ).defaultWorkerWeight |
| 107 | ).toBeGreaterThan(0) |
| 108 | expect( |
| 109 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 110 | workerChoiceStrategy |
| 111 | ).workerVirtualTaskRunTime |
| 112 | ).toBe(0) |
| 113 | } |
| 114 | } |
| 115 | await pool.destroy() |
| 116 | }) |
| 117 | |
| 118 | it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { |
| 119 | const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
| 120 | let pool = new FixedThreadPool( |
| 121 | max, |
| 122 | './tests/worker-files/thread/testWorker.js', |
| 123 | { workerChoiceStrategy } |
| 124 | ) |
| 125 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 126 | runTime: false, |
| 127 | avgRunTime: false, |
| 128 | medRunTime: false, |
| 129 | waitTime: false, |
| 130 | avgWaitTime: false, |
| 131 | medWaitTime: false, |
| 132 | elu: false |
| 133 | }) |
| 134 | await pool.destroy() |
| 135 | pool = new DynamicThreadPool( |
| 136 | min, |
| 137 | max, |
| 138 | './tests/worker-files/thread/testWorker.js', |
| 139 | { workerChoiceStrategy } |
| 140 | ) |
| 141 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 142 | runTime: false, |
| 143 | avgRunTime: false, |
| 144 | medRunTime: false, |
| 145 | waitTime: false, |
| 146 | avgWaitTime: false, |
| 147 | medWaitTime: false, |
| 148 | elu: false |
| 149 | }) |
| 150 | // We need to clean up the resources after our test |
| 151 | await pool.destroy() |
| 152 | }) |
| 153 | |
| 154 | it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => { |
| 155 | const pool = new FixedThreadPool( |
| 156 | max, |
| 157 | './tests/worker-files/thread/testWorker.js', |
| 158 | { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } |
| 159 | ) |
| 160 | // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` |
| 161 | const promises = new Set() |
| 162 | const maxMultiplier = 2 |
| 163 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 164 | promises.add(pool.execute()) |
| 165 | } |
| 166 | await Promise.all(promises) |
| 167 | for (const workerNode of pool.workerNodes) { |
| 168 | expect(workerNode.workerUsage).toStrictEqual({ |
| 169 | tasks: { |
| 170 | executed: maxMultiplier, |
| 171 | executing: 0, |
| 172 | queued: 0, |
| 173 | failed: 0 |
| 174 | }, |
| 175 | runTime: { |
| 176 | aggregation: 0, |
| 177 | average: 0, |
| 178 | median: 0, |
| 179 | history: expect.any(CircularArray) |
| 180 | }, |
| 181 | waitTime: { |
| 182 | aggregation: 0, |
| 183 | average: 0, |
| 184 | median: 0, |
| 185 | history: expect.any(CircularArray) |
| 186 | }, |
| 187 | elu: undefined |
| 188 | }) |
| 189 | } |
| 190 | expect( |
| 191 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 192 | WorkerChoiceStrategies.ROUND_ROBIN |
| 193 | ).nextWorkerNodeId |
| 194 | ).toBe(0) |
| 195 | // We need to clean up the resources after our test |
| 196 | await pool.destroy() |
| 197 | }) |
| 198 | |
| 199 | it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => { |
| 200 | const pool = new DynamicThreadPool( |
| 201 | min, |
| 202 | max, |
| 203 | './tests/worker-files/thread/testWorker.js', |
| 204 | { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } |
| 205 | ) |
| 206 | // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` |
| 207 | const promises = new Set() |
| 208 | const maxMultiplier = 2 |
| 209 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 210 | promises.add(pool.execute()) |
| 211 | } |
| 212 | await Promise.all(promises) |
| 213 | for (const workerNode of pool.workerNodes) { |
| 214 | expect(workerNode.workerUsage).toStrictEqual({ |
| 215 | tasks: { |
| 216 | executed: maxMultiplier, |
| 217 | executing: 0, |
| 218 | queued: 0, |
| 219 | failed: 0 |
| 220 | }, |
| 221 | runTime: { |
| 222 | aggregation: 0, |
| 223 | average: 0, |
| 224 | median: 0, |
| 225 | history: expect.any(CircularArray) |
| 226 | }, |
| 227 | waitTime: { |
| 228 | aggregation: 0, |
| 229 | average: 0, |
| 230 | median: 0, |
| 231 | history: expect.any(CircularArray) |
| 232 | }, |
| 233 | elu: undefined |
| 234 | }) |
| 235 | } |
| 236 | expect( |
| 237 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 238 | WorkerChoiceStrategies.ROUND_ROBIN |
| 239 | ).nextWorkerNodeId |
| 240 | ).toBe(0) |
| 241 | // We need to clean up the resources after our test |
| 242 | await pool.destroy() |
| 243 | }) |
| 244 | |
| 245 | it('Verify ROUND_ROBIN strategy runtime behavior', async () => { |
| 246 | const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
| 247 | let pool = new FixedClusterPool( |
| 248 | max, |
| 249 | './tests/worker-files/cluster/testWorker.js', |
| 250 | { workerChoiceStrategy } |
| 251 | ) |
| 252 | let results = new Set() |
| 253 | for (let i = 0; i < max; i++) { |
| 254 | results.add(pool.workerNodes[pool.chooseWorkerNode()].worker.id) |
| 255 | } |
| 256 | expect(results.size).toBe(max) |
| 257 | await pool.destroy() |
| 258 | pool = new FixedThreadPool( |
| 259 | max, |
| 260 | './tests/worker-files/thread/testWorker.js', |
| 261 | { workerChoiceStrategy } |
| 262 | ) |
| 263 | results = new Set() |
| 264 | for (let i = 0; i < max; i++) { |
| 265 | results.add(pool.workerNodes[pool.chooseWorkerNode()].worker.threadId) |
| 266 | } |
| 267 | expect(results.size).toBe(max) |
| 268 | await pool.destroy() |
| 269 | }) |
| 270 | |
| 271 | it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => { |
| 272 | const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
| 273 | let pool = new FixedThreadPool( |
| 274 | max, |
| 275 | './tests/worker-files/thread/testWorker.js', |
| 276 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } |
| 277 | ) |
| 278 | expect( |
| 279 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 280 | workerChoiceStrategy |
| 281 | ).nextWorkerNodeId |
| 282 | ).toBeDefined() |
| 283 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 284 | expect( |
| 285 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 286 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 287 | ).nextWorkerNodeId |
| 288 | ).toBe(0) |
| 289 | await pool.destroy() |
| 290 | pool = new DynamicThreadPool( |
| 291 | min, |
| 292 | max, |
| 293 | './tests/worker-files/thread/testWorker.js', |
| 294 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } |
| 295 | ) |
| 296 | expect( |
| 297 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 298 | workerChoiceStrategy |
| 299 | ).nextWorkerNodeId |
| 300 | ).toBeDefined() |
| 301 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 302 | expect( |
| 303 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 304 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 305 | ).nextWorkerNodeId |
| 306 | ).toBe(0) |
| 307 | // We need to clean up the resources after our test |
| 308 | await pool.destroy() |
| 309 | }) |
| 310 | |
| 311 | it('Verify LEAST_USED strategy default tasks usage statistics requirements', async () => { |
| 312 | const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED |
| 313 | let pool = new FixedThreadPool( |
| 314 | max, |
| 315 | './tests/worker-files/thread/testWorker.js', |
| 316 | { workerChoiceStrategy } |
| 317 | ) |
| 318 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 319 | runTime: false, |
| 320 | avgRunTime: false, |
| 321 | medRunTime: false, |
| 322 | waitTime: false, |
| 323 | avgWaitTime: false, |
| 324 | medWaitTime: false, |
| 325 | elu: false |
| 326 | }) |
| 327 | await pool.destroy() |
| 328 | pool = new DynamicThreadPool( |
| 329 | min, |
| 330 | max, |
| 331 | './tests/worker-files/thread/testWorker.js', |
| 332 | { workerChoiceStrategy } |
| 333 | ) |
| 334 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 335 | runTime: false, |
| 336 | avgRunTime: false, |
| 337 | medRunTime: false, |
| 338 | waitTime: false, |
| 339 | avgWaitTime: false, |
| 340 | medWaitTime: false, |
| 341 | elu: false |
| 342 | }) |
| 343 | // We need to clean up the resources after our test |
| 344 | await pool.destroy() |
| 345 | }) |
| 346 | |
| 347 | it('Verify LEAST_USED strategy can be run in a fixed pool', async () => { |
| 348 | const pool = new FixedThreadPool( |
| 349 | max, |
| 350 | './tests/worker-files/thread/testWorker.js', |
| 351 | { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED } |
| 352 | ) |
| 353 | // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose` |
| 354 | const promises = new Set() |
| 355 | const maxMultiplier = 2 |
| 356 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 357 | promises.add(pool.execute()) |
| 358 | } |
| 359 | await Promise.all(promises) |
| 360 | for (const workerNode of pool.workerNodes) { |
| 361 | expect(workerNode.workerUsage).toStrictEqual({ |
| 362 | tasks: { |
| 363 | executed: maxMultiplier, |
| 364 | executing: 0, |
| 365 | queued: 0, |
| 366 | failed: 0 |
| 367 | }, |
| 368 | runTime: { |
| 369 | aggregation: 0, |
| 370 | average: 0, |
| 371 | median: 0, |
| 372 | history: expect.any(CircularArray) |
| 373 | }, |
| 374 | waitTime: { |
| 375 | aggregation: 0, |
| 376 | average: 0, |
| 377 | median: 0, |
| 378 | history: expect.any(CircularArray) |
| 379 | }, |
| 380 | elu: undefined |
| 381 | }) |
| 382 | } |
| 383 | // We need to clean up the resources after our test |
| 384 | await pool.destroy() |
| 385 | }) |
| 386 | |
| 387 | it('Verify LEAST_USED strategy can be run in a dynamic pool', async () => { |
| 388 | const pool = new DynamicThreadPool( |
| 389 | min, |
| 390 | max, |
| 391 | './tests/worker-files/thread/testWorker.js', |
| 392 | { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED } |
| 393 | ) |
| 394 | // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose` |
| 395 | const promises = new Set() |
| 396 | const maxMultiplier = 2 |
| 397 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 398 | promises.add(pool.execute()) |
| 399 | } |
| 400 | await Promise.all(promises) |
| 401 | for (const workerNode of pool.workerNodes) { |
| 402 | expect(workerNode.workerUsage).toStrictEqual({ |
| 403 | tasks: { |
| 404 | executed: maxMultiplier, |
| 405 | executing: 0, |
| 406 | queued: 0, |
| 407 | failed: 0 |
| 408 | }, |
| 409 | runTime: { |
| 410 | aggregation: 0, |
| 411 | average: 0, |
| 412 | median: 0, |
| 413 | history: expect.any(CircularArray) |
| 414 | }, |
| 415 | waitTime: { |
| 416 | aggregation: 0, |
| 417 | average: 0, |
| 418 | median: 0, |
| 419 | history: expect.any(CircularArray) |
| 420 | }, |
| 421 | |
| 422 | elu: undefined |
| 423 | }) |
| 424 | } |
| 425 | // We need to clean up the resources after our test |
| 426 | await pool.destroy() |
| 427 | }) |
| 428 | |
| 429 | it('Verify LEAST_BUSY strategy default tasks usage statistics requirements', async () => { |
| 430 | const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY |
| 431 | let pool = new FixedThreadPool( |
| 432 | max, |
| 433 | './tests/worker-files/thread/testWorker.js', |
| 434 | { workerChoiceStrategy } |
| 435 | ) |
| 436 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 437 | runTime: true, |
| 438 | avgRunTime: false, |
| 439 | medRunTime: false, |
| 440 | waitTime: false, |
| 441 | avgWaitTime: false, |
| 442 | medWaitTime: false, |
| 443 | elu: false |
| 444 | }) |
| 445 | await pool.destroy() |
| 446 | pool = new DynamicThreadPool( |
| 447 | min, |
| 448 | max, |
| 449 | './tests/worker-files/thread/testWorker.js', |
| 450 | { workerChoiceStrategy } |
| 451 | ) |
| 452 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 453 | runTime: true, |
| 454 | avgRunTime: false, |
| 455 | medRunTime: false, |
| 456 | waitTime: false, |
| 457 | avgWaitTime: false, |
| 458 | medWaitTime: false, |
| 459 | elu: false |
| 460 | }) |
| 461 | // We need to clean up the resources after our test |
| 462 | await pool.destroy() |
| 463 | }) |
| 464 | |
| 465 | it('Verify LEAST_BUSY strategy can be run in a fixed pool', async () => { |
| 466 | const pool = new FixedThreadPool( |
| 467 | max, |
| 468 | './tests/worker-files/thread/testWorker.js', |
| 469 | { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY } |
| 470 | ) |
| 471 | // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose` |
| 472 | const promises = new Set() |
| 473 | const maxMultiplier = 2 |
| 474 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 475 | promises.add(pool.execute()) |
| 476 | } |
| 477 | await Promise.all(promises) |
| 478 | for (const workerNode of pool.workerNodes) { |
| 479 | expect(workerNode.workerUsage).toStrictEqual({ |
| 480 | tasks: { |
| 481 | executed: expect.any(Number), |
| 482 | executing: 0, |
| 483 | queued: 0, |
| 484 | failed: 0 |
| 485 | }, |
| 486 | runTime: { |
| 487 | aggregation: expect.any(Number), |
| 488 | average: 0, |
| 489 | median: 0, |
| 490 | history: expect.any(CircularArray) |
| 491 | }, |
| 492 | waitTime: { |
| 493 | aggregation: 0, |
| 494 | average: 0, |
| 495 | median: 0, |
| 496 | history: expect.any(CircularArray) |
| 497 | }, |
| 498 | elu: undefined |
| 499 | }) |
| 500 | expect(workerNode.workerUsage.tasks.executed).toBeGreaterThanOrEqual(0) |
| 501 | expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( |
| 502 | max * maxMultiplier |
| 503 | ) |
| 504 | expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThanOrEqual( |
| 505 | 0 |
| 506 | ) |
| 507 | } |
| 508 | // We need to clean up the resources after our test |
| 509 | await pool.destroy() |
| 510 | }) |
| 511 | |
| 512 | it('Verify LEAST_BUSY strategy can be run in a dynamic pool', async () => { |
| 513 | const pool = new DynamicThreadPool( |
| 514 | min, |
| 515 | max, |
| 516 | './tests/worker-files/thread/testWorker.js', |
| 517 | { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY } |
| 518 | ) |
| 519 | // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose` |
| 520 | const promises = new Set() |
| 521 | const maxMultiplier = 2 |
| 522 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 523 | promises.add(pool.execute()) |
| 524 | } |
| 525 | await Promise.all(promises) |
| 526 | for (const workerNode of pool.workerNodes) { |
| 527 | expect(workerNode.workerUsage).toStrictEqual({ |
| 528 | tasks: { |
| 529 | executed: expect.any(Number), |
| 530 | executing: 0, |
| 531 | queued: 0, |
| 532 | failed: 0 |
| 533 | }, |
| 534 | runTime: { |
| 535 | aggregation: expect.any(Number), |
| 536 | average: 0, |
| 537 | median: 0, |
| 538 | history: expect.any(CircularArray) |
| 539 | }, |
| 540 | waitTime: { |
| 541 | aggregation: 0, |
| 542 | average: 0, |
| 543 | median: 0, |
| 544 | history: expect.any(CircularArray) |
| 545 | }, |
| 546 | elu: undefined |
| 547 | }) |
| 548 | expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0) |
| 549 | expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( |
| 550 | max * maxMultiplier |
| 551 | ) |
| 552 | expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) |
| 553 | } |
| 554 | // We need to clean up the resources after our test |
| 555 | await pool.destroy() |
| 556 | }) |
| 557 | |
| 558 | it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => { |
| 559 | const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE |
| 560 | let pool = new FixedThreadPool( |
| 561 | max, |
| 562 | './tests/worker-files/thread/testWorker.js', |
| 563 | { workerChoiceStrategy } |
| 564 | ) |
| 565 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 566 | runTime: true, |
| 567 | avgRunTime: true, |
| 568 | medRunTime: false, |
| 569 | waitTime: false, |
| 570 | avgWaitTime: false, |
| 571 | medWaitTime: false, |
| 572 | elu: false |
| 573 | }) |
| 574 | await pool.destroy() |
| 575 | pool = new DynamicThreadPool( |
| 576 | min, |
| 577 | max, |
| 578 | './tests/worker-files/thread/testWorker.js', |
| 579 | { workerChoiceStrategy } |
| 580 | ) |
| 581 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 582 | runTime: true, |
| 583 | avgRunTime: true, |
| 584 | medRunTime: false, |
| 585 | waitTime: false, |
| 586 | avgWaitTime: false, |
| 587 | medWaitTime: false, |
| 588 | elu: false |
| 589 | }) |
| 590 | // We need to clean up the resources after our test |
| 591 | await pool.destroy() |
| 592 | }) |
| 593 | |
| 594 | it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => { |
| 595 | const pool = new FixedThreadPool( |
| 596 | max, |
| 597 | './tests/worker-files/thread/testWorker.js', |
| 598 | { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } |
| 599 | ) |
| 600 | // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` |
| 601 | const promises = new Set() |
| 602 | const maxMultiplier = 2 |
| 603 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 604 | promises.add(pool.execute()) |
| 605 | } |
| 606 | await Promise.all(promises) |
| 607 | for (const workerNode of pool.workerNodes) { |
| 608 | expect(workerNode.workerUsage).toStrictEqual({ |
| 609 | tasks: { |
| 610 | executed: maxMultiplier, |
| 611 | executing: 0, |
| 612 | queued: 0, |
| 613 | failed: 0 |
| 614 | }, |
| 615 | runTime: { |
| 616 | aggregation: expect.any(Number), |
| 617 | average: expect.any(Number), |
| 618 | median: 0, |
| 619 | history: expect.any(CircularArray) |
| 620 | }, |
| 621 | waitTime: { |
| 622 | aggregation: 0, |
| 623 | average: 0, |
| 624 | median: 0, |
| 625 | history: expect.any(CircularArray) |
| 626 | }, |
| 627 | elu: undefined |
| 628 | }) |
| 629 | expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) |
| 630 | expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0) |
| 631 | } |
| 632 | expect( |
| 633 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 634 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 635 | ).workersVirtualTaskEndTimestamp.length |
| 636 | ).toBe(pool.workerNodes.length) |
| 637 | // We need to clean up the resources after our test |
| 638 | await pool.destroy() |
| 639 | }) |
| 640 | |
| 641 | it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => { |
| 642 | const pool = new DynamicThreadPool( |
| 643 | min, |
| 644 | max, |
| 645 | './tests/worker-files/thread/testWorker.js', |
| 646 | { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } |
| 647 | ) |
| 648 | // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` |
| 649 | const promises = new Set() |
| 650 | const maxMultiplier = 2 |
| 651 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 652 | promises.add(pool.execute()) |
| 653 | } |
| 654 | await Promise.all(promises) |
| 655 | for (const workerNode of pool.workerNodes) { |
| 656 | expect(workerNode.workerUsage).toStrictEqual({ |
| 657 | tasks: { |
| 658 | executed: maxMultiplier, |
| 659 | executing: 0, |
| 660 | queued: 0, |
| 661 | failed: 0 |
| 662 | }, |
| 663 | runTime: { |
| 664 | aggregation: expect.any(Number), |
| 665 | average: expect.any(Number), |
| 666 | median: 0, |
| 667 | history: expect.any(CircularArray) |
| 668 | }, |
| 669 | waitTime: { |
| 670 | aggregation: 0, |
| 671 | average: 0, |
| 672 | median: 0, |
| 673 | history: expect.any(CircularArray) |
| 674 | }, |
| 675 | elu: undefined |
| 676 | }) |
| 677 | expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) |
| 678 | expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0) |
| 679 | } |
| 680 | expect( |
| 681 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 682 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 683 | ).workersVirtualTaskEndTimestamp.length |
| 684 | ).toBe(pool.workerNodes.length) |
| 685 | // We need to clean up the resources after our test |
| 686 | await pool.destroy() |
| 687 | }) |
| 688 | |
| 689 | it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median runtime statistic', async () => { |
| 690 | const pool = new DynamicThreadPool( |
| 691 | min, |
| 692 | max, |
| 693 | './tests/worker-files/thread/testWorker.js', |
| 694 | { |
| 695 | workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE, |
| 696 | workerChoiceStrategyOptions: { |
| 697 | medRunTime: true |
| 698 | } |
| 699 | } |
| 700 | ) |
| 701 | // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` |
| 702 | const promises = new Set() |
| 703 | const maxMultiplier = 2 |
| 704 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 705 | promises.add(pool.execute()) |
| 706 | } |
| 707 | await Promise.all(promises) |
| 708 | for (const workerNode of pool.workerNodes) { |
| 709 | expect(workerNode.workerUsage).toStrictEqual({ |
| 710 | tasks: { |
| 711 | executed: maxMultiplier, |
| 712 | executing: 0, |
| 713 | queued: 0, |
| 714 | failed: 0 |
| 715 | }, |
| 716 | runTime: { |
| 717 | aggregation: expect.any(Number), |
| 718 | average: 0, |
| 719 | median: expect.any(Number), |
| 720 | history: expect.any(CircularArray) |
| 721 | }, |
| 722 | waitTime: { |
| 723 | aggregation: 0, |
| 724 | average: 0, |
| 725 | median: 0, |
| 726 | history: expect.any(CircularArray) |
| 727 | }, |
| 728 | elu: undefined |
| 729 | }) |
| 730 | expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) |
| 731 | expect(workerNode.workerUsage.runTime.median).toBeGreaterThan(0) |
| 732 | } |
| 733 | expect( |
| 734 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 735 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 736 | ).workersVirtualTaskEndTimestamp.length |
| 737 | ).toBe(pool.workerNodes.length) |
| 738 | // We need to clean up the resources after our test |
| 739 | await pool.destroy() |
| 740 | }) |
| 741 | |
| 742 | it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => { |
| 743 | const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE |
| 744 | let pool = new FixedThreadPool( |
| 745 | max, |
| 746 | './tests/worker-files/thread/testWorker.js' |
| 747 | ) |
| 748 | expect( |
| 749 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 750 | workerChoiceStrategy |
| 751 | ).workersVirtualTaskEndTimestamp |
| 752 | ).toBeInstanceOf(Array) |
| 753 | expect( |
| 754 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 755 | workerChoiceStrategy |
| 756 | ).workersVirtualTaskEndTimestamp.length |
| 757 | ).toBe(0) |
| 758 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 759 | workerChoiceStrategy |
| 760 | ).workersVirtualTaskEndTimestamp[0] = performance.now() |
| 761 | expect( |
| 762 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 763 | workerChoiceStrategy |
| 764 | ).workersVirtualTaskEndTimestamp.length |
| 765 | ).toBe(1) |
| 766 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 767 | expect( |
| 768 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 769 | workerChoiceStrategy |
| 770 | ).workersVirtualTaskEndTimestamp |
| 771 | ).toBeInstanceOf(Array) |
| 772 | expect( |
| 773 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 774 | workerChoiceStrategy |
| 775 | ).workersVirtualTaskEndTimestamp.length |
| 776 | ).toBe(0) |
| 777 | await pool.destroy() |
| 778 | pool = new DynamicThreadPool( |
| 779 | min, |
| 780 | max, |
| 781 | './tests/worker-files/thread/testWorker.js' |
| 782 | ) |
| 783 | expect( |
| 784 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 785 | workerChoiceStrategy |
| 786 | ).workersVirtualTaskEndTimestamp |
| 787 | ).toBeInstanceOf(Array) |
| 788 | expect( |
| 789 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 790 | workerChoiceStrategy |
| 791 | ).workersVirtualTaskEndTimestamp.length |
| 792 | ).toBe(0) |
| 793 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 794 | workerChoiceStrategy |
| 795 | ).workersVirtualTaskEndTimestamp[0] = performance.now() |
| 796 | expect( |
| 797 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 798 | workerChoiceStrategy |
| 799 | ).workersVirtualTaskEndTimestamp.length |
| 800 | ).toBe(1) |
| 801 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 802 | expect( |
| 803 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 804 | workerChoiceStrategy |
| 805 | ).workersVirtualTaskEndTimestamp |
| 806 | ).toBeInstanceOf(Array) |
| 807 | expect( |
| 808 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 809 | workerChoiceStrategy |
| 810 | ).workersVirtualTaskEndTimestamp.length |
| 811 | ).toBe(0) |
| 812 | // We need to clean up the resources after our test |
| 813 | await pool.destroy() |
| 814 | }) |
| 815 | |
| 816 | it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { |
| 817 | const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 818 | let pool = new FixedThreadPool( |
| 819 | max, |
| 820 | './tests/worker-files/thread/testWorker.js', |
| 821 | { workerChoiceStrategy } |
| 822 | ) |
| 823 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 824 | runTime: true, |
| 825 | avgRunTime: true, |
| 826 | medRunTime: false, |
| 827 | waitTime: false, |
| 828 | avgWaitTime: false, |
| 829 | medWaitTime: false, |
| 830 | elu: false |
| 831 | }) |
| 832 | await pool.destroy() |
| 833 | pool = new DynamicThreadPool( |
| 834 | min, |
| 835 | max, |
| 836 | './tests/worker-files/thread/testWorker.js', |
| 837 | { workerChoiceStrategy } |
| 838 | ) |
| 839 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 840 | runTime: true, |
| 841 | avgRunTime: true, |
| 842 | medRunTime: false, |
| 843 | waitTime: false, |
| 844 | avgWaitTime: false, |
| 845 | medWaitTime: false, |
| 846 | elu: false |
| 847 | }) |
| 848 | // We need to clean up the resources after our test |
| 849 | await pool.destroy() |
| 850 | }) |
| 851 | |
| 852 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => { |
| 853 | const pool = new FixedThreadPool( |
| 854 | max, |
| 855 | './tests/worker-files/thread/testWorker.js', |
| 856 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } |
| 857 | ) |
| 858 | // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` |
| 859 | const promises = new Set() |
| 860 | const maxMultiplier = 2 |
| 861 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 862 | promises.add(pool.execute()) |
| 863 | } |
| 864 | await Promise.all(promises) |
| 865 | for (const workerNode of pool.workerNodes) { |
| 866 | expect(workerNode.workerUsage).toStrictEqual({ |
| 867 | tasks: { |
| 868 | executed: expect.any(Number), |
| 869 | executing: 0, |
| 870 | queued: 0, |
| 871 | failed: 0 |
| 872 | }, |
| 873 | runTime: { |
| 874 | aggregation: expect.any(Number), |
| 875 | average: expect.any(Number), |
| 876 | median: 0, |
| 877 | history: expect.any(CircularArray) |
| 878 | }, |
| 879 | waitTime: { |
| 880 | aggregation: 0, |
| 881 | average: 0, |
| 882 | median: 0, |
| 883 | history: expect.any(CircularArray) |
| 884 | }, |
| 885 | elu: undefined |
| 886 | }) |
| 887 | expect(workerNode.workerUsage.tasks.executed).toBeGreaterThanOrEqual(0) |
| 888 | expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( |
| 889 | max * maxMultiplier |
| 890 | ) |
| 891 | expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThanOrEqual( |
| 892 | 0 |
| 893 | ) |
| 894 | expect(workerNode.workerUsage.runTime.average).toBeGreaterThanOrEqual(0) |
| 895 | } |
| 896 | expect( |
| 897 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 898 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 899 | ).defaultWorkerWeight |
| 900 | ).toBeGreaterThan(0) |
| 901 | expect( |
| 902 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 903 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 904 | ).workerVirtualTaskRunTime |
| 905 | ).toBeGreaterThanOrEqual(0) |
| 906 | // We need to clean up the resources after our test |
| 907 | await pool.destroy() |
| 908 | }) |
| 909 | |
| 910 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => { |
| 911 | const pool = new DynamicThreadPool( |
| 912 | min, |
| 913 | max, |
| 914 | './tests/worker-files/thread/testWorker.js', |
| 915 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } |
| 916 | ) |
| 917 | // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` |
| 918 | const promises = new Set() |
| 919 | const maxMultiplier = 2 |
| 920 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 921 | promises.add(pool.execute()) |
| 922 | } |
| 923 | await Promise.all(promises) |
| 924 | for (const workerNode of pool.workerNodes) { |
| 925 | expect(workerNode.workerUsage).toStrictEqual({ |
| 926 | tasks: { |
| 927 | executed: expect.any(Number), |
| 928 | executing: 0, |
| 929 | queued: 0, |
| 930 | failed: 0 |
| 931 | }, |
| 932 | runTime: { |
| 933 | aggregation: expect.any(Number), |
| 934 | average: expect.any(Number), |
| 935 | median: 0, |
| 936 | history: expect.any(CircularArray) |
| 937 | }, |
| 938 | waitTime: { |
| 939 | aggregation: 0, |
| 940 | average: 0, |
| 941 | median: 0, |
| 942 | history: expect.any(CircularArray) |
| 943 | }, |
| 944 | elu: undefined |
| 945 | }) |
| 946 | expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0) |
| 947 | expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( |
| 948 | max * maxMultiplier |
| 949 | ) |
| 950 | expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) |
| 951 | expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0) |
| 952 | } |
| 953 | expect( |
| 954 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 955 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 956 | ).defaultWorkerWeight |
| 957 | ).toBeGreaterThan(0) |
| 958 | expect( |
| 959 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 960 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 961 | ).workerVirtualTaskRunTime |
| 962 | ).toBeGreaterThanOrEqual(0) |
| 963 | // We need to clean up the resources after our test |
| 964 | await pool.destroy() |
| 965 | }) |
| 966 | |
| 967 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool with median runtime statistic', async () => { |
| 968 | const pool = new DynamicThreadPool( |
| 969 | min, |
| 970 | max, |
| 971 | './tests/worker-files/thread/testWorker.js', |
| 972 | { |
| 973 | workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, |
| 974 | workerChoiceStrategyOptions: { |
| 975 | medRunTime: true |
| 976 | } |
| 977 | } |
| 978 | ) |
| 979 | // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` |
| 980 | const promises = new Set() |
| 981 | const maxMultiplier = 2 |
| 982 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 983 | promises.add(pool.execute()) |
| 984 | } |
| 985 | await Promise.all(promises) |
| 986 | for (const workerNode of pool.workerNodes) { |
| 987 | expect(workerNode.workerUsage).toStrictEqual({ |
| 988 | tasks: { |
| 989 | executed: expect.any(Number), |
| 990 | executing: 0, |
| 991 | queued: 0, |
| 992 | failed: 0 |
| 993 | }, |
| 994 | runTime: { |
| 995 | aggregation: expect.any(Number), |
| 996 | average: 0, |
| 997 | median: expect.any(Number), |
| 998 | history: expect.any(CircularArray) |
| 999 | }, |
| 1000 | waitTime: { |
| 1001 | aggregation: 0, |
| 1002 | average: 0, |
| 1003 | median: 0, |
| 1004 | history: expect.any(CircularArray) |
| 1005 | }, |
| 1006 | elu: undefined |
| 1007 | }) |
| 1008 | expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0) |
| 1009 | expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( |
| 1010 | max * maxMultiplier |
| 1011 | ) |
| 1012 | expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) |
| 1013 | expect(workerNode.workerUsage.runTime.median).toBeGreaterThan(0) |
| 1014 | } |
| 1015 | expect( |
| 1016 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1017 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1018 | ).defaultWorkerWeight |
| 1019 | ).toBeGreaterThan(0) |
| 1020 | expect( |
| 1021 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1022 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1023 | ).workerVirtualTaskRunTime |
| 1024 | ).toBeGreaterThanOrEqual(0) |
| 1025 | // We need to clean up the resources after our test |
| 1026 | await pool.destroy() |
| 1027 | }) |
| 1028 | |
| 1029 | it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => { |
| 1030 | const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 1031 | let pool = new FixedThreadPool( |
| 1032 | max, |
| 1033 | './tests/worker-files/thread/testWorker.js' |
| 1034 | ) |
| 1035 | expect( |
| 1036 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1037 | workerChoiceStrategy |
| 1038 | ).currentWorkerNodeId |
| 1039 | ).toBeDefined() |
| 1040 | expect( |
| 1041 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1042 | workerChoiceStrategy |
| 1043 | ).defaultWorkerWeight |
| 1044 | ).toBeDefined() |
| 1045 | expect( |
| 1046 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1047 | workerChoiceStrategy |
| 1048 | ).workerVirtualTaskRunTime |
| 1049 | ).toBeDefined() |
| 1050 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 1051 | expect( |
| 1052 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1053 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1054 | ).currentWorkerNodeId |
| 1055 | ).toBe(0) |
| 1056 | expect( |
| 1057 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1058 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1059 | ).defaultWorkerWeight |
| 1060 | ).toBeGreaterThan(0) |
| 1061 | expect( |
| 1062 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1063 | workerChoiceStrategy |
| 1064 | ).workerVirtualTaskRunTime |
| 1065 | ).toBe(0) |
| 1066 | await pool.destroy() |
| 1067 | pool = new DynamicThreadPool( |
| 1068 | min, |
| 1069 | max, |
| 1070 | './tests/worker-files/thread/testWorker.js' |
| 1071 | ) |
| 1072 | expect( |
| 1073 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1074 | workerChoiceStrategy |
| 1075 | ).currentWorkerNodeId |
| 1076 | ).toBeDefined() |
| 1077 | expect( |
| 1078 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1079 | workerChoiceStrategy |
| 1080 | ).defaultWorkerWeight |
| 1081 | ).toBeDefined() |
| 1082 | expect( |
| 1083 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1084 | workerChoiceStrategy |
| 1085 | ).workerVirtualTaskRunTime |
| 1086 | ).toBeDefined() |
| 1087 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 1088 | expect( |
| 1089 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1090 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1091 | ).currentWorkerNodeId |
| 1092 | ).toBe(0) |
| 1093 | expect( |
| 1094 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1095 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1096 | ).defaultWorkerWeight |
| 1097 | ).toBeGreaterThan(0) |
| 1098 | expect( |
| 1099 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1100 | workerChoiceStrategy |
| 1101 | ).workerVirtualTaskRunTime |
| 1102 | ).toBe(0) |
| 1103 | // We need to clean up the resources after our test |
| 1104 | await pool.destroy() |
| 1105 | }) |
| 1106 | |
| 1107 | it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { |
| 1108 | const workerChoiceStrategy = |
| 1109 | WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN |
| 1110 | let pool = new FixedThreadPool( |
| 1111 | max, |
| 1112 | './tests/worker-files/thread/testWorker.js', |
| 1113 | { workerChoiceStrategy } |
| 1114 | ) |
| 1115 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 1116 | runTime: false, |
| 1117 | avgRunTime: false, |
| 1118 | medRunTime: false, |
| 1119 | waitTime: false, |
| 1120 | avgWaitTime: false, |
| 1121 | medWaitTime: false, |
| 1122 | elu: false |
| 1123 | }) |
| 1124 | await pool.destroy() |
| 1125 | pool = new DynamicThreadPool( |
| 1126 | min, |
| 1127 | max, |
| 1128 | './tests/worker-files/thread/testWorker.js', |
| 1129 | { workerChoiceStrategy } |
| 1130 | ) |
| 1131 | expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ |
| 1132 | runTime: false, |
| 1133 | avgRunTime: false, |
| 1134 | medRunTime: false, |
| 1135 | waitTime: false, |
| 1136 | avgWaitTime: false, |
| 1137 | medWaitTime: false, |
| 1138 | elu: false |
| 1139 | }) |
| 1140 | // We need to clean up the resources after our test |
| 1141 | await pool.destroy() |
| 1142 | }) |
| 1143 | |
| 1144 | it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => { |
| 1145 | const pool = new FixedThreadPool( |
| 1146 | max, |
| 1147 | './tests/worker-files/thread/testWorker.js', |
| 1148 | { |
| 1149 | workerChoiceStrategy: |
| 1150 | WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN |
| 1151 | } |
| 1152 | ) |
| 1153 | // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose` |
| 1154 | const promises = new Set() |
| 1155 | const maxMultiplier = 2 |
| 1156 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 1157 | promises.add(pool.execute()) |
| 1158 | } |
| 1159 | await Promise.all(promises) |
| 1160 | for (const workerNode of pool.workerNodes) { |
| 1161 | expect(workerNode.workerUsage).toStrictEqual({ |
| 1162 | tasks: { |
| 1163 | executed: maxMultiplier, |
| 1164 | executing: 0, |
| 1165 | queued: 0, |
| 1166 | failed: 0 |
| 1167 | }, |
| 1168 | runTime: { |
| 1169 | aggregation: 0, |
| 1170 | average: 0, |
| 1171 | median: 0, |
| 1172 | history: expect.any(CircularArray) |
| 1173 | }, |
| 1174 | waitTime: { |
| 1175 | aggregation: 0, |
| 1176 | average: 0, |
| 1177 | median: 0, |
| 1178 | history: expect.any(CircularArray) |
| 1179 | }, |
| 1180 | elu: undefined |
| 1181 | }) |
| 1182 | } |
| 1183 | expect( |
| 1184 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1185 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1186 | ).defaultWorkerWeight |
| 1187 | ).toBeGreaterThan(0) |
| 1188 | expect( |
| 1189 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1190 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1191 | ).currentRoundId |
| 1192 | ).toBe(0) |
| 1193 | expect( |
| 1194 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1195 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1196 | ).currentWorkerNodeId |
| 1197 | ).toBe(0) |
| 1198 | expect( |
| 1199 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1200 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1201 | ).roundWeights |
| 1202 | ).toStrictEqual([ |
| 1203 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1204 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1205 | ).defaultWorkerWeight |
| 1206 | ]) |
| 1207 | // We need to clean up the resources after our test |
| 1208 | await pool.destroy() |
| 1209 | }) |
| 1210 | |
| 1211 | it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => { |
| 1212 | const pool = new DynamicThreadPool( |
| 1213 | min, |
| 1214 | max, |
| 1215 | './tests/worker-files/thread/testWorker.js', |
| 1216 | { |
| 1217 | workerChoiceStrategy: |
| 1218 | WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN |
| 1219 | } |
| 1220 | ) |
| 1221 | // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose` |
| 1222 | const promises = new Set() |
| 1223 | const maxMultiplier = 2 |
| 1224 | for (let i = 0; i < max * maxMultiplier; i++) { |
| 1225 | promises.add(pool.execute()) |
| 1226 | } |
| 1227 | await Promise.all(promises) |
| 1228 | for (const workerNode of pool.workerNodes) { |
| 1229 | expect(workerNode.workerUsage).toStrictEqual({ |
| 1230 | tasks: { |
| 1231 | executed: maxMultiplier, |
| 1232 | executing: 0, |
| 1233 | queued: 0, |
| 1234 | failed: 0 |
| 1235 | }, |
| 1236 | runTime: { |
| 1237 | aggregation: 0, |
| 1238 | average: 0, |
| 1239 | median: 0, |
| 1240 | history: expect.any(CircularArray) |
| 1241 | }, |
| 1242 | waitTime: { |
| 1243 | aggregation: 0, |
| 1244 | average: 0, |
| 1245 | median: 0, |
| 1246 | history: expect.any(CircularArray) |
| 1247 | }, |
| 1248 | elu: undefined |
| 1249 | }) |
| 1250 | } |
| 1251 | expect( |
| 1252 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1253 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1254 | ).defaultWorkerWeight |
| 1255 | ).toBeGreaterThan(0) |
| 1256 | expect( |
| 1257 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1258 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1259 | ).currentRoundId |
| 1260 | ).toBe(0) |
| 1261 | expect( |
| 1262 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1263 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1264 | ).currentWorkerNodeId |
| 1265 | ).toBe(0) |
| 1266 | expect( |
| 1267 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1268 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1269 | ).roundWeights |
| 1270 | ).toStrictEqual([ |
| 1271 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1272 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1273 | ).defaultWorkerWeight |
| 1274 | ]) |
| 1275 | // We need to clean up the resources after our test |
| 1276 | await pool.destroy() |
| 1277 | }) |
| 1278 | |
| 1279 | it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => { |
| 1280 | const workerChoiceStrategy = |
| 1281 | WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN |
| 1282 | let pool = new FixedThreadPool( |
| 1283 | max, |
| 1284 | './tests/worker-files/thread/testWorker.js' |
| 1285 | ) |
| 1286 | expect( |
| 1287 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1288 | workerChoiceStrategy |
| 1289 | ).currentRoundId |
| 1290 | ).toBeDefined() |
| 1291 | expect( |
| 1292 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1293 | workerChoiceStrategy |
| 1294 | ).currentWorkerNodeId |
| 1295 | ).toBeDefined() |
| 1296 | expect( |
| 1297 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1298 | workerChoiceStrategy |
| 1299 | ).defaultWorkerWeight |
| 1300 | ).toBeDefined() |
| 1301 | expect( |
| 1302 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1303 | workerChoiceStrategy |
| 1304 | ).roundWeights |
| 1305 | ).toBeDefined() |
| 1306 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 1307 | expect( |
| 1308 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1309 | workerChoiceStrategy |
| 1310 | ).currentRoundId |
| 1311 | ).toBe(0) |
| 1312 | expect( |
| 1313 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1314 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1315 | ).currentWorkerNodeId |
| 1316 | ).toBe(0) |
| 1317 | expect( |
| 1318 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1319 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1320 | ).defaultWorkerWeight |
| 1321 | ).toBeGreaterThan(0) |
| 1322 | expect( |
| 1323 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1324 | workerChoiceStrategy |
| 1325 | ).roundWeights |
| 1326 | ).toStrictEqual([ |
| 1327 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1328 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1329 | ).defaultWorkerWeight |
| 1330 | ]) |
| 1331 | await pool.destroy() |
| 1332 | pool = new DynamicThreadPool( |
| 1333 | min, |
| 1334 | max, |
| 1335 | './tests/worker-files/thread/testWorker.js' |
| 1336 | ) |
| 1337 | expect( |
| 1338 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1339 | workerChoiceStrategy |
| 1340 | ).currentRoundId |
| 1341 | ).toBeDefined() |
| 1342 | expect( |
| 1343 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1344 | workerChoiceStrategy |
| 1345 | ).currentWorkerNodeId |
| 1346 | ).toBeDefined() |
| 1347 | expect( |
| 1348 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1349 | workerChoiceStrategy |
| 1350 | ).defaultWorkerWeight |
| 1351 | ).toBeDefined() |
| 1352 | expect( |
| 1353 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1354 | workerChoiceStrategy |
| 1355 | ).roundWeights |
| 1356 | ).toBeDefined() |
| 1357 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 1358 | expect( |
| 1359 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1360 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1361 | ).currentWorkerNodeId |
| 1362 | ).toBe(0) |
| 1363 | expect( |
| 1364 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1365 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1366 | ).defaultWorkerWeight |
| 1367 | ).toBeGreaterThan(0) |
| 1368 | expect( |
| 1369 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1370 | workerChoiceStrategy |
| 1371 | ).roundWeights |
| 1372 | ).toStrictEqual([ |
| 1373 | pool.workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 1374 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
| 1375 | ).defaultWorkerWeight |
| 1376 | ]) |
| 1377 | // We need to clean up the resources after our test |
| 1378 | await pool.destroy() |
| 1379 | }) |
| 1380 | |
| 1381 | it('Verify unknown strategy throw error', () => { |
| 1382 | expect( |
| 1383 | () => |
| 1384 | new DynamicThreadPool( |
| 1385 | min, |
| 1386 | max, |
| 1387 | './tests/worker-files/thread/testWorker.js', |
| 1388 | { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } |
| 1389 | ) |
| 1390 | ).toThrowError("Invalid worker choice strategy 'UNKNOWN_STRATEGY'") |
| 1391 | }) |
| 1392 | }) |