Bug 1343749. Part 1 - Let MediaTestManager manage timeout of each test. r=jya
authorJW Wang <jwwang@mozilla.com>
Thu, 02 Mar 2017 12:44:40 +0800
changeset 494418 bb160269c9972f5a48a9f9268f07300d22a85ca5
parent 494417 475af30afdadfdc2ab5873e374afc3028c80b4dc
child 494419 64fcb6bf729566d9db9acc620bd48bd79cacdbbd
push id48023
push userjwwang@mozilla.com
push dateTue, 07 Mar 2017 02:32:15 +0000
reviewersjya
bugs1343749
milestone54.0a1
Bug 1343749. Part 1 - Let MediaTestManager manage timeout of each test. r=jya MozReview-Commit-ID: HocDk9FCi6Q
dom/media/test/manifest.js
--- a/dom/media/test/manifest.js
+++ b/dom/media/test/manifest.js
@@ -1606,16 +1606,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
@@ -1629,16 +1633,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() {
@@ -1655,31 +1660,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();
     }
   }