Bug 1257153 - Cancel implicit wait timers on rejection; r=automatedtester a=test-only
authorAndreas Tolfsen <ato@mozilla.com>
Wed, 16 Mar 2016 12:27:36 +0000
changeset 323508 b206ad099d989a5aed8756138d35fd51a1c17fdf
parent 323507 895867bacac6d037c22258984c59750aa17b1576
child 323509 10de925450161fcc1352d80579bfc5a6657d4a23
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersautomatedtester, test-only
bugs1257153
milestone47.0a2
Bug 1257153 - Cancel implicit wait timers on rejection; r=automatedtester a=test-only This fixes a leak where timers were not cancelled upon promise rejection. When the function passed to implicitlyWaitFor is rejected for not finding elements after the timeout has elapsed, the elementSearch callback is no longer called because the timer is cancelled when it calls reject(). MozReview-Commit-ID: FR2iA8s1NjX
testing/marionette/element.js
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -667,17 +667,17 @@ ElementManager.prototype = {
  */
 function implicitlyWaitFor(func, timeout, interval = 100) {
   let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
 
   return new Promise((resolve, reject) => {
     let startTime = new Date().getTime();
     let endTime = startTime + timeout;
 
-    let onTimer = function() {
+    let elementSearch = function() {
       let res;
       try {
         res = func();
       } catch (e) {
         reject(e);
       }
 
       // empty arrays evaluate to true in JS,
@@ -687,26 +687,29 @@ function implicitlyWaitFor(func, timeout
       // allowing |func| to be evaluated at least once
       let col = element.isElementCollection(res);
       if (((col && res.length > 0 ) || (!col && !!res)) ||
           (startTime == endTime || new Date().getTime() >= endTime)) {
         resolve(res);
       }
     };
 
-    // Run a check immediately so we do not cause a delay in execution
-    // due to the set timer interval.
-    onTimer();
+    // the repeating slack timer waits |interval|
+    // before invoking |elementSearch|
+    elementSearch();
 
-    timer.init(onTimer, interval, Ci.nsITimer.TYPE_REPEATING_SLACK);
+    timer.init(elementSearch, interval, Ci.nsITimer.TYPE_REPEATING_SLACK);
 
-  // cancel timer and return result for yielding
+  // cancel timer and propagate result
   }).then(res => {
     timer.cancel();
     return res;
+  }, err => {
+    timer.cancel();
+    throw err;
   });
 }
 
 element.isElementCollection = function(seq) {
   if (seq === null) {
     return false;
   }