Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_h2proxy_connection_limit.js
blobf326b48b40e9aa52a4d6e9311eff49f68f1a1407
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // Summary:
6 // Test whether the connection limit is honored when http2 proxy is used.
7 //
8 // Test step:
9 // 1. Create 30 http requests.
10 // 2. Check if the count of all sockets created by proxy is less than 6.
12 "use strict";
14 /* import-globals-from head_cache.js */
15 /* import-globals-from head_cookies.js */
16 /* import-globals-from head_channels.js */
17 /* import-globals-from head_servers.js */
19 function makeChan(uri) {
20 let chan = NetUtil.newChannel({
21 uri,
22 loadUsingSystemPrincipal: true,
23 }).QueryInterface(Ci.nsIHttpChannel);
24 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
25 return chan;
28 add_task(async function test_connection_limit() {
29 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
30 Ci.nsIX509CertDB
32 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
33 addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
35 let proxy = new NodeHTTP2ProxyServer();
36 await proxy.start();
37 registerCleanupFunction(async () => {
38 await proxy.stop();
39 });
41 const maxConnections = 6;
42 Services.prefs.setIntPref(
43 "network.http.max-persistent-connections-per-server",
44 maxConnections
46 registerCleanupFunction(async () => {
47 Services.prefs.clearUserPref(
48 "network.http.max-persistent-connections-per-server"
50 });
52 await with_node_servers([NodeHTTP2Server], async server => {
53 await server.registerPathHandler("/test", (req, resp) => {
54 resp.writeHead(200);
55 resp.end("All good");
56 });
58 let promises = [];
59 for (let i = 0; i < 30; ++i) {
60 let chan = makeChan(`${server.origin()}/test`);
61 promises.push(
62 new Promise(resolve => {
63 chan.asyncOpen(
64 new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL)
69 await Promise.all(promises);
70 let count = await proxy.socketCount(server.port());
71 Assert.lessOrEqual(
72 count,
73 maxConnections,
74 "socket count should be less than maxConnections"
76 });
77 });