make qunit test order shuffling reproducible

shuffle the tests in the rake task instead of the runner.
 * the specific order used in a test run is preserved in
   spec/javascripts/tests.js, so it's easy to tell which tests
   ran before the failing one.
 * the PRNG seed is displayed in the log output, and can be
   re-used via the `seed` environment variable, making test
   failures dependent on run order much easier to reproduce.

refs CNVS-20850

Change-Id: I2538789d8b4102976e456ab4b89924bde0c5b709
Reviewed-on: https://gerrit.instructure.com/55530
Tested-by: Jenkins
Reviewed-by: Simon Williams <simon@instructure.com>
Reviewed-by: Ethan Vizitei <evizitei@instructure.com>
Product-Review: Jeremy Stanley <jeremy@instructure.com>
QA-Review: Jeremy Stanley <jeremy@instructure.com>
This commit is contained in:
Jeremy Stanley 2015-06-02 13:29:43 -06:00
parent 2112542e53
commit 753570d6eb
2 changed files with 14 additions and 17 deletions

View File

@ -38,6 +38,17 @@ namespace :js do
build_requirejs_config
end
def generate_prng
if ENV["seed"]
seed = ENV["seed"].to_i
else
srand
seed = rand(1 << 20)
end
puts "--> randomized with seed #{seed}"
Random.new(seed)
end
def build_requirejs_config
require 'canvas/require_js'
require 'erubis'
@ -52,7 +63,7 @@ namespace :js do
"spec/plugins/*/javascripts/compiled/#{matcher}"
].map{ |file| file.sub(/\.js$/, '').sub(/public\/javascripts\//, '') }
File.open("#{Rails.root}/spec/javascripts/tests.js", 'w') { |f|
f.write("window.__TESTS__ = #{JSON.pretty_generate(tests)}")
f.write("window.__TESTS__ = #{JSON.pretty_generate(tests.shuffle(random: generate_prng))}")
}
end
@ -85,7 +96,7 @@ namespace :js do
end
def test_suite(reporter=nil)
if test_js_with_timeout(300,reporter) != 0 && !ENV['JS_SPEC_MATCHER']
if test_js_with_timeout(300,reporter) != 0 && !ENV['JS_SPEC_MATCHER'] && ENV['retry'] != 'false'
puts "--> Karma tests failed." # retrying karma...
raise "Karma tests failed on second attempt." if test_js_with_timeout(400,reporter) != 0
end

View File

@ -1,5 +1,4 @@
tests = shuffle(__TESTS__);
// tests = __TESTS__
tests = __TESTS__;
// tests = tests.slice(0,50);
// console.log(tests);
@ -40,16 +39,3 @@ if(window.__karma__) {
}
});
}
// Fisher-Yates shuffle
// http://jsperf.com/fisher-yates-compare-shuffle/12
function shuffle(array) {
var tmp, current, top = array.length;
if (top) while (top) {
current = (Math.random() * (top--)) | 0;
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}