Bug 1504756 - [marionette] Add executeSoon() to run tasks on the main thread. r=ato
☠☠ backed out by 49bd1dd914a4 ☠ ☠
authorHenrik Skupin <mail@hskupin.info>
Tue, 04 Dec 2018 21:57:28 +0000
changeset 508559 04da1f01afba4940e1fa94585e046c7d1103ddd6
parent 508558 32d18c6e3d45c164ddc641186e50e005cd782bc9
child 508560 c90e4b2e1b2848756daa201bd2d667c3557edadb
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato
bugs1504756
milestone65.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 1504756 - [marionette] Add executeSoon() to run tasks on the main thread. r=ato To be closer to other test harnesses which are using executeSoon() to run a task on the main thread, this patch adds the same method to Marionette's sync module. Differential Revision: https://phabricator.services.mozilla.com/D13659
testing/marionette/doc/internals/sync.rst
testing/marionette/sync.js
testing/marionette/test/unit/test_sync.js
testing/marionette/transport.js
--- a/testing/marionette/doc/internals/sync.rst
+++ b/testing/marionette/doc/internals/sync.rst
@@ -1,13 +1,15 @@
 sync module
 ===========
 
 Provides an assortment of synchronisation primitives.
 
+.. js:autofunction:: executeSoon
+
 .. js:autoclass:: MessageManagerDestroyedPromise
   :members:
 
 .. js:autoclass:: PollPromise
   :members:
 
 .. js:autoclass:: Sleep
   :members:
--- a/testing/marionette/sync.js
+++ b/testing/marionette/sync.js
@@ -13,28 +13,44 @@ const {
   stack,
   TimeoutError,
 } = ChromeUtils.import("chrome://marionette/content/error.js", {});
 const {Log} = ChromeUtils.import("chrome://marionette/content/log.js", {});
 
 XPCOMUtils.defineLazyGetter(this, "log", Log.get);
 
 this.EXPORTED_SYMBOLS = [
+  "executeSoon",
   "DebounceCallback",
   "IdlePromise",
   "MessageManagerDestroyedPromise",
   "PollPromise",
   "Sleep",
   "TimedPromise",
 ];
 
 const {TYPE_ONE_SHOT, TYPE_REPEATING_SLACK} = Ci.nsITimer;
 
 const PROMISE_TIMEOUT = AppConstants.DEBUG ? 4500 : 1500;
 
+
+/**
+ * Dispatch a function to be executed on the main thread.
+ *
+ * @param {function} func
+ *     Function to be executed.
+ */
+function executeSoon(func) {
+  if (typeof func != "function") {
+    throw new TypeError();
+  }
+
+  Services.tm.dispatchToMainThread(func);
+}
+
 /**
  * @callback Condition
  *
  * @param {function(*)} resolve
  *     To be called when the condition has been met.  Will return the
  *     resolved value.
  * @param {function} reject
  *     To be called when the condition has not been met.  Will cause
--- a/testing/marionette/test/unit/test_sync.js
+++ b/testing/marionette/test/unit/test_sync.js
@@ -30,16 +30,33 @@ class MockTimer {
     }
   }
 
   cancel() {
     this.cancelled = true;
   }
 }
 
+add_test(function test_executeSoon_callback() {
+  // executeSoon() is already defined for xpcshell in head.js. As such import
+  // our implementation into a custom namespace.
+  let sync = {};
+  ChromeUtils.import("chrome://marionette/content/sync.js", sync);
+
+  for (let func of ["foo", null, true, [], {}]) {
+    Assert.throws(() => sync.executeSoon(func), /TypeError/);
+  }
+
+  let a;
+  sync.executeSoon(() => { a = 1; });
+  executeSoon(() => equal(1, a));
+
+  run_next_test();
+});
+
 add_test(function test_PollPromise_funcTypes() {
   for (let type of ["foo", 42, null, undefined, true, [], {}]) {
     Assert.throws(() => new PollPromise(type), /TypeError/);
   }
   new PollPromise(() => {});
   new PollPromise(function() {});
 
   run_next_test();
--- a/testing/marionette/transport.js
+++ b/testing/marionette/transport.js
@@ -5,24 +5,27 @@
 "use strict";
 
 /* global Pipe, ScriptableInputStream */
 
 const CC = Components.Constructor;
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/EventEmitter.jsm");
-const {StreamUtils} =
-    ChromeUtils.import("chrome://marionette/content/stream-utils.js", {});
-const {Packet, JSONPacket, BulkPacket} =
-    ChromeUtils.import("chrome://marionette/content/packets.js", {});
-
-const executeSoon = function(func) {
-  Services.tm.dispatchToMainThread(func);
-};
+const {
+  StreamUtils,
+} = ChromeUtils.import("chrome://marionette/content/stream-utils.js", {});
+const {
+  BulkPacket,
+  JSONPacket,
+  Packet,
+} = ChromeUtils.import("chrome://marionette/content/packets.js", {});
+const {
+  executeSoon,
+} = ChromeUtils.import("chrome://marionette/content/sync.js", {});
 
 const flags = {wantVerbose: false, wantLogging: false};
 
 const dumpv =
   flags.wantVerbose ?
   function(msg) { dump(msg + "\n"); } :
   function() {};