Bug 1473614 - Add sync function Sleep for pausing async functions. r=automatedtester
☠☠ backed out by bcd31adc1e7a ☠ ☠
authorAndreas Tolfsen <ato@sny.no>
Fri, 03 Aug 2018 16:10:23 +0100
changeset 826849 1ee58097b1412972a3ce2a6be6114025747f3f0a
parent 826848 87a0ebcd004ffb9c15716b36635286bb4e3bd167
child 826850 1d28e128807c3969771d1722bc567713606add73
push id118394
push usermaglione.k@gmail.com
push dateSun, 05 Aug 2018 20:34:06 +0000
reviewersautomatedtester
bugs1473614
milestone63.0a1
Bug 1473614 - Add sync function Sleep for pausing async functions. r=automatedtester This adds a new public API to the Marionette sync module that can "pause" async functions for an arbitrary amount of hardcoded time. This can be useful for debugging purposes.
testing/marionette/sync.js
testing/marionette/test/unit/test_sync.js
--- a/testing/marionette/sync.js
+++ b/testing/marionette/sync.js
@@ -13,16 +13,17 @@ const {
 } = ChromeUtils.import("chrome://marionette/content/error.js", {});
 const {Log} = ChromeUtils.import("chrome://marionette/content/log.js", {});
 
 XPCOMUtils.defineLazyGetter(this, "logger", Log.get);
 
 this.EXPORTED_SYMBOLS = [
   /* exported PollPromise, TimedPromise */
   "PollPromise",
+  "Sleep",
   "TimedPromise",
 
   /* exported MessageManagerDestroyedPromise */
   "MessageManagerDestroyedPromise",
 ];
 
 const {TYPE_ONE_SHOT, TYPE_REPEATING_SLACK} = Ci.nsITimer;
 
@@ -203,16 +204,37 @@ function TimedPromise(fn, {timeout = 150
     return res;
   }, err => {
     timer.cancel();
     throw err;
   });
 }
 
 /**
+ * Pauses for the given duration.
+ *
+ * @param {number} timeout
+ *     Duration to wait before fulfilling promise in milliseconds.
+ *
+ * @return {Promise}
+ *     Promise that fulfills when the `timeout` is elapsed.
+ *
+ * @throws {TypeError}
+ *     If `timeout` is not a number.
+ * @throws {RangeError}
+ *     If `timeout` is not an unsigned integer.
+ */
+function Sleep(timeout) {
+  if (typeof timeout != "number") {
+    throw new TypeError();
+  }
+  return new TimedPromise(() => {}, {timeout, throws: null});
+}
+
+/**
  * Detects when the specified message manager has been destroyed.
  *
  * One can observe the removal and detachment of a content browser
  * (`<xul:browser>`) or a chrome window by its message manager
  * disconnecting.
  *
  * When a browser is associated with a tab, this is safer than only
  * relying on the event `TabClose` which signalises the _intent to_
--- a/testing/marionette/test/unit/test_sync.js
+++ b/testing/marionette/test/unit/test_sync.js
@@ -1,14 +1,15 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 const {
   PollPromise,
+  Sleep,
   TimedPromise,
 } = ChromeUtils.import("chrome://marionette/content/sync.js", {});
 
 const DEFAULT_TIMEOUT = 2000;
 
 add_test(function test_PollPromise_funcTypes() {
   for (let type of ["foo", 42, null, undefined, true, [], {}]) {
     Assert.throws(() => new PollPromise(type), /TypeError/);
@@ -126,8 +127,17 @@ add_test(function test_TimedPromise_time
   }
   for (let timeout of [1.2, -1]) {
     Assert.throws(() => new TimedPromise(() => {}, {timeout}), /RangeError/);
   }
   new TimedPromise(() => {}, {timeout: 42});
 
   run_next_test();
 });
+
+add_task(async function test_Sleep() {
+  await Sleep(0);
+  for (let type of ["foo", true, null, undefined]) {
+    Assert.throws(() => Sleep(type), /TypeError/);
+  }
+  Assert.throws(() => Sleep(1.2), /RangeError/);
+  Assert.throws(() => Sleep(-1), /RangeError/);
+});