Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_trr_cancel.js
blob60b65d7eafb17b2817ef6d666fd18bdb3b42de06
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 "use strict";
7 trr_test_setup();
8 registerCleanupFunction(async () => {
9 trr_clear_prefs();
10 });
12 let trrServer = null;
13 add_task(async function start_trr_server() {
14 trrServer = new TRRServer();
15 registerCleanupFunction(async () => {
16 await trrServer.stop();
17 });
18 await trrServer.start();
19 dump(`port = ${trrServer.port()}\n`);
21 Services.prefs.setCharPref(
22 "network.trr.uri",
23 `https://foo.example.com:${trrServer.port()}/dns-query`
25 Services.prefs.setIntPref("network.trr.mode", Ci.nsIDNSService.MODE_TRRFIRST);
26 });
28 add_task(async function sanity_check() {
29 await trrServer.registerDoHAnswers("example.com", "A", {
30 answers: [
32 name: "example.com",
33 ttl: 55,
34 type: "A",
35 flush: false,
36 data: "1.2.3.4",
39 });
40 // Simple check to see that TRR works.
41 await new TRRDNSListener("example.com", { expectedAnswer: "1.2.3.4" });
42 });
44 // Cancelling the request is not sync when using the socket process, so
45 // we skip this test when it's enabled.
46 add_task(
47 { skip_if: () => mozinfo.socketprocess_networking },
48 async function cancel_immediately() {
49 await trrServer.registerDoHAnswers("example.org", "A", {
50 answers: [
52 name: "example.org",
53 ttl: 55,
54 type: "A",
55 flush: false,
56 data: "2.3.4.5",
59 });
60 let r1 = new TRRDNSListener("example.org", { expectedSuccess: false });
61 let r2 = new TRRDNSListener("example.org", { expectedAnswer: "2.3.4.5" });
62 r1.cancel();
63 let { inStatus } = await r1;
64 equal(inStatus, Cr.NS_ERROR_ABORT);
65 await r2;
66 equal(await trrServer.requestCount("example.org", "A"), 1);
68 // Now we cancel both of them
69 Services.dns.clearCache(true);
70 r1 = new TRRDNSListener("example.org", { expectedSuccess: false });
71 r2 = new TRRDNSListener("example.org", { expectedSuccess: false });
72 r1.cancel();
73 r2.cancel();
74 ({ inStatus } = await r1);
75 equal(inStatus, Cr.NS_ERROR_ABORT);
76 ({ inStatus } = await r2);
77 equal(inStatus, Cr.NS_ERROR_ABORT);
78 await new Promise(resolve => do_timeout(50, resolve));
79 equal(await trrServer.requestCount("example.org", "A"), 2);
83 add_task(async function cancel_delayed() {
84 Services.dns.clearCache(true);
85 await trrServer.registerDoHAnswers("example.com", "A", {
86 answers: [
88 name: "example.com",
89 ttl: 55,
90 type: "A",
91 flush: false,
92 data: "1.1.1.1",
95 delay: 500,
96 });
97 let r1 = new TRRDNSListener("example.com", { expectedSuccess: false });
98 let r2 = new TRRDNSListener("example.com", { expectedAnswer: "1.1.1.1" });
99 await new Promise(resolve => do_timeout(50, resolve));
100 r1.cancel();
101 let { inStatus } = await r1;
102 equal(inStatus, Cr.NS_ERROR_ABORT);
103 await r2;
106 add_task(async function cancel_after_completed() {
107 Services.dns.clearCache(true);
108 await trrServer.registerDoHAnswers("example.com", "A", {
109 answers: [
111 name: "example.com",
112 ttl: 55,
113 type: "A",
114 flush: false,
115 data: "2.2.2.2",
119 let r1 = new TRRDNSListener("example.com", { expectedAnswer: "2.2.2.2" });
120 await r1;
121 let r2 = new TRRDNSListener("example.com", { expectedAnswer: "2.2.2.2" });
122 // Check that cancelling r1 after it's complete does not affect r2 in any way.
123 r1.cancel();
124 await r2;
127 add_task(async function clearCacheWhileResolving() {
128 Services.dns.clearCache(true);
129 await trrServer.registerDoHAnswers("example.com", "A", {
130 answers: [
132 name: "example.com",
133 ttl: 55,
134 type: "A",
135 flush: false,
136 data: "3.3.3.3",
139 delay: 500,
141 // Check that calling clearCache does not leave the request hanging.
142 let r1 = new TRRDNSListener("example.com", { expectedAnswer: "3.3.3.3" });
143 let r2 = new TRRDNSListener("example.com", { expectedAnswer: "3.3.3.3" });
144 Services.dns.clearCache(true);
145 await r1;
146 await r2;
148 // Also check the same for HTTPS records
149 await trrServer.registerDoHAnswers("httpsvc.com", "HTTPS", {
150 answers: [
152 name: "httpsvc.com",
153 ttl: 55,
154 type: "HTTPS",
155 flush: false,
156 data: {
157 priority: 1,
158 name: "test.p1.com",
159 values: [{ key: "alpn", value: ["h2", "h3"] }],
163 delay: 500,
165 let r3 = new TRRDNSListener("httpsvc.com", {
166 type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
168 let r4 = new TRRDNSListener("httpsvc.com", {
169 type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
171 Services.dns.clearCache(true);
172 await r3;
173 await r4;
174 equal(await trrServer.requestCount("httpsvc.com", "HTTPS"), 1);
175 Services.dns.clearCache(true);
176 await new TRRDNSListener("httpsvc.com", {
177 type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
179 equal(await trrServer.requestCount("httpsvc.com", "HTTPS"), 2);