aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/shardingtestcase.js
blob: 46cdc249d0d7ef54ee3a7c44a378b99cf8791db5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2009 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Utility for sharding tests.
 *
 * Usage instructions:
 * <ol>
 *   <li>Instead of writing your large test in foo_test.html, write it in
 * foo_test_template.html</li>
 *   <li>Add a call to {@code goog.testing.ShardingTestCase.shardByFileName()}
 * near the top of your test, before any test cases or setup methods.</li>
 *   <li>Symlink foo_test_template.html into different sharded test files
 * named foo_1of4_test.html, foo_2of4_test.html, etc, using `ln -s`.</li>
 *   <li>Add the symlinks as foo_1of4_test.html.
 *       In perforce, run the command `g4 add foo_1of4_test.html` followed
 * by `g4 reopen -t symlink foo_1of4_test.html` so that perforce treats the file
 * as a symlink
 *   </li>
 * </ol>
 *
 */

goog.provide('goog.testing.ShardingTestCase');

goog.require('goog.asserts');
goog.require('goog.testing.TestCase');

/**
 * A test case that runs tests in per-file shards.
 * @param {number} shardIndex Shard index for this page,
 *     <strong>1-indexed</strong>.
 * @param {number} numShards Number of shards to split up test cases into.
 * @extends {goog.testing.TestCase}
 * @constructor
 */
goog.testing.ShardingTestCase = function(shardIndex, numShards, opt_name) {
  goog.base(this, opt_name);

  goog.asserts.assert(shardIndex > 0, 'Shard index should be positive');
  goog.asserts.assert(numShards > 0, 'Number of shards should be positive');
  goog.asserts.assert(shardIndex <= numShards,
      'Shard index out of bounds');

  /**
   * @type {number}
   * @private
   */
  this.shardIndex_ = shardIndex;

  /**
   * @type {number}
   * @private
   */
  this.numShards_ = numShards;
};
goog.inherits(goog.testing.ShardingTestCase, goog.testing.TestCase);


/**
 * Whether we've actually partitioned the tests yet. We may execute twice
 * ('Run again without reloading') without failing.
 * @type {boolean}
 * @private
 */
goog.testing.ShardingTestCase.prototype.sharded_ = false;


/**
 * Installs a runTests global function that goog.testing.JsUnit will use to
 * run tests, which will run a single shard of the tests present on the page.
 * @override
 */
goog.testing.ShardingTestCase.prototype.runTests = function() {
  if (!this.sharded_) {
    var numTests = this.getCount();
    goog.asserts.assert(numTests >= this.numShards_,
        'Must have at least as many tests as shards!');
    var shardSize = Math.ceil(numTests / this.numShards_);
    var startIndex = (this.shardIndex_ - 1) * shardSize;
    var endIndex = startIndex + shardSize;
    goog.asserts.assert(this.order == goog.testing.TestCase.Order.SORTED,
        'Only SORTED order is allowed for sharded tests');
    this.setTests(this.getTests().slice(startIndex, endIndex));
    this.sharded_ = true;
  }

  // Call original runTests method to execute the tests.
  goog.base(this, 'runTests');
};


/**
 * Shards tests based on the test filename. Assumes that the filename is
 * formatted like 'foo_1of5_test.html'.
 * @param {string=} opt_name A descriptive name for the test case.
 */
goog.testing.ShardingTestCase.shardByFileName = function(opt_name) {
  var path = window.location.pathname;
  var shardMatch = path.match(/_(\d+)of(\d+)_test\.html/);
  goog.asserts.assert(shardMatch,
      'Filename must be of the form "foo_1of5_test.html"');
  var shardIndex = parseInt(shardMatch[1], 10);
  var numShards = parseInt(shardMatch[2], 10);

  var testCase = new goog.testing.ShardingTestCase(
      shardIndex, numShards, opt_name);
  goog.testing.TestCase.initializeTestRunner(testCase);
};