Bug 967372 - Fix intermittent test_hawk.js failures on Windows. r=ttaubert, a=test-only
authorJed Parsons <jedp@mozilla.com>
Sat, 08 Feb 2014 17:07:34 -0800
changeset 182844 82fa5389a966897cc2d824f2daa8468b3389a539
parent 182843 4d9aa8730b3663f108c3acfe8f748f36b15dd7ee
child 182845 ed268cf8ef4b8844081a801a6185c53745245f27
push id3343
push userffxbld
push dateMon, 17 Mar 2014 21:55:32 +0000
treeherdermozilla-beta@2f7d3415f79f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersttaubert, test-only
bugs967372
milestone29.0a2
Bug 967372 - Fix intermittent test_hawk.js failures on Windows. r=ttaubert, a=test-only
services/common/tests/unit/test_hawk.js
--- a/services/common/tests/unit/test_hawk.js
+++ b/services/common/tests/unit/test_hawk.js
@@ -1,11 +1,13 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
+"use strict";
+
 Cu.import("resource://gre/modules/Promise.jsm");
 Cu.import("resource://services-common/hawk.js");
 
 const SECOND_MS = 1000;
 const MINUTE_MS = SECOND_MS * 60;
 const HOUR_MS = MINUTE_MS * 60;
 
 const TEST_CREDS = {
@@ -15,17 +17,16 @@ const TEST_CREDS = {
 };
 
 initTestLogging("Trace");
 
 add_task(function test_now() {
   let client = new HawkClient("https://example.com");
 
   do_check_true(client.now() - Date.now() < SECOND_MS);
-  run_next_test();
 });
 
 add_task(function test_updateClockOffset() {
   let client = new HawkClient("https://example.com");
 
   let now = new Date();
   let serverDate = now.toUTCString();
 
@@ -36,18 +37,16 @@ add_task(function test_updateClockOffset
 
   // Check that they're close; there will likely be a one-second rounding
   // error, so checking strict equality will likely fail.
   //
   // localtimeOffsetMsec is how many milliseconds to add to the local clock so
   // that it agrees with the server.  We are one hour ahead of the server, so
   // our offset should be -1 hour.
   do_check_true(Math.abs(client.localtimeOffsetMsec + HOUR_MS) <= SECOND_MS);
-
-  run_next_test();
 });
 
 add_task(function test_authenticated_get_request() {
   let message = "{\"msg\": \"Great Success!\"}";
   let method = "GET";
 
   let server = httpd_setup({"/foo": (request, response) => {
       do_check_true(request.hasHeader("Authorization"));
@@ -100,16 +99,17 @@ add_task(function test_credentials_optio
       response.setHeader("Content-Type", "application/json");
       response.bodyOutputStream.write(message, message.length);
     }
   });
 
   let client = new HawkClient(server.baseURI);
   let result = yield client.request("/foo", method); // credentials undefined
   do_check_eq(JSON.parse(result).msg, "you're in the friend zone");
+
   yield deferredStop(server);
 });
 
 add_task(function test_server_error() {
   let message = "Ohai!";
   let method = "GET";
 
   let server = httpd_setup({"/foo": (request, response) => {
@@ -117,16 +117,17 @@ add_task(function test_server_error() {
       response.bodyOutputStream.write(message, message.length);
     }
   });
 
   let client = new HawkClient(server.baseURI);
 
   try {
     yield client.request("/foo", method, TEST_CREDS);
+    do_throw("Expected an error");
   } catch(err) {
     do_check_eq(418, err.code);
     do_check_eq("I am a Teapot", err.message);
   }
 
   yield deferredStop(server);
 });
 
@@ -139,16 +140,17 @@ add_task(function test_server_error_json
       response.bodyOutputStream.write(message, message.length);
     }
   });
 
   let client = new HawkClient(server.baseURI);
 
   try {
     yield client.request("/foo", method, TEST_CREDS);
+    do_throw("Expected an error");
   } catch(err) {
     do_check_eq("Cannot get ye flask.", err.error);
   }
 
   yield deferredStop(server);
 });
 
 add_task(function test_offset_after_request() {
@@ -238,17 +240,16 @@ add_task(function test_2xx_success() {
   let client = new HawkClient(server.baseURI);
 
   let response = yield client.request("/foo", method, credentials);
 
   // Shouldn't be any content in a 202
   do_check_eq(response, "");
 
   yield deferredStop(server);
-
 });
 
 add_task(function test_retry_request_on_fail() {
   let attempts = 0;
   let credentials = {
     id: "eyJleHBpcmVzIjogMTM2NTAxMDg5OC4x",
     key: "qTZf4ZFpAMpMoeSsX3zVRjiqmNs=",
     algorithm: "sha256"
@@ -264,24 +265,26 @@ add_task(function test_retry_request_on_
 
       let delta = getTimestampDelta(request.getHeader("Authorization"));
 
       // First time through, we should have a bad timestamp
       if (attempts === 1) {
         do_check_true(delta > MINUTE_MS);
         let message = "never!!!";
         response.setStatusLine(request.httpVersion, 401, "Unauthorized");
-        return response.bodyOutputStream.write(message, message.length);
+        response.bodyOutputStream.write(message, message.length);
+        return;
       }
 
       // Second time through, timestamp should be corrected by client
       do_check_true(delta < MINUTE_MS);
       let message = "i love you!!!";
       response.setStatusLine(request.httpVersion, 200, "OK");
       response.bodyOutputStream.write(message, message.length);
+      return;
     }
   });
 
   let client = new HawkClient(server.baseURI);
   function getOffset() {
     return client.localtimeOffsetMsec;
   }
 
@@ -345,27 +348,25 @@ add_task(function test_multiple_401_retr
   do_check_eq(attempts, 2);
 
   yield deferredStop(server);
 });
 
 add_task(function test_500_no_retry() {
   // If we get a 500 error, the client should not retry (as it would with a
   // 401)
-  let attempts = 0;
   let credentials = {
     id: "eyJleHBpcmVzIjogMTM2NTAxMDg5OC4x",
     key: "qTZf4ZFpAMpMoeSsX3zVRjiqmNs=",
     algorithm: "sha256"
   };
   let method = "GET";
 
   let server = httpd_setup({
     "/no-shutup": function() {
-      attempts += 1;
       let message = "Cannot get ye flask.";
       response.setStatusLine(request.httpVersion, 500, "Internal server error");
       response.bodyOutputStream.write(message, message.length);
     }
   });
 
   let client = new HawkClient(server.baseURI);
   function getOffset() {
@@ -376,23 +377,22 @@ add_task(function test_500_no_retry() {
   // it could
   client.now = () => {
     return Date.now() - 12 * HOUR_MS;
   };
 
   // Request will 500; no retries
   try {
     yield client.request("/no-shutup", method, credentials);
+    do_throw("Expected an error");
   } catch(err) {
     do_check_eq(err.code, 500);
   }
-  do_check_eq(attempts, 1);
 
   yield deferredStop(server);
-
 });
 
 add_task(function test_401_then_500() {
   // Like test_multiple_401_retry_once, but return a 500 to the
   // second request, ensuring that the promise is properly rejected
   // in client.request.
   let attempts = 0;
   let credentials = {
@@ -412,25 +412,27 @@ add_task(function test_401_then_500() {
       let delta = getTimestampDelta(request.getHeader("Authorization"));
 
       // First time through, we should have a bad timestamp
       // Client will retry
       if (attempts === 1) {
         do_check_true(delta > MINUTE_MS);
         let message = "never!!!";
         response.setStatusLine(request.httpVersion, 401, "Unauthorized");
-        return response.bodyOutputStream.write(message, message.length);
+        response.bodyOutputStream.write(message, message.length);
+        return;
       }
 
       // Second time through, timestamp should be corrected by client
       // And fail on the client
       do_check_true(delta < MINUTE_MS);
       let message = "Cannot get ye flask.";
       response.setStatusLine(request.httpVersion, 500, "Internal server error");
       response.bodyOutputStream.write(message, message.length);
+      return;
     }
   });
 
   let client = new HawkClient(server.baseURI);
   function getOffset() {
     return client.localtimeOffsetMsec;
   }
 
@@ -448,23 +450,22 @@ add_task(function test_401_then_500() {
     do_check_eq(err.code, 500);
   }
   do_check_eq(attempts, 2);
 
   yield deferredStop(server);
 });
 
 add_task(function throw_if_not_json_body() {
-  do_test_pending();
   let client = new HawkClient("https://example.com");
   try {
     yield client.request("/bogus", "GET", {}, "I am not json");
+    do_throw("Expected an error");
   } catch(err) {
     do_check_true(!!err.message);
-    do_test_finished();
   }
 });
 
 // End of tests.
 // Utility functions follow
 
 function getTimestampDelta(authHeader, now=Date.now()) {
   let tsMS = new Date(