Bug 1473614 - Add sync function Sleep for pausing async functions. r=automatedtester
authorAndreas Tolfsen <ato@sny.no>
Fri, 03 Aug 2018 16:10:23 +0100
changeset 430415 c76d0cb71ad460386bf10e8c09ece7f6d2d76ec3
parent 430414 075c87b1393a66c60f796383efd8f697d36893a1
child 430416 684c928574c6896fde319a7a947e2125ca37a366
push id34403
push usercbrindusan@mozilla.com
push dateTue, 07 Aug 2018 21:52:23 +0000
treeherdermozilla-central@d9e6ce390607 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersautomatedtester
bugs1473614
milestone63.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 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(resolve => resolve(), {timeout}), /RangeError/);
   }
   new TimedPromise(resolve => resolve(), {timeout: 42});
 
   run_next_test();
 });
+
+add_task(async function test_Sleep() {
+  await Sleep(0);
+  for (let type of ["foo", true, null, undefined]) {
+    Assert.throws(() => new Sleep(type), /TypeError/);
+  }
+  Assert.throws(() => new Sleep(1.2), /RangeError/);
+  Assert.throws(() => new Sleep(-1), /RangeError/);
+});