(times, policy, options, allHosts, expectedHosts, permutations, done)
| 488 | }); |
| 489 | |
| 490 | function testRoundRobinPlan(times, policy, options, allHosts, expectedHosts, permutations, done) { |
| 491 | const client = options ? new Client(options) : null; |
| 492 | |
| 493 | policy.init(client, allHosts, function (err) { |
| 494 | assert.ifError(err); |
| 495 | let i = 0; |
| 496 | utils.map(new Array(times), function (n, next) { |
| 497 | n = i++; |
| 498 | policy.newQueryPlan(null, null, function(err, iterator) { |
| 499 | assert.ifError(err); |
| 500 | if (expectedHosts instanceof HostMap) { |
| 501 | expectedHosts = expectedHosts.values(); |
| 502 | } |
| 503 | utils.mapSeries(expectedHosts, function (planN, iteratorNext) { |
| 504 | const item = iterator.next(); |
| 505 | assert.strictEqual(item.done, false); |
| 506 | // Wait a random amount of time between executions to ensure |
| 507 | // sequence of query plan iteration does not impact other |
| 508 | // query plans. |
| 509 | const randomWait = Math.floor((Math.random() * 5) + 1); |
| 510 | setTimeout(function () { |
| 511 | iteratorNext(null, item.value); |
| 512 | }, randomWait); |
| 513 | }, function(err, planHosts) { |
| 514 | assert.ifError(err); |
| 515 | |
| 516 | // Ensure each host appears only once. |
| 517 | expectedHosts.forEach(function(host) { |
| 518 | let length = 0; |
| 519 | planHosts.forEach(function(planHost) { |
| 520 | length += (host === planHost ? 1 : 0); |
| 521 | }); |
| 522 | assert.strictEqual(1, length, |
| 523 | host + " appears " + length + " times in " |
| 524 | + planHosts + ". Expected only once."); |
| 525 | }); |
| 526 | next(err, {number: n, plan: planHosts}); |
| 527 | }); |
| 528 | }); |
| 529 | }, function (err, plans) { |
| 530 | assert.equal(err, null); |
| 531 | // Sort plans in order of creation (they are emitted by completion |
| 532 | // which is random). |
| 533 | plans.sort(function(a, b) { |
| 534 | return a.number - b.number; |
| 535 | }); |
| 536 | |
| 537 | assert.strictEqual(times, plans.length); |
| 538 | // Ensure each permutation happened the expected number of times |
| 539 | // (times / permutations) and never consecutively. |
| 540 | permutations.forEach(function(permutation) { |
| 541 | let length = 0; |
| 542 | let lastPlan = null; |
| 543 | plans.forEach(function(item) { |
| 544 | const planDesc = JSON.stringify(item.plan); |
| 545 | const permutationDesc = JSON.stringify(permutation); |
| 546 | length += (planDesc === permutationDesc ? 1 : 0); |
| 547 | assert.notEqual(lastPlan, planDesc, "last encountered plan is the" + |
no test coverage detected