Bug 1055145: Remove push server fallback from Loop Client. r=abr a=sylvestre
authorPaul Kerr [:pkerr] <pkerr@mozilla.com>
Fri, 19 Dec 2014 15:37:26 -0800
changeset 242576 39ce6eff1b10f597f900ca15220117f593faa406
parent 242575 877827bc103a271dc3b12b0f5526bd20ff40dd8f
child 242577 59089cd08413fe3392693d843eb4d1d8e4c16cc5
push id4311
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 19:37:41 +0000
treeherdermozilla-beta@150c9fed433b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersabr, sylvestre
bugs1055145
milestone36.0a2
Bug 1055145: Remove push server fallback from Loop Client. r=abr a=sylvestre
browser/components/loop/MozLoopPushHandler.jsm
browser/components/loop/test/xpcshell/test_looppush_initialize.js
--- a/browser/components/loop/MozLoopPushHandler.jsm
+++ b/browser/components/loop/MozLoopPushHandler.jsm
@@ -276,19 +276,19 @@ let MozLoopPushHandler = {
     this._websocket.protocol = "push-notification";
 
     let performOpen = () => {
       let uri = Services.io.newURI(this.pushServerUri, null, null);
       this._websocket.asyncOpen(uri, this.pushServerUri, this, null);
     }
 
     let pushServerURLFetchError = () => {
-      console.warn("MozLoopPushHandler - Could not retrieve push server URL from Loop server; using default");
-      this.pushServerUri = Services.prefs.getCharPref("services.push.serverURL");
-      performOpen();
+      console.warn("MozLoopPushHandler - Could not retrieve push server URL from Loop server, will retry");
+      this._retryOperation(() => this._openSocket());
+      return;
     }
 
     if (!this.pushServerUri) {
       // Get push server to use from the Loop server
       let pushUrlEndpoint = Services.prefs.getCharPref("loop.server") + "/push-server-config";
       let req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
                 createInstance(Ci.nsIXMLHttpRequest);
       req.open("GET", pushUrlEndpoint);
@@ -298,16 +298,17 @@ let MozLoopPushHandler = {
           try {
             pushServerConfig = JSON.parse(req.responseText);
           } catch (e) {
             console.warn("MozLoopPushHandler - Error parsing JSON response for push server URL");
             pushServerURLFetchError();
           }
           if (pushServerConfig.pushServerURI) {
             this.pushServerUri = pushServerConfig.pushServerURI;
+            this._retryEnd();
             performOpen();
           } else {
             console.warn("MozLoopPushHandler - push server URL config lacks pushServerURI parameter");
             pushServerURLFetchError();
           }
         } else {
           console.warn("MozLoopPushHandler - push server URL retrieve error: " + req.status);
           pushServerURLFetchError();
--- a/browser/components/loop/test/xpcshell/test_looppush_initialize.js
+++ b/browser/components/loop/test/xpcshell/test_looppush_initialize.js
@@ -1,22 +1,61 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 {
   let dummyCallback = () => {};
   let mockWebSocket = new MockWebSocketChannel();
+  let pushServerRequestCount = 0;
 
   add_test(function test_initalize_offline() {
     Services.io.offline = true;
     do_check_false(MozLoopPushHandler.initialize());
     Services.io.offline = false;
-    MozLoopPushHandler.initialize({mockWebSocket: mockWebSocket});
     run_next_test();
   });
 
+  add_test(function test_initalize() {
+    loopServer.registerPathHandler("/push-server-config", (request, response) => {
+      // The PushHandler should retry the request for the push-server-config for
+      // each of these cases without throwing an error.
+      let n = 0;
+      switch (++pushServerRequestCount) {
+      case ++n:
+        // Non-200 response
+        response.setStatusLine(null, 500, "Retry");
+        response.processAsync();
+        response.finish();
+        break;
+      case ++n:
+        // missing parameter
+        response.setStatusLine(null, 200, "OK");
+        response.write(JSON.stringify({pushServerURI: null}));
+        response.processAsync();
+        response.finish();
+        break;
+      case ++n:
+        // json parse error
+        response.setStatusLine(null, 200, "OK");
+        response.processAsync();
+        response.finish();
+        break;
+      case ++n:
+        response.setStatusLine(null, 200, "OK");
+        response.write(JSON.stringify({pushServerURI: kServerPushUrl}));
+        response.processAsync();
+        response.finish();
+
+        run_next_test();
+        break;
+      }
+    });
+
+    do_check_true(MozLoopPushHandler.initialize({mockWebSocket: mockWebSocket}));
+  });
+
   add_test(function test_initalize_missing_chanid() {
     Assert.throws(() => {MozLoopPushHandler.register(null, dummyCallback, dummyCallback)});
     run_next_test();
   });
 
   add_test(function test_initalize_missing_regcallback() {
     Assert.throws(() => {MozLoopPushHandler.register("chan-1", null, dummyCallback)});
     run_next_test();
@@ -99,16 +138,18 @@
     MozLoopPushHandler.registeredChannels = {}; //Do this to force a new registration callback.
     mockWebSocket.initRegStatus = 500;
     mockWebSocket.stop();
   });
 
   function run_test() {
     setupFakeLoopServer();
 
-    Services.prefs.setCharPref("services.push.serverURL", kServerPushUrl);
-    Services.prefs.setCharPref("loop.server", kLoopServerUrl);
     Services.prefs.setIntPref("loop.retry_delay.start", 10); // 10 ms
     Services.prefs.setIntPref("loop.retry_delay.limit", 20); // 20 ms
 
+    do_register_cleanup(function() {
+      Services.prefs.setCharPref("loop.server", kLoopServerUrl);
+    });
+
     run_next_test();
   };
 }