2 // Create 6 (=network.http.max-persistent-connections-per-server)
3 // common Http requests and 2 urgent-start Http requests to a single
4 // host and path, in parallel.
5 // Let all the requests unanswered by the server handler. (process them
6 // async and don't finish)
7 // The first 6 pending common requests will fill the limit for per-server
9 // But the two urgent requests must reach the server despite those 6 common
11 // The server handler doesn't let the test finish until all 8 expected requests
13 // Note: if the urgent request handling is broken (the urgent-marked requests
14 // get blocked by queuing) this test will time out
18 const { HttpServer } = ChromeUtils.importESModule(
19 "resource://testing-common/httpd.sys.mjs"
21 var server = new HttpServer();
23 var baseURL = "http://localhost:" + server.identity.primaryPort + "/";
24 var maxConnections = 0;
25 var urgentRequests = 0;
34 dump("TEST INFO | " + msg + "\n");
38 function make_channel(url) {
39 var request = NetUtil.newChannel({
41 loadUsingSystemPrincipal: true,
43 request.QueryInterface(Ci.nsIHttpChannel);
47 function serverStopListener() {
51 function commonHttpRequest(id) {
53 var chan = make_channel(uri);
54 var listner = new HttpResponseListener(id);
55 chan.setRequestHeader("X-ID", id, false);
56 chan.setRequestHeader("Cache-control", "no-store", false);
57 chan.asyncOpen(listner);
58 log("Create common http request id=" + id);
61 function urgentStartHttpRequest(id) {
63 var chan = make_channel(uri);
64 var listner = new HttpResponseListener(id);
65 var cos = chan.QueryInterface(Ci.nsIClassOfService);
66 cos.addClassFlags(Ci.nsIClassOfService.UrgentStart);
67 chan.setRequestHeader("X-ID", id, false);
68 chan.setRequestHeader("Cache-control", "no-store", false);
69 chan.asyncOpen(listner);
70 log("Create urgent-start http request id=" + id);
73 function setup_httpRequests() {
74 log("setup_httpRequests");
75 for (var i = 0; i < maxConnections; i++) {
81 function setup_urgentStartRequests() {
82 for (var i = 0; i < urgentRequests; i++) {
83 urgentStartHttpRequest(1000 + i);
88 function HttpResponseListener(id) {
92 HttpResponseListener.prototype = {
98 log("STOP id=" + this.id);
103 var responseQueue = [];
104 function setup_http_server() {
105 log("setup_http_server");
106 maxConnections = Services.prefs.getIntPref(
107 "network.http.max-persistent-connections-per-server"
110 var allCommonHttpRequestReceived = false;
111 // Start server; will be stopped at test cleanup time.
112 server.registerPathHandler("/", function (metadata, response) {
113 var id = metadata.getHeader("X-ID");
114 log("Server recived the response id=" + id);
115 response.processAsync();
116 responseQueue.push(response);
119 responseQueue.length == maxConnections &&
120 !allCommonHttpRequestReceived
122 allCommonHttpRequestReceived = true;
123 setup_urgentStartRequests();
125 // Wait for all expected requests to come but don't process then.
126 // Collect them in a queue for later processing. We don't want to
127 // respond to the client until all the expected requests are made
129 if (responseQueue.length == maxConnections + urgentRequests) {
134 registerCleanupFunction(function () {
135 server.stop(serverStopListener);
139 function processResponse() {
140 while (responseQueue.length) {
141 var resposne = responseQueue.pop();
146 function run_test() {
148 setup_httpRequests();