Bug 1845311 - [Part 2] Use ChromeUtils.defineLazyGetter in more places r=arai,webcomp...
[gecko.git] / netwerk / test / unit / test_separate_connections.js
blob4f5c82ec979b1a84cee2022335b99965abd0b925
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4   "resource://testing-common/httpd.sys.mjs"
5 );
7 ChromeUtils.defineLazyGetter(this, "URL", function () {
8   return "http://localhost:" + httpserv.identity.primaryPort;
9 });
11 // This unit test ensures each container has its own connection pool.
12 // We verify this behavior by opening channels with different userContextId,
13 // and their connection info's hash keys should be different.
15 // In the first round of this test, we record the hash key in each container.
16 // In the second round, we check if each container's hash key is consistent
17 // and different from other container's hash key.
19 let httpserv = null;
20 let gSecondRoundStarted = false;
22 function handler(metadata, response) {
23   response.setHeader("Content-Type", "text/plain", false);
24   response.setHeader("Cache-Control", "no-cache", false);
25   response.setStatusLine(metadata.httpVersion, 200, "OK");
26   let body = "0123456789";
27   response.bodyOutputStream.write(body, body.length);
30 function makeChan(url, userContextId) {
31   let chan = NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
32   chan.loadInfo.originAttributes = { userContextId };
33   return chan;
36 let previousHashKeys = [];
38 function Listener(userContextId) {
39   this.userContextId = userContextId;
42 let gTestsRun = 0;
43 Listener.prototype = {
44   onStartRequest(request) {
45     request
46       .QueryInterface(Ci.nsIHttpChannel)
47       .QueryInterface(Ci.nsIHttpChannelInternal);
49     Assert.equal(
50       request.loadInfo.originAttributes.userContextId,
51       this.userContextId
52     );
54     let hashKey = request.connectionInfoHashKey;
55     if (gSecondRoundStarted) {
56       // Compare the hash keys with the previous set ones.
57       // Hash keys should match if and only if their userContextId are the same.
58       for (let userContextId = 0; userContextId < 3; userContextId++) {
59         if (userContextId == this.userContextId) {
60           Assert.equal(hashKey, previousHashKeys[userContextId]);
61         } else {
62           Assert.notEqual(hashKey, previousHashKeys[userContextId]);
63         }
64       }
65     } else {
66       // Set the hash keys in the first round.
67       previousHashKeys[this.userContextId] = hashKey;
68     }
69   },
70   onDataAvailable(request, stream, off, cnt) {
71     read_stream(stream, cnt);
72   },
73   onStopRequest() {
74     gTestsRun++;
75     if (gTestsRun == 3) {
76       gTestsRun = 0;
77       if (gSecondRoundStarted) {
78         // The second round finishes.
79         httpserv.stop(do_test_finished);
80       } else {
81         // The first round finishes. Do the second round.
82         gSecondRoundStarted = true;
83         doTest();
84       }
85     }
86   },
89 function doTest() {
90   for (let userContextId = 0; userContextId < 3; userContextId++) {
91     let chan = makeChan(URL, userContextId);
92     let listener = new Listener(userContextId);
93     chan.asyncOpen(listener);
94   }
97 function run_test() {
98   do_test_pending();
99   httpserv = new HttpServer();
100   httpserv.registerPathHandler("/", handler);
101   httpserv.start(-1);
103   doTest();