Bug 1544051 - Part 1 - Move add_task into SimpleTest.js r=ahal
authorBrian Grinstead <bgrinstead@mozilla.com>
Thu, 18 Apr 2019 17:01:02 +0000
changeset 470046 77269915d66a17b150c6595fb80e85d7ec1e38ab
parent 470045 e95b995fc086ba80fb30791901e634374d33eacf
child 470047 480e15689b61af2ecd889193fe42558d8d0e04e2
push id112839
push userapavel@mozilla.com
push dateThu, 18 Apr 2019 21:50:57 +0000
treeherdermozilla-inbound@e0a826fcd85b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersahal
bugs1544051
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1544051 - Part 1 - Move add_task into SimpleTest.js r=ahal Differential Revision: https://phabricator.services.mozilla.com/D27759
testing/mochitest/tests/SimpleTest/AddTask.js
testing/mochitest/tests/SimpleTest/SimpleTest.js
--- a/testing/mochitest/tests/SimpleTest/AddTask.js
+++ b/testing/mochitest/tests/SimpleTest/AddTask.js
@@ -1,96 +0,0 @@
-// add_task(generatorFunction):
-// Call `add_task(generatorFunction)` for each separate
-// asynchronous task in a mochitest. Tasks are run consecutively.
-// Before the first task, `SimpleTest.waitForExplicitFinish()`
-// will be called automatically, and after the last task,
-// `SimpleTest.finish()` will be called.
-var add_task = (function () {
-  // The list of tasks to run.
-  var task_list = [];
-  var run_only_this_task = null;
-
-  function isGenerator(value) {
-    return value && typeof value === "object" && typeof value.next === "function";
-  }
-
-  // The "add_task" function
-  return function (generatorFunction) {
-    if (task_list.length === 0) {
-      // This is the first time add_task has been called.
-      // First, confirm that SimpleTest is available.
-      if (!SimpleTest) {
-        throw new Error("SimpleTest not available.");
-      }
-      // Don't stop tests until asynchronous tasks are finished.
-      SimpleTest.waitForExplicitFinish();
-      // Because the client is using add_task for this set of tests,
-      // we need to spawn a "master task" that calls each task in succesion.
-      // Use setTimeout to ensure the master task runs after the client
-      // script finishes.
-      setTimeout(function nextTick() {
-        // If we are in a HTML document, we should wait for the document
-        // to be fully loaded.
-        // These checks ensure that we are in an HTML document without
-        // throwing TypeError; also I am told that readyState in XUL documents
-        // are totally bogus so we don't try to do this there.
-        if (typeof window !== "undefined" &&
-            typeof HTMLDocument !== "undefined" &&
-            window.document instanceof HTMLDocument &&
-            window.document.readyState !== "complete") {
-          setTimeout(nextTick);
-          return;
-        }
-
-        (async () => {
-          // Allow for a task to be skipped; we need only use the structured logger
-          // for this, whilst deactivating log buffering to ensure that messages
-          // are always printed to stdout.
-          function skipTask(name) {
-            let logger = parentRunner && parentRunner.structuredLogger;
-            if (!logger) {
-              info("AddTask.js | Skipping test " + name);
-              return;
-            }
-            logger.deactivateBuffering();
-            logger.testStatus(SimpleTest._getCurrentTestURL(), name, "SKIP");
-            logger.warning("AddTask.js | Skipping test " + name);
-            logger.activateBuffering();
-          }
-
-          // We stop the entire test file at the first exception because this
-          // may mean that the state of subsequent tests may be corrupt.
-          try {
-            for (var task of task_list) {
-              var name = task.name || "";
-              if (task.__skipMe || (run_only_this_task && task != run_only_this_task)) {
-                skipTask(name);
-                continue;
-              }
-              info("AddTask.js | Entering test " + name);
-              let result = await task();
-              if (isGenerator(result)) {
-                ok(false, "Task returned a generator");
-              }
-              info("AddTask.js | Leaving test " + name);
-            }
-          } catch (ex) {
-            try {
-              SimpleTest.record(false, "" + ex, "Should not throw any errors", ex.stack);
-            } catch (ex2) {
-              SimpleTest.record(false, "(The exception cannot be converted to string.)",
-                 "Should not throw any errors", ex.stack);
-            }
-          }
-          // All tasks are finished.
-          SimpleTest.finish();
-        })();
-      });
-    }
-    generatorFunction.skip = () => generatorFunction.__skipMe = true;
-    generatorFunction.only = () => run_only_this_task = generatorFunction;
-    // Add the task to the list of tasks to run after
-    // the main thread is finished.
-    task_list.push(generatorFunction);
-    return generatorFunction;
-  };
-})();
--- a/testing/mochitest/tests/SimpleTest/SimpleTest.js
+++ b/testing/mochitest/tests/SimpleTest/SimpleTest.js
@@ -1716,13 +1716,111 @@ function getAndroidSdk() {
                                        .getService(SpecialPowers.Ci.nsIPropertyBag2)
                                        .getProperty(versionString);
         }
         document.documentElement.removeChild(iframe);
     }
     return gAndroidSdk;
 }
 
+// add_task(generatorFunction):
+// Call `add_task(generatorFunction)` for each separate
+// asynchronous task in a mochitest. Tasks are run consecutively.
+// Before the first task, `SimpleTest.waitForExplicitFinish()`
+// will be called automatically, and after the last task,
+// `SimpleTest.finish()` will be called.
+var add_task = (function () {
+  // The list of tasks to run.
+  var task_list = [];
+  var run_only_this_task = null;
+
+  function isGenerator(value) {
+    return value && typeof value === "object" && typeof value.next === "function";
+  }
+
+  // The "add_task" function
+  return function (generatorFunction) {
+    if (task_list.length === 0) {
+      // This is the first time add_task has been called.
+      // First, confirm that SimpleTest is available.
+      if (!SimpleTest) {
+        throw new Error("SimpleTest not available.");
+      }
+      // Don't stop tests until asynchronous tasks are finished.
+      SimpleTest.waitForExplicitFinish();
+      // Because the client is using add_task for this set of tests,
+      // we need to spawn a "master task" that calls each task in succesion.
+      // Use setTimeout to ensure the master task runs after the client
+      // script finishes.
+      setTimeout(function nextTick() {
+        // If we are in a HTML document, we should wait for the document
+        // to be fully loaded.
+        // These checks ensure that we are in an HTML document without
+        // throwing TypeError; also I am told that readyState in XUL documents
+        // are totally bogus so we don't try to do this there.
+        if (typeof window !== "undefined" &&
+            typeof HTMLDocument !== "undefined" &&
+            window.document instanceof HTMLDocument &&
+            window.document.readyState !== "complete") {
+          setTimeout(nextTick);
+          return;
+        }
+
+        (async () => {
+          // Allow for a task to be skipped; we need only use the structured logger
+          // for this, whilst deactivating log buffering to ensure that messages
+          // are always printed to stdout.
+          function skipTask(name) {
+            let logger = parentRunner && parentRunner.structuredLogger;
+            if (!logger) {
+              info("add_task | Skipping test " + name);
+              return;
+            }
+            logger.deactivateBuffering();
+            logger.testStatus(SimpleTest._getCurrentTestURL(), name, "SKIP");
+            logger.warning("add_task | Skipping test " + name);
+            logger.activateBuffering();
+          }
+
+          // We stop the entire test file at the first exception because this
+          // may mean that the state of subsequent tests may be corrupt.
+          try {
+            for (var task of task_list) {
+              var name = task.name || "";
+              if (task.__skipMe || (run_only_this_task && task != run_only_this_task)) {
+                skipTask(name);
+                continue;
+              }
+              info("add_task | Entering test " + name);
+              let result = await task();
+              if (isGenerator(result)) {
+                ok(false, "Task returned a generator");
+              }
+              info("add_task | Leaving test " + name);
+            }
+          } catch (ex) {
+            try {
+              SimpleTest.record(false, "" + ex, "Should not throw any errors", ex.stack);
+            } catch (ex2) {
+              SimpleTest.record(false, "(The exception cannot be converted to string.)",
+                 "Should not throw any errors", ex.stack);
+            }
+          }
+          // All tasks are finished.
+          SimpleTest.finish();
+        })();
+      });
+    }
+    generatorFunction.skip = () => generatorFunction.__skipMe = true;
+    generatorFunction.only = () => run_only_this_task = generatorFunction;
+    // Add the task to the list of tasks to run after
+    // the main thread is finished.
+    task_list.push(generatorFunction);
+    return generatorFunction;
+  };
+})();
+
+
 // Request complete log when using failure patterns so that failure info
 // from infra can be useful.
 if (usesFailurePatterns()) {
   SimpleTest.requestCompleteLog();
 }