Bug 1348626 - Retry when isPinged() failed to avoid false alarm.
MozReview-Commit-ID: BOdJZReICxZ
--- 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)