Bug 1348626 - Retry when isPinged() failed to avoid false alarm. draft
authorHenry Chang <hchang@mozilla.com>
Sun, 19 Mar 2017 15:52:25 +0800
changeset 501238 7ddf64b373468d768fde7b2016376c223c7628b2
parent 501162 fb3c323635d60638ab8a13f919d308f8de59e25b
child 549815 895e1e3a0e3f151078b515d7ed6c60042c781c48
push id49913
push userhchang@mozilla.com
push dateSun, 19 Mar 2017 07:54:03 +0000
bugs1348626
milestone55.0a1
Bug 1348626 - Retry when isPinged() failed to avoid false alarm. 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)