netwerk/test/unit/test_httpResponseTimeout.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_httpResponseTimeout.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,163 @@
     1.4 +/* -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +"use strict";
    1.11 +
    1.12 +Cu.import("resource://testing-common/httpd.js");
    1.13 +
    1.14 +var baseURL;
    1.15 +const kResponseTimeoutPref = "network.http.response.timeout";
    1.16 +const kResponseTimeout = 1;
    1.17 +const kShortLivedKeepalivePref =
    1.18 +  "network.http.tcp_keepalive.short_lived_connections";
    1.19 +const kLongLivedKeepalivePref =
    1.20 +  "network.http.tcp_keepalive.long_lived_connections";
    1.21 +
    1.22 +const prefService = Cc["@mozilla.org/preferences-service;1"]
    1.23 +                       .getService(Ci.nsIPrefBranch);
    1.24 +
    1.25 +var server = new HttpServer();
    1.26 +
    1.27 +function TimeoutListener(expectResponse) {
    1.28 +  this.expectResponse = expectResponse;
    1.29 +}
    1.30 +
    1.31 +TimeoutListener.prototype = {
    1.32 +  onStartRequest: function (request, ctx) {
    1.33 +  },
    1.34 +
    1.35 +  onDataAvailable: function (request, ctx, stream) {
    1.36 +  },
    1.37 +
    1.38 +  onStopRequest: function (request, ctx, status) {
    1.39 +    if (this.expectResponse) {
    1.40 +      do_check_eq(status, Cr.NS_OK);
    1.41 +    } else {
    1.42 +      do_check_eq(status, Cr.NS_ERROR_NET_TIMEOUT);
    1.43 +    }
    1.44 +
    1.45 +    run_next_test();
    1.46 +  },
    1.47 +};
    1.48 +
    1.49 +function serverStopListener() {
    1.50 +  do_test_finished();
    1.51 +}
    1.52 +
    1.53 +function testTimeout(timeoutEnabled, expectResponse) {
    1.54 +  // Set timeout pref.
    1.55 +  if (timeoutEnabled) {
    1.56 +    prefService.setIntPref(kResponseTimeoutPref, kResponseTimeout);
    1.57 +  } else {
    1.58 +    prefService.setIntPref(kResponseTimeoutPref, 0);
    1.59 +  }
    1.60 +
    1.61 +  var ios = Cc["@mozilla.org/network/io-service;1"]
    1.62 +  .getService(Ci.nsIIOService);
    1.63 +  var chan = ios.newChannel(baseURL, null, null)
    1.64 +  .QueryInterface(Ci.nsIHttpChannel);
    1.65 +  var listener = new TimeoutListener(expectResponse);
    1.66 +  chan.asyncOpen(listener, null);
    1.67 +}
    1.68 +
    1.69 +function testTimeoutEnabled() {
    1.70 +  // Set a timeout value; expect a timeout and no response.
    1.71 +  testTimeout(true, false);
    1.72 +}
    1.73 +
    1.74 +function testTimeoutDisabled() {
    1.75 +  // Set a timeout value of 0; expect a response.
    1.76 +  testTimeout(false, true);
    1.77 +}
    1.78 +
    1.79 +function testTimeoutDisabledByShortLivedKeepalives() {
    1.80 +  // Enable TCP Keepalives for short lived HTTP connections.
    1.81 +  prefService.setBoolPref(kShortLivedKeepalivePref, true);
    1.82 +  prefService.setBoolPref(kLongLivedKeepalivePref, false);
    1.83 +
    1.84 +  // Try to set a timeout value, but expect a response without timeout.
    1.85 +  testTimeout(true, true);
    1.86 +}
    1.87 +
    1.88 +function testTimeoutDisabledByLongLivedKeepalives() {
    1.89 +  // Enable TCP Keepalives for long lived HTTP connections.
    1.90 +  prefService.setBoolPref(kShortLivedKeepalivePref, false);
    1.91 +  prefService.setBoolPref(kLongLivedKeepalivePref, true);
    1.92 +
    1.93 +  // Try to set a timeout value, but expect a response without timeout.
    1.94 +  testTimeout(true, true);
    1.95 +}
    1.96 +
    1.97 +function testTimeoutDisabledByBothKeepalives() {
    1.98 +  // Enable TCP Keepalives for short and long lived HTTP connections.
    1.99 +  prefService.setBoolPref(kShortLivedKeepalivePref, true);
   1.100 +  prefService.setBoolPref(kLongLivedKeepalivePref, true);
   1.101 +
   1.102 +  // Try to set a timeout value, but expect a response without timeout.
   1.103 +  testTimeout(true, true);
   1.104 +}
   1.105 +
   1.106 +function setup_tests() {
   1.107 +  // Start tests with timeout enabled, i.e. disable TCP keepalives for HTTP.
   1.108 +  // Reset pref in cleanup.
   1.109 +  if (prefService.getBoolPref(kShortLivedKeepalivePref)) {
   1.110 +    prefService.setBoolPref(kShortLivedKeepalivePref, false);
   1.111 +    do_register_cleanup(function() {
   1.112 +      prefService.setBoolPref(kShortLivedKeepalivePref, true);
   1.113 +    });
   1.114 +  }
   1.115 +  if (prefService.getBoolPref(kLongLivedKeepalivePref)) {
   1.116 +    prefService.setBoolPref(kLongLivedKeepalivePref, false);
   1.117 +    do_register_cleanup(function() {
   1.118 +      prefService.setBoolPref(kLongLivedKeepalivePref, true);
   1.119 +    });
   1.120 +  }
   1.121 +
   1.122 +  var tests = [
   1.123 +    // Enable with a timeout value >0;
   1.124 +    testTimeoutEnabled,
   1.125 +    // Disable with a timeout value of 0;
   1.126 +    testTimeoutDisabled,
   1.127 +    // Disable by enabling TCP keepalive for short-lived HTTP connections.
   1.128 +    testTimeoutDisabledByShortLivedKeepalives,
   1.129 +    // Disable by enabling TCP keepalive for long-lived HTTP connections.
   1.130 +    testTimeoutDisabledByLongLivedKeepalives,
   1.131 +    // Disable by enabling TCP keepalive for both HTTP connection types.
   1.132 +    testTimeoutDisabledByBothKeepalives
   1.133 +  ];
   1.134 +
   1.135 +  for (var i=0; i < tests.length; i++) {
   1.136 +    add_test(tests[i]);
   1.137 +  }
   1.138 +}
   1.139 +
   1.140 +function setup_http_server() {
   1.141 +  // Start server; will be stopped at test cleanup time.
   1.142 +  server.start(-1);
   1.143 +  baseURL = "http://localhost:" + server.identity.primaryPort + "/";
   1.144 +  do_print("Using baseURL: " + baseURL);
   1.145 +  server.registerPathHandler('/', function(metadata, response) {
   1.146 +    // Wait until the timeout should have passed, then respond.
   1.147 +    response.processAsync();
   1.148 +
   1.149 +    do_timeout((kResponseTimeout+1)*1000 /* ms */, function() {
   1.150 +      response.setStatusLine(metadata.httpVersion, 200, "OK");
   1.151 +      response.write("Hello world");
   1.152 +      response.finish();
   1.153 +    });
   1.154 +  });
   1.155 +  do_register_cleanup(function() {
   1.156 +    server.stop(serverStopListener);
   1.157 +  });
   1.158 +}
   1.159 +
   1.160 +function run_test() {
   1.161 +  setup_http_server();
   1.162 +
   1.163 +  setup_tests();
   1.164 +
   1.165 +  run_next_test();
   1.166 +}

mercurial