| 1 |
// Copyright 2009 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 Utility for sharding tests. |
| 17 |
* |
| 18 |
* Usage instructions: |
| 19 |
* <ol> |
| 20 |
* <li>Instead of writing your large test in foo_test.html, write it in |
| 21 |
* foo_test_template.html</li> |
| 22 |
* <li>Add a call to {@code goog.testing.ShardingTestCase.shardByFileName()} |
| 23 |
* near the top of your test, before any test cases or setup methods.</li> |
| 24 |
* <li>Symlink foo_test_template.html into different sharded test files |
| 25 |
* named foo_1of4_test.html, foo_2of4_test.html, etc, using `ln -s`.</li> |
| 26 |
* <li>Add the symlinks as foo_1of4_test.html. |
| 27 |
* In perforce, run the command `g4 add foo_1of4_test.html` followed |
| 28 |
* by `g4 reopen -t symlink foo_1of4_test.html` so that perforce treats the file |
| 29 |
* as a symlink |
| 30 |
* </li> |
| 31 |
* </ol> |
| 32 |
* |
| 33 |
* @author [email protected] (Nick Santos) |
| 34 |
*/ |
| 35 |
|
| 36 |
goog.setTestOnly('goog.testing.ShardingTestCase'); |
| 37 |
goog.provide('goog.testing.ShardingTestCase'); |
| 38 |
|
| 39 |
goog.require('goog.asserts'); |
| 40 |
goog.require('goog.testing.TestCase'); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* A test case that runs tests in per-file shards. |
| 46 |
* @param {number} shardIndex Shard index for this page, |
| 47 |
* <strong>1-indexed</strong>. |
| 48 |
* @param {number} numShards Number of shards to split up test cases into. |
| 49 |
* @param {string=} opt_name The name of the test case. |
| 50 |
* @extends {goog.testing.TestCase} |
| 51 |
* @constructor |
| 52 |
* @final |
| 53 |
*/ |
| 54 |
goog.testing.ShardingTestCase = function(shardIndex, numShards, opt_name) { |
| 55 |
goog.testing.ShardingTestCase.base(this, 'constructor', opt_name); |
| 56 |
|
| 57 |
goog.asserts.assert(shardIndex > 0, 'Shard index should be positive'); |
| 58 |
goog.asserts.assert(numShards > 0, 'Number of shards should be positive'); |
| 59 |
goog.asserts.assert(shardIndex <= numShards, 'Shard index out of bounds'); |
| 60 |
|
| 61 |
/** |
| 62 |
* @type {number} |
| 63 |
* @private |
| 64 |
*/ |
| 65 |
this.shardIndex_ = shardIndex; |
| 66 |
|
| 67 |
/** |
| 68 |
* @type {number} |
| 69 |
* @private |
| 70 |
*/ |
| 71 |
this.numShards_ = numShards; |
| 72 |
}; |
| 73 |
goog.inherits(goog.testing.ShardingTestCase, goog.testing.TestCase); |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Whether we've actually partitioned the tests yet. We may execute twice |
| 78 |
* ('Run again without reloading') without failing. |
| 79 |
* @type {boolean} |
| 80 |
* @private |
| 81 |
*/ |
| 82 |
goog.testing.ShardingTestCase.prototype.sharded_ = false; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Installs a runTests global function that goog.testing.JsUnit will use to |
| 87 |
* run tests, which will run a single shard of the tests present on the page. |
| 88 |
* @override |
| 89 |
*/ |
| 90 |
goog.testing.ShardingTestCase.prototype.runTests = function() { |
| 91 |
if (!this.sharded_) { |
| 92 |
var numTests = this.getCount(); |
| 93 |
goog.asserts.assert( |
| 94 |
numTests >= this.numShards_, |
| 95 |
'Must have at least as many tests as shards!'); |
| 96 |
var shardSize = Math.ceil(numTests / this.numShards_); |
| 97 |
var startIndex = (this.shardIndex_ - 1) * shardSize; |
| 98 |
var endIndex = startIndex + shardSize; |
| 99 |
goog.asserts.assert( |
| 100 |
this.order == goog.testing.TestCase.Order.SORTED, |
| 101 |
'Only SORTED order is allowed for sharded tests'); |
| 102 |
this.setTests(this.getTests().slice(startIndex, endIndex)); |
| 103 |
this.sharded_ = true; |
| 104 |
} |
| 105 |
|
| 106 |
// Call original runTests method to execute the tests. |
| 107 |
goog.testing.ShardingTestCase.base(this, 'runTests'); |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Shards tests based on the test filename. Assumes that the filename is |
| 113 |
* formatted like 'foo_1of5_test.html'. |
| 114 |
* @param {string=} opt_name A descriptive name for the test case. |
| 115 |
*/ |
| 116 |
goog.testing.ShardingTestCase.shardByFileName = function(opt_name) { |
| 117 |
var path = window.location.pathname; |
| 118 |
var shardMatch = path.match(/_(\d+)of(\d+)_test\.(js|html)/); |
| 119 |
goog.asserts.assert( |
| 120 |
shardMatch, 'Filename must be of the form "foo_1of5_test.{js,html}"'); |
| 121 |
var shardIndex = parseInt(shardMatch[1], 10); |
| 122 |
var numShards = parseInt(shardMatch[2], 10); |
| 123 |
|
| 124 |
var testCase = |
| 125 |
new goog.testing.ShardingTestCase(shardIndex, numShards, opt_name); |
| 126 |
goog.testing.TestCase.initializeTestRunner(testCase); |
| 127 |
}; |