4 // Assume we have 6 http requests in queue, 3 are from the focused window with
5 // normal priority and the other 3 are from the non-focused window with the
7 // We want to test that when "network.http.active_tab_priority" is false,
8 // the server should receive 3 requests with the highest priority first
9 // and then receive the rest 3 requests.
12 // 1. Create 6 dummy http requests. Server would not process responses until told
14 // 2. Once server receive 6 dummy requests, create 3 http requests with the focused
15 // window id and normal priority and 3 requests with non-focused window id and
16 // the highrst priority.
17 // Note that the requets's id is set to its window id.
18 // 3. Server starts to process the 6 dummy http requests, so the client can start to
19 // process the pending queue. Server will queue those http requests again and wait
20 // until get all 6 requests.
21 // 4. When the server receive all 6 requests, we want to check if 3 requests with higher
22 // priority are sent before others.
23 // First, we check that if the request id of the first 3 requests in the queue is
24 // equal to non focused window id.
25 // Second, we check if the request id of the rest requests is equal to focused
30 const { HttpServer } = ChromeUtils.importESModule(
31 "resource://testing-common/httpd.sys.mjs"
34 var server = new HttpServer();
36 var baseURL = "http://localhost:" + server.identity.primaryPort + "/";
37 var maxConnections = 0;
39 const FOCUSED_WINDOW_ID = 123;
40 var NON_FOCUSED_WINDOW_ID;
41 var FOCUSED_WINDOW_REQUEST_COUNT;
42 var NON_FOCUSED_WINDOW_REQUEST_COUNT;
50 dump("TEST INFO | " + msg + "\n");
54 function make_channel(url) {
55 var request = NetUtil.newChannel({
57 loadUsingSystemPrincipal: true,
59 request.QueryInterface(Ci.nsIHttpChannel);
63 function serverStopListener() {
67 function createHttpRequest(browserId, requestId, priority) {
69 var chan = make_channel(uri);
70 chan.browserId = browserId;
71 chan.QueryInterface(Ci.nsISupportsPriority).priority = priority;
72 var listner = new HttpResponseListener(requestId);
73 chan.setRequestHeader("X-ID", requestId, false);
74 chan.setRequestHeader("Cache-control", "no-store", false);
75 chan.asyncOpen(listner);
76 log("Create http request id=" + requestId);
79 function setup_dummyHttpRequests() {
80 log("setup_dummyHttpRequests");
81 for (var i = 0; i < maxConnections; i++) {
82 createHttpRequest(0, i, Ci.nsISupportsPriority.PRIORITY_NORMAL);
87 function setup_focusedWindowHttpRequests() {
88 log("setup_focusedWindowHttpRequests");
89 for (var i = 0; i < FOCUSED_WINDOW_REQUEST_COUNT; i++) {
93 Ci.nsISupportsPriority.PRIORITY_NORMAL
99 function setup_nonFocusedWindowHttpRequests() {
100 log("setup_nonFocusedWindowHttpRequests");
101 for (var i = 0; i < NON_FOCUSED_WINDOW_REQUEST_COUNT; i++) {
103 NON_FOCUSED_WINDOW_ID,
104 NON_FOCUSED_WINDOW_ID,
105 Ci.nsISupportsPriority.PRIORITY_HIGHEST
111 function HttpResponseListener(id) {
115 HttpResponseListener.prototype = {
116 onStartRequest(request) {},
118 onDataAvailable(request, stream, off, cnt) {},
120 onStopRequest(request, status) {
121 log("STOP id=" + this.id);
126 function check_response_id(responses, browserId) {
127 for (var i = 0; i < responses.length; i++) {
128 var id = responses[i].getHeader("X-ID");
129 log("response id=" + id + " browserId=" + browserId);
130 Assert.equal(id, browserId);
134 var responseQueue = [];
135 function setup_http_server() {
136 log("setup_http_server");
137 maxConnections = Services.prefs.getIntPref(
138 "network.http.max-persistent-connections-per-server"
140 FOCUSED_WINDOW_REQUEST_COUNT = Math.floor(maxConnections * 0.5);
141 NON_FOCUSED_WINDOW_REQUEST_COUNT =
142 maxConnections - FOCUSED_WINDOW_REQUEST_COUNT;
143 NON_FOCUSED_WINDOW_ID = FOCUSED_WINDOW_ID + FOCUSED_WINDOW_REQUEST_COUNT;
145 var allDummyHttpRequestReceived = false;
146 // Start server; will be stopped at test cleanup time.
147 server.registerPathHandler("/", function (metadata, response) {
148 var id = metadata.getHeader("X-ID");
149 log("Server recived the response id=" + id);
151 response.processAsync();
152 response.setHeader("X-ID", id);
153 responseQueue.push(response);
156 responseQueue.length == maxConnections &&
157 !allDummyHttpRequestReceived
159 log("received all dummy http requets");
160 allDummyHttpRequestReceived = true;
161 setup_nonFocusedWindowHttpRequests();
162 setup_focusedWindowHttpRequests();
164 } else if (responseQueue.length == maxConnections) {
165 var nonFocusedWindowResponses = responseQueue.slice(
167 NON_FOCUSED_WINDOW_REQUEST_COUNT
169 var focusedWindowResponses = responseQueue.slice(
170 NON_FOCUSED_WINDOW_REQUEST_COUNT,
173 check_response_id(nonFocusedWindowResponses, NON_FOCUSED_WINDOW_ID);
174 check_response_id(focusedWindowResponses, FOCUSED_WINDOW_ID);
179 registerCleanupFunction(function () {
180 server.stop(serverStopListener);
184 function processResponses() {
185 while (responseQueue.length) {
186 var resposne = responseQueue.pop();
191 function run_test() {
192 // Set "network.http.active_tab_priority" to false, so we can expect to
193 // receive http requests with higher priority first.
194 Services.prefs.setBoolPref("network.http.active_tab_priority", false);
197 setup_dummyHttpRequests();