Bug 1348626 - Retry when isPinged() failed to avoid false alarm. r=francois
authorHenry Chang <hchang@mozilla.com>
Sun, 19 Mar 2017 15:52:25 +0800
changeset 348400 f7541b903b1565a6274627e2da19e6d780ba2559
parent 348399 2eb2f588b2d6a7fe818aa4f8453ffd63754d93e5
child 348401 fa8084f4f200ebb0b1649287c583e60cfa9198f3
push id39148
push userhchang@mozilla.com
push dateMon, 20 Mar 2017 10:30:05 +0000
treeherderautoland@f7541b903b15 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrancois
bugs1348626
milestone55.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 1348626 - Retry when isPinged() failed to avoid false alarm. r=francois MozReview-Commit-ID: BOdJZReICxZ
toolkit/components/url-classifier/tests/mochitest/test_classify_ping.html
--- a/toolkit/components/url-classifier/tests/mochitest/test_classify_ping.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_classify_ping.html
@@ -16,35 +16,35 @@
 <script class="testbody" type="text/javascript">
   SimpleTest.requestFlakyTimeout("Delay to make sure ping is made prior than XHR");
 
   const timeout = 200;
   const host_nottrack = "http://not-tracking.example.com/";
   const host_track = "http://trackertest.org/";
   const path_ping = "tests/toolkit/components/url-classifier/tests/mochitest/ping.sjs";
   const TP_ENABLE_PREF = "privacy.trackingprotection.enabled";
+  const RETRY_TIMEOUT_MS = 200;
 
   var testData = [
     { url: "trackertest.org/",
       db: "test-track-simple"
     }
   ];
 
   function testPingNonBlacklist() {
     SpecialPowers.setBoolPref(TP_ENABLE_PREF, true);
 
     var msg = "ping should reach page not in blacklist";
     var expectPing = true;
     var id = "1111";
     ping(id, host_nottrack);
 
     return new Promise(function(resolve, reject) {
-      setTimeout(function() {
-        isPinged(id, expectPing, msg, resolve);
-      }, timeout);
+      // Retry at most 30 seconds.
+      isPingedWithRetry(id, expectPing, msg, resolve, 30 * 1000 / RETRY_TIMEOUT_MS);
     });
   }
 
   function testPingBlacklistSafebrowsingOff() {
     SpecialPowers.setBoolPref(TP_ENABLE_PREF, false);
 
     var msg = "ping should reach page in blacklist when tracking protection is off";
     var expectPing = true;
@@ -80,29 +80,40 @@
     document.body.appendChild(elm);
 
     // Trigger ping.
     elm.click();
 
     document.body.removeChild(elm);
   }
 
-  function isPinged(id, expected, msg, callback) {
+  function isPingedWithRetry(id, expected, msg, callback, retryCnt) {
     var url = "http://mochi.test:8888/" + path_ping;
     var xhr = new XMLHttpRequest();
     xhr.open('GET', url + "?id=" + id);
     xhr.onload = function() {
       var isPinged = xhr.response === "ping";
-      is(expected, isPinged, msg);
-
-      callback();
+      let success = isPinged === expected;
+      if (success || 0 === retryCnt) {
+        is(expected, isPinged, msg);
+        callback();
+        return;
+      }
+      // Retry on failure.
+      setTimeout(() => {
+        isPingedWithRetry(id, expected, msg, callback, retryCnt - 1);
+      }, RETRY_TIMEOUT_MS);
     };
     xhr.send();
   }
 
+  function isPinged(id, expected, msg, callback) {
+    isPingedWithRetry(id, expected, msg, callback, 0);
+  }
+
   function cleanup() {
     SpecialPowers.clearUserPref(TP_ENABLE_PREF);
   }
 
   function runTest() {
     classifierHelper.waitForInit()
       .then(() => classifierHelper.addUrlToDB(testData))
       .then(testPingNonBlacklist)