parallel_closure_test_suite.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2015 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Parallel closure_test_suite test file. This test is not
  16. * intended to be ran or depended on directly.
  17. *
  18. */
  19. goog.module('goog.testing.parallelClosureTestSuite');
  20. goog.setTestOnly('goog.testing.parallelClosureTestSuite');
  21. var Promise = goog.require('goog.Promise');
  22. var events = goog.require('goog.events');
  23. var MultiTestRunner = goog.require('goog.testing.MultiTestRunner');
  24. var TestCase = goog.require('goog.testing.TestCase');
  25. var jsunit = goog.require('goog.testing.jsunit');
  26. var testSuite = goog.require('goog.testing.testSuite');
  27. var json = goog.require('goog.json');
  28. var testRunner;
  29. /**
  30. * @typedef {{
  31. * totalTests: number,
  32. * totalFailures: number,
  33. * failureReports: string,
  34. * allResults: !Object<string, !Array<string>>
  35. * }}
  36. */
  37. var ParallelTestResults;
  38. /**
  39. * Processes the test results returned from MultiTestRunner and creates a
  40. * consolidated result object that the test runner understands.
  41. * @param {!Array<!Object<string,!Array<string>>>} testResults The list of
  42. * individual test results from MultiTestRunner.
  43. * @return {!ParallelTestResults} Flattened test report for all tests.
  44. */
  45. function processAllTestResults(testResults) {
  46. var totalTests = 0;
  47. var totalFailed = 0;
  48. var allResults = {};
  49. var failureReports = '';
  50. for (var i = 0; i < testResults.length; i++) {
  51. var result = testResults[i];
  52. for (var testName in result) {
  53. totalTests++;
  54. allResults[testName] = result[testName];
  55. var failures = result[testName];
  56. if (failures.length) {
  57. totalFailed++;
  58. for (var j = 0; j < failures.length; j++) {
  59. failureReports += failures[j] + '\n';
  60. }
  61. }
  62. }
  63. }
  64. return {
  65. totalTests: totalTests,
  66. totalFailures: totalFailed,
  67. failureReports: failureReports,
  68. allResults: allResults
  69. };
  70. }
  71. var testObj = {
  72. setUpPage: function() {
  73. // G_parallelTestRunner is exported in gen_parallel_test_html.py.
  74. var timeout = goog.global['G_parallelTestRunner']['testTimeout'];
  75. var allTests = goog.global['G_parallelTestRunner']['allTests'];
  76. var parallelFrames = goog.global['G_parallelTestRunner']['parallelFrames'];
  77. var parallelTimeout =
  78. goog.global['G_parallelTestRunner']['parallelTimeout'];
  79. // Create a test runner and render it.
  80. testRunner = new MultiTestRunner()
  81. .setName(document.title)
  82. .setBasePath('/google3/')
  83. .setPoolSize(parallelFrames)
  84. .setStatsBucketSizes(5, 500)
  85. .setTimeout(timeout * 1000)
  86. .addTests(allTests);
  87. testRunner.render(document.getElementById('runner'));
  88. // There's only a single test method that runs all the tests, so this
  89. // promiseTimeout is effectively the timeout of the entire test suite
  90. TestCase.getActiveTestCase().promiseTimeout = parallelTimeout * 1000;
  91. // Return testRunner for testing purposes.
  92. return testRunner;
  93. },
  94. testRunAllTests: function() {
  95. var failurePromise = new Promise(function(resolve, reject) {
  96. events.listen(testRunner, 'testsFinished', resolve);
  97. });
  98. testRunner.start();
  99. var allResults = {};
  100. // TestPoller.java invokes this to get test results for sponge. We override
  101. // it and return the results of each individual test instead of the
  102. // containing "testRunAllTests".
  103. window['G_testRunner']['getTestResults'] = function() {
  104. return allResults;
  105. };
  106. window['G_testRunner']['getTestResultsAsJson'] = function() {
  107. return json.serialize(allResults);
  108. };
  109. return failurePromise.then(function(failures) {
  110. var testResults = processAllTestResults(failures['allTestResults']);
  111. allResults = testResults.allResults;
  112. if (testResults.totalFailures) {
  113. fail(
  114. testResults.totalFailures + ' of ' + testResults.totalTests +
  115. ' test(s) failed!\n\n' + testResults.failureReports);
  116. }
  117. });
  118. }
  119. };
  120. // G_parallelTestRunner should only be present when being run from a parallel
  121. // closure_test_suite target. If it's not present, we're including this file
  122. // to be unit tested.
  123. if (goog.global['G_parallelTestRunner']) {
  124. testSuite(testObj);
  125. }
  126. // Export test methods/vars so they can also be tested.
  127. testObj['processAllTestResults'] = processAllTestResults;
  128. exports = testObj;