Bug 557589 - code audit and create unit test plan for service.js [r=mconnor]
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Tue, 10 Aug 2010 18:16:57 +0200
changeset 49610 edc4b8a08357b225301a7af258309730bde137f5
parent 49609 95630d242dad3cc118175f4674d2c87489941875
child 49611 5d64e277d398474899bccdf19175a6dfdf9a12e3
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconnor
bugs557589
Bug 557589 - code audit and create unit test plan for service.js [r=mconnor] Part 4 followup: Fix a reference error in Weave.Service._checkServerError and improve test coverage for verifyLogin() to exercise that code path.
services/sync/modules/service.js
services/sync/tests/unit/test_service_verifyLogin.js
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -1519,17 +1519,17 @@ WeaveSvc.prototype = {
   /**
    * Check to see if this is a failure
    *
    */
   _checkServerError: function WeaveSvc__checkServerError(resp) {
     if (Utils.checkStatus(resp.status, null, [500, [502, 504]])) {
       Status.enforceBackoff = true;
       if (resp.status == 503 && resp.headers["retry-after"])
-        Observers.notify("weave:service:backoff:interval", parseInt(resp.headers["retry-after"], 10));
+        Svc.Obs.notify("weave:service:backoff:interval", parseInt(resp.headers["retry-after"], 10));
     }
   },
   /**
    * Return a value for a backoff interval.  Maximum is eight hours, unless
    * Status.backoffInterval is higher.
    *
    */
   _calculateBackoff: function WeaveSvc__calculateBackoff(attempts, base_interval) {
--- a/services/sync/tests/unit/test_service_verifyLogin.js
+++ b/services/sync/tests/unit/test_service_verifyLogin.js
@@ -13,28 +13,43 @@ function login_handler(request, response
     response.setStatusLine(request.httpVersion, 200, "OK");
   } else {
     body = "Unauthorized";
     response.setStatusLine(request.httpVersion, 401, "Unauthorized");
   }
   response.bodyOutputStream.write(body, body.length);
 }
 
+function send(statusCode, status, body) {
+  return function(request, response) {
+    response.setStatusLine(request.httpVersion, statusCode, status);
+    response.bodyOutputStream.write(body, body.length);
+  };
+}
+
+function service_unavailable(request, response) {
+  let body = "Service Unavailable";
+  response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
+  response.setHeader("Retry-After", "42");
+  response.bodyOutputStream.write(body, body.length);
+}
+
 function run_test() {
   let logger = Log4Moz.repository.rootLogger;
   Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
 
   do_test_pending();
   let server = httpd_setup({
-    "/1.0/johndoe/info/collections": login_handler
+    "/api/1.0/johndoe/info/collections": login_handler,
+    "/api/1.0/janedoe/info/collections": service_unavailable,
+    "/user/1.0/johndoe/node/weave": send(200, "OK", "http://localhost:8080/api/")
   });
 
   try {
     Weave.Service.serverURL = "http://localhost:8080/";
-    Weave.Service.clusterURL = "http://localhost:8080/";
 
     _("Force the initial state.");
     Status.service = STATUS_OK;
     do_check_eq(Status.service, STATUS_OK);
 
     _("Credentials won't check out because we're not configured yet.");
     do_check_false(Weave.Service.verifyLogin());
     do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
@@ -42,20 +57,35 @@ function run_test() {
 
     _("Try again with username and password set.");
     Weave.Service.username = "johndoe";
     Weave.Service.password = "ilovejane";
     do_check_false(Weave.Service.verifyLogin());
     do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
     do_check_eq(Status.login, LOGIN_FAILED_NO_PASSPHRASE);
 
+    _("verifyLogin() has found out the user's cluster URL, though.");
+    do_check_eq(Weave.Service.clusterURL, "http://localhost:8080/api/");
+
     _("Success if passphrase is set.");
     Weave.Service.passphrase = "foo";
-    Weave.Service.login();
+    do_check_true(Weave.Service.verifyLogin());
     do_check_eq(Status.service, STATUS_OK);
     do_check_eq(Status.login, LOGIN_SUCCEEDED);
-    do_check_true(Weave.Service.isLoggedIn);
+
+    _("If verifyLogin() encounters a server error, it flips on the backoff flag and notifies observers on a 503 with Retry-After.");
+    Weave.Service.username = "janedoe";
+    do_check_false(Status.enforceBackoff);
+    let backoffInterval;    
+    Svc.Obs.add("weave:service:backoff:interval", function(subject, data) {
+      backoffInterval = subject;
+    });
+    do_check_false(Weave.Service.verifyLogin());
+    do_check_true(Status.enforceBackoff);
+    do_check_eq(backoffInterval, 42);
+    do_check_eq(Status.service, LOGIN_FAILED);
+    do_check_eq(Status.login, LOGIN_FAILED_SERVER_ERROR);
 
   } finally {
     Svc.Prefs.resetBranch("");
     server.stop(do_test_finished);
   }
 }