testGroups.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createTestGroups = createTestGroups;
  6. exports.filterForShard = filterForShard;
  7. /**
  8. * Copyright Microsoft Corporation. All rights reserved.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. function createTestGroups(projectSuite, workers) {
  23. // This function groups tests that can be run together.
  24. // Tests cannot be run together when:
  25. // - They belong to different projects - requires different workers.
  26. // - They have a different repeatEachIndex - requires different workers.
  27. // - They have a different set of worker fixtures in the pool - requires different workers.
  28. // - They have a different requireFile - reuses the worker, but runs each requireFile separately.
  29. // - They belong to a parallel suite.
  30. // Using the map "workerHash -> requireFile -> group" makes us preserve the natural order
  31. // of worker hashes and require files for the simple cases.
  32. const groups = new Map();
  33. const createGroup = test => {
  34. return {
  35. workerHash: test._workerHash,
  36. requireFile: test._requireFile,
  37. repeatEachIndex: test.repeatEachIndex,
  38. projectId: test._projectId,
  39. tests: []
  40. };
  41. };
  42. for (const test of projectSuite.allTests()) {
  43. let withWorkerHash = groups.get(test._workerHash);
  44. if (!withWorkerHash) {
  45. withWorkerHash = new Map();
  46. groups.set(test._workerHash, withWorkerHash);
  47. }
  48. let withRequireFile = withWorkerHash.get(test._requireFile);
  49. if (!withRequireFile) {
  50. withRequireFile = {
  51. general: createGroup(test),
  52. parallel: new Map(),
  53. parallelWithHooks: createGroup(test)
  54. };
  55. withWorkerHash.set(test._requireFile, withRequireFile);
  56. }
  57. // Note that a parallel suite cannot be inside a serial suite. This is enforced in TestType.
  58. let insideParallel = false;
  59. let outerMostSequentialSuite;
  60. let hasAllHooks = false;
  61. for (let parent = test.parent; parent; parent = parent.parent) {
  62. if (parent._parallelMode === 'serial' || parent._parallelMode === 'default') outerMostSequentialSuite = parent;
  63. insideParallel = insideParallel || parent._parallelMode === 'parallel';
  64. hasAllHooks = hasAllHooks || parent._hooks.some(hook => hook.type === 'beforeAll' || hook.type === 'afterAll');
  65. }
  66. if (insideParallel) {
  67. if (hasAllHooks && !outerMostSequentialSuite) {
  68. withRequireFile.parallelWithHooks.tests.push(test);
  69. } else {
  70. const key = outerMostSequentialSuite || test;
  71. let group = withRequireFile.parallel.get(key);
  72. if (!group) {
  73. group = createGroup(test);
  74. withRequireFile.parallel.set(key, group);
  75. }
  76. group.tests.push(test);
  77. }
  78. } else {
  79. withRequireFile.general.tests.push(test);
  80. }
  81. }
  82. const result = [];
  83. for (const withWorkerHash of groups.values()) {
  84. for (const withRequireFile of withWorkerHash.values()) {
  85. // Tests without parallel mode should run serially as a single group.
  86. if (withRequireFile.general.tests.length) result.push(withRequireFile.general);
  87. // Parallel test groups without beforeAll/afterAll can be run independently.
  88. result.push(...withRequireFile.parallel.values());
  89. // Tests with beforeAll/afterAll should try to share workers as much as possible.
  90. const parallelWithHooksGroupSize = Math.ceil(withRequireFile.parallelWithHooks.tests.length / workers);
  91. let lastGroup;
  92. for (const test of withRequireFile.parallelWithHooks.tests) {
  93. if (!lastGroup || lastGroup.tests.length >= parallelWithHooksGroupSize) {
  94. lastGroup = createGroup(test);
  95. result.push(lastGroup);
  96. }
  97. lastGroup.tests.push(test);
  98. }
  99. }
  100. }
  101. return result;
  102. }
  103. function filterForShard(shard, testGroups) {
  104. // Note that sharding works based on test groups.
  105. // This means parallel files will be sharded by single tests,
  106. // while non-parallel files will be sharded by the whole file.
  107. //
  108. // Shards are still balanced by the number of tests, not files,
  109. // even in the case of non-paralleled files.
  110. let shardableTotal = 0;
  111. for (const group of testGroups) shardableTotal += group.tests.length;
  112. // Each shard gets some tests.
  113. const shardSize = Math.floor(shardableTotal / shard.total);
  114. // First few shards get one more test each.
  115. const extraOne = shardableTotal - shardSize * shard.total;
  116. const currentShard = shard.current - 1; // Make it zero-based for calculations.
  117. const from = shardSize * currentShard + Math.min(extraOne, currentShard);
  118. const to = from + shardSize + (currentShard < extraOne ? 1 : 0);
  119. let current = 0;
  120. const result = new Set();
  121. for (const group of testGroups) {
  122. // Any test group goes to the shard that contains the first test of this group.
  123. // So, this shard gets any group that starts at [from; to)
  124. if (current >= from && current < to) result.add(group);
  125. current += group.tests.length;
  126. }
  127. return result;
  128. }