Bug 1343749. Part 1 - Let MediaTestManager manage timeout of each test. draft
authorJW Wang <jwwang@mozilla.com>
Thu, 02 Mar 2017 12:44:40 +0800
changeset 493462 fb6f36d8e626fe532be6116fd44941f04482e368
parent 493461 6fde28323b45139ea8db8cfa80faca44fed90dfa
child 493463 4d8a2141e8697312c40076e1bf7f5ed5b6e5f8fa
push id47768
push userjwwang@mozilla.com
push dateSat, 04 Mar 2017 02:07:00 +0000
bugs1343749
milestone54.0a1
Bug 1343749. Part 1 - Let MediaTestManager manage timeout of each test. MozReview-Commit-ID: HocDk9FCi6Q
dom/media/test/manifest.js
--- a/dom/media/test/manifest.js
+++ b/dom/media/test/manifest.js
@@ -1607,16 +1607,20 @@ const DEBUG_TEST_LOOP_FOREVER = false;
 //      MediaTestManager.start(token) if it starts a test. The test object is
 //      guaranteed to be playable by our supported decoders; you don't need to
 //      check canPlayType.
 //   3. When your tests finishes, call MediaTestManager.finished(), passing
 //      the token back to the manager. The manager may either start the next run
 //      or end the mochitest if all the tests are done.
 function MediaTestManager() {
 
+  // Set a very large timeout to prevent Mochitest timeout.
+  // Instead MediaTestManager will manage timeout of each test.
+  SimpleTest.requestLongerTimeout(1000);
+
   // Return how many seconds elapsed since |begin|.
   function elapsedTime(begin) {
     var end = new Date();
     return (end.getTime() - begin.getTime()) / 1000;
   }
   // Sets up a MediaTestManager to runs through the 'tests' array, which needs
   // to be one of, or have the same fields as, the g*Test arrays of tests. Uses
   // the user supplied 'startTest' function to initialize the test. This
@@ -1630,16 +1634,17 @@ function MediaTestManager() {
     SimpleTest.info("Started " + this.startTime + " (" + this.startTime.getTime()/1000 + "s)");
     this.testNum = 0;
     this.tests = tests;
     this.startTest = startTest;
     this.tokens = [];
     this.isShutdown = false;
     this.numTestsRunning = 0;
     this.handlers = {};
+    this.timers = {};
 
     // Always wait for explicit finish.
     SimpleTest.waitForExplicitFinish();
     SpecialPowers.pushPrefEnv({'set': gTestPrefs}, (function() {
       this.nextTest();
     }).bind(this));
 
     SimpleTest.registerCleanupFunction(function() {
@@ -1656,31 +1661,45 @@ function MediaTestManager() {
   }
 
   // Registers that the test corresponding to 'token' has been started.
   // Don't call more than once per token.
   this.started = function(token, handler) {
     this.tokens.push(token);
     this.numTestsRunning++;
     this.handlers[token] = handler;
+
+    var onTimeout = function() {
+      ok(false, `${token} timed out!`);
+      this.finished(token);
+    }.bind(this);
+    // Default timeout to 180s for each test.
+    this.timers[token] = setTimeout(onTimeout, 180000);
+
     is(this.numTestsRunning, this.tokens.length,
        "[started " + token + " t=" + elapsedTime(this.startTime) + "] Length of array should match number of running tests");
   }
 
   // Registers that the test corresponding to 'token' has finished. Call when
   // you've finished your test. If all tests are complete this will finish the
   // run, otherwise it may start up the next run. It's ok to call multiple times
   // per token.
   this.finished = function(token) {
     var i = this.tokens.indexOf(token);
     if (i != -1) {
       // Remove the element from the list of running tests.
       this.tokens.splice(i, 1);
     }
 
+    if (this.timers[token]) {
+      // Cancel the timer when the test finishes.
+      clearTimeout(this.timers[token]);
+      this.timers[token] = null;
+    }
+
     info("[finished " + token + "] remaining= " + this.tokens);
     this.numTestsRunning--;
     is(this.numTestsRunning, this.tokens.length,
        "[finished " + token + " t=" + elapsedTime(this.startTime) + "] Length of array should match number of running tests");
     if (this.tokens.length < PARALLEL_TESTS) {
       this.nextTest();
     }
   }