| 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 |
/** |
| 16 |
* @fileoverview Parallel closure_test_suite test file. This test is not |
| 17 |
* intended to be ran or depended on directly. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.module('goog.testing.parallelClosureTestSuite'); |
| 22 |
goog.setTestOnly('goog.testing.parallelClosureTestSuite'); |
| 23 |
|
| 24 |
var Promise = goog.require('goog.Promise'); |
| 25 |
var events = goog.require('goog.events'); |
| 26 |
var MultiTestRunner = goog.require('goog.testing.MultiTestRunner'); |
| 27 |
var TestCase = goog.require('goog.testing.TestCase'); |
| 28 |
var jsunit = goog.require('goog.testing.jsunit'); |
| 29 |
var testSuite = goog.require('goog.testing.testSuite'); |
| 30 |
var json = goog.require('goog.json'); |
| 31 |
|
| 32 |
var testRunner; |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* @typedef {{ |
| 37 |
* totalTests: number, |
| 38 |
* totalFailures: number, |
| 39 |
* failureReports: string, |
| 40 |
* allResults: !Object<string, !Array<string>> |
| 41 |
* }} |
| 42 |
*/ |
| 43 |
var ParallelTestResults; |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Processes the test results returned from MultiTestRunner and creates a |
| 48 |
* consolidated result object that the test runner understands. |
| 49 |
* @param {!Array<!Object<string,!Array<string>>>} testResults The list of |
| 50 |
* individual test results from MultiTestRunner. |
| 51 |
* @return {!ParallelTestResults} Flattened test report for all tests. |
| 52 |
*/ |
| 53 |
function processAllTestResults(testResults) { |
| 54 |
var totalTests = 0; |
| 55 |
var totalFailed = 0; |
| 56 |
var allResults = {}; |
| 57 |
var failureReports = ''; |
| 58 |
|
| 59 |
for (var i = 0; i < testResults.length; i++) { |
| 60 |
var result = testResults[i]; |
| 61 |
for (var testName in result) { |
| 62 |
totalTests++; |
| 63 |
allResults[testName] = result[testName]; |
| 64 |
var failures = result[testName]; |
| 65 |
if (failures.length) { |
| 66 |
totalFailed++; |
| 67 |
for (var j = 0; j < failures.length; j++) { |
| 68 |
failureReports += failures[j] + '\n'; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return { |
| 75 |
totalTests: totalTests, |
| 76 |
totalFailures: totalFailed, |
| 77 |
failureReports: failureReports, |
| 78 |
allResults: allResults |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
var testObj = { |
| 83 |
setUpPage: function() { |
| 84 |
// G_parallelTestRunner is exported in gen_parallel_test_html.py. |
| 85 |
var timeout = goog.global['G_parallelTestRunner']['testTimeout']; |
| 86 |
var allTests = goog.global['G_parallelTestRunner']['allTests']; |
| 87 |
var parallelFrames = goog.global['G_parallelTestRunner']['parallelFrames']; |
| 88 |
var parallelTimeout = |
| 89 |
goog.global['G_parallelTestRunner']['parallelTimeout']; |
| 90 |
|
| 91 |
// Create a test runner and render it. |
| 92 |
testRunner = new MultiTestRunner() |
| 93 |
.setName(document.title) |
| 94 |
.setBasePath('/google3/') |
| 95 |
.setPoolSize(parallelFrames) |
| 96 |
.setStatsBucketSizes(5, 500) |
| 97 |
.setTimeout(timeout * 1000) |
| 98 |
.addTests(allTests); |
| 99 |
|
| 100 |
testRunner.render(document.getElementById('runner')); |
| 101 |
|
| 102 |
// There's only a single test method that runs all the tests, so this |
| 103 |
// promiseTimeout is effectively the timeout of the entire test suite |
| 104 |
TestCase.getActiveTestCase().promiseTimeout = parallelTimeout * 1000; |
| 105 |
|
| 106 |
// Return testRunner for testing purposes. |
| 107 |
return testRunner; |
| 108 |
}, |
| 109 |
|
| 110 |
testRunAllTests: function() { |
| 111 |
var failurePromise = new Promise(function(resolve, reject) { |
| 112 |
events.listen(testRunner, 'testsFinished', resolve); |
| 113 |
}); |
| 114 |
|
| 115 |
testRunner.start(); |
| 116 |
|
| 117 |
var allResults = {}; |
| 118 |
// TestPoller.java invokes this to get test results for sponge. We override |
| 119 |
// it and return the results of each individual test instead of the |
| 120 |
// containing "testRunAllTests". |
| 121 |
window['G_testRunner']['getTestResults'] = function() { |
| 122 |
return allResults; |
| 123 |
}; |
| 124 |
|
| 125 |
window['G_testRunner']['getTestResultsAsJson'] = function() { |
| 126 |
return json.serialize(allResults); |
| 127 |
}; |
| 128 |
|
| 129 |
return failurePromise.then(function(failures) { |
| 130 |
var testResults = processAllTestResults(failures['allTestResults']); |
| 131 |
allResults = testResults.allResults; |
| 132 |
if (testResults.totalFailures) { |
| 133 |
fail( |
| 134 |
testResults.totalFailures + ' of ' + testResults.totalTests + |
| 135 |
' test(s) failed!\n\n' + testResults.failureReports); |
| 136 |
} |
| 137 |
}); |
| 138 |
} |
| 139 |
}; |
| 140 |
|
| 141 |
// G_parallelTestRunner should only be present when being run from a parallel |
| 142 |
// closure_test_suite target. If it's not present, we're including this file |
| 143 |
// to be unit tested. |
| 144 |
if (goog.global['G_parallelTestRunner']) { |
| 145 |
testSuite(testObj); |
| 146 |
} |
| 147 |
|
| 148 |
// Export test methods/vars so they can also be tested. |
| 149 |
testObj['processAllTestResults'] = processAllTestResults; |
| 150 |
exports = testObj; |