test_bug1312782_http1.js (5861B)
1 // test bug 1312782. 2 // 3 // Summary: 4 // Assume we have 6 http requests in queue, 4 are from the focused window and 5 // the other 2 are from the non-focused window. We want to test that the server 6 // should receive 4 requests from the focused window first and then receive the 7 // rest 2 requests. 8 // 9 // Test step: 10 // 1. Create 6 dummy http requests. Server would not process responses until get 11 // all 6 requests. 12 // 2. Once server receive 6 dummy requests, create 4 http requests with the focused 13 // window id and 2 requests with non-focused window id. Note that the requets's 14 // id is a serial number starting from the focused window id. 15 // 3. Server starts to process the 6 dummy http requests, so the client can start to 16 // process the pending queue. Server will queue those http requests again and wait 17 // until get all 6 requests. 18 // 4. When the server receive all 6 requests, starts to check that the request ids of 19 // the first 4 requests in the queue should be all less than focused window id 20 // plus 4. Also, the request ids of the rest requests should be less than non-focused 21 // window id + 2. 22 23 "use strict"; 24 25 const { HttpServer } = ChromeUtils.importESModule( 26 "resource://testing-common/httpd.sys.mjs" 27 ); 28 29 var server = new HttpServer(); 30 server.start(-1); 31 var baseURL = "http://localhost:" + server.identity.primaryPort + "/"; 32 var maxConnections = 0; 33 var debug = false; 34 const FOCUSED_WINDOW_ID = 123; 35 var NON_FOCUSED_WINDOW_ID; 36 var FOCUSED_WINDOW_REQUEST_COUNT; 37 var NON_FOCUSED_WINDOW_REQUEST_COUNT; 38 39 function log(msg) { 40 if (!debug) { 41 return; 42 } 43 44 if (msg) { 45 dump("TEST INFO | " + msg + "\n"); 46 } 47 } 48 49 function make_channel(url) { 50 var request = NetUtil.newChannel({ 51 uri: url, 52 loadUsingSystemPrincipal: true, 53 }); 54 request.QueryInterface(Ci.nsIHttpChannel); 55 return request; 56 } 57 58 function serverStopListener() { 59 server.stop(); 60 } 61 62 function createHttpRequest(browserId, requestId) { 63 let uri = baseURL; 64 var chan = make_channel(uri); 65 chan.browserId = browserId; 66 var listner = new HttpResponseListener(requestId); 67 chan.setRequestHeader("X-ID", requestId, false); 68 chan.setRequestHeader("Cache-control", "no-store", false); 69 chan.asyncOpen(listner); 70 log("Create http request id=" + requestId); 71 } 72 73 function setup_dummyHttpRequests() { 74 log("setup_dummyHttpRequests"); 75 for (var i = 0; i < maxConnections; i++) { 76 createHttpRequest(0, i); 77 do_test_pending(); 78 } 79 } 80 81 function setup_focusedWindowHttpRequests() { 82 log("setup_focusedWindowHttpRequests"); 83 for (var i = 0; i < FOCUSED_WINDOW_REQUEST_COUNT; i++) { 84 createHttpRequest(FOCUSED_WINDOW_ID, FOCUSED_WINDOW_ID + i); 85 do_test_pending(); 86 } 87 } 88 89 function setup_nonFocusedWindowHttpRequests() { 90 log("setup_nonFocusedWindowHttpRequests"); 91 for (var i = 0; i < NON_FOCUSED_WINDOW_REQUEST_COUNT; i++) { 92 createHttpRequest(NON_FOCUSED_WINDOW_ID, NON_FOCUSED_WINDOW_ID + i); 93 do_test_pending(); 94 } 95 } 96 97 function HttpResponseListener(id) { 98 this.id = id; 99 } 100 101 HttpResponseListener.prototype = { 102 onStartRequest() {}, 103 104 onDataAvailable() {}, 105 106 onStopRequest() { 107 log("STOP id=" + this.id); 108 do_test_finished(); 109 }, 110 }; 111 112 function check_response_id(responses, maxWindowId) { 113 for (var i = 0; i < responses.length; i++) { 114 var id = responses[i].getHeader("X-ID"); 115 log("response id=" + id + " maxWindowId=" + maxWindowId); 116 Assert.less(Number(id), maxWindowId); 117 } 118 } 119 120 var responseQueue = []; 121 function setup_http_server() { 122 log("setup_http_server"); 123 maxConnections = Services.prefs.getIntPref( 124 "network.http.max-persistent-connections-per-server" 125 ); 126 FOCUSED_WINDOW_REQUEST_COUNT = Math.floor(maxConnections * 0.8); 127 NON_FOCUSED_WINDOW_REQUEST_COUNT = 128 maxConnections - FOCUSED_WINDOW_REQUEST_COUNT; 129 NON_FOCUSED_WINDOW_ID = FOCUSED_WINDOW_ID + FOCUSED_WINDOW_REQUEST_COUNT; 130 131 var allDummyHttpRequestReceived = false; 132 // Start server; will be stopped at test cleanup time. 133 server.registerPathHandler("/", function (metadata, response) { 134 var id = metadata.getHeader("X-ID"); 135 log("Server recived the response id=" + id); 136 137 response.processAsync(); 138 response.setHeader("X-ID", id); 139 responseQueue.push(response); 140 141 if ( 142 responseQueue.length == maxConnections && 143 !allDummyHttpRequestReceived 144 ) { 145 log("received all dummy http requets"); 146 allDummyHttpRequestReceived = true; 147 setup_nonFocusedWindowHttpRequests(); 148 setup_focusedWindowHttpRequests(); 149 processResponses(); 150 } else if (responseQueue.length == maxConnections) { 151 var focusedWindowResponses = responseQueue.slice( 152 0, 153 FOCUSED_WINDOW_REQUEST_COUNT 154 ); 155 var nonFocusedWindowResponses = responseQueue.slice( 156 FOCUSED_WINDOW_REQUEST_COUNT, 157 responseQueue.length 158 ); 159 check_response_id( 160 focusedWindowResponses, 161 FOCUSED_WINDOW_ID + FOCUSED_WINDOW_REQUEST_COUNT 162 ); 163 check_response_id( 164 nonFocusedWindowResponses, 165 NON_FOCUSED_WINDOW_ID + NON_FOCUSED_WINDOW_REQUEST_COUNT 166 ); 167 processResponses(); 168 } 169 }); 170 171 registerCleanupFunction(function () { 172 server.stop(serverStopListener); 173 }); 174 } 175 176 function processResponses() { 177 while (responseQueue.length) { 178 var resposne = responseQueue.pop(); 179 resposne.finish(); 180 } 181 } 182 183 function run_test() { 184 // Make sure "network.http.active_tab_priority" is true, so we can expect to 185 // receive http requests with focused window id before others. 186 Services.prefs.setBoolPref("network.http.active_tab_priority", true); 187 188 setup_http_server(); 189 setup_dummyHttpRequests(); 190 191 var windowIdWrapper = Cc["@mozilla.org/supports-PRUint64;1"].createInstance( 192 Ci.nsISupportsPRUint64 193 ); 194 windowIdWrapper.data = FOCUSED_WINDOW_ID; 195 var obsvc = Services.obs; 196 obsvc.notifyObservers(windowIdWrapper, "net:current-browser-id"); 197 }