Backed out changeset 48baafc34055 (bug 1789166) for causing mochitests failures....
[gecko.git] / netwerk / test / unit / test_dns_cancel.js
blobc20e63ae4cee9b697051b9db4e15ac2bef5e8661
1 "use strict";
3 var hostname1 = "";
4 var hostname2 = "";
5 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
7 for (var i = 0; i < 20; i++) {
8   hostname1 += possible.charAt(Math.floor(Math.random() * possible.length));
9   hostname2 += possible.charAt(Math.floor(Math.random() * possible.length));
12 var requestList1Canceled2;
13 var requestList1NotCanceled;
15 var requestList2Canceled;
16 var requestList2NotCanceled;
18 var listener1 = {
19   onLookupComplete(inRequest, inRecord, inStatus) {
20     // One request should be resolved and two request should be canceled.
21     if (inRequest == requestList1NotCanceled) {
22       // This request should not be canceled.
23       Assert.notEqual(inStatus, Cr.NS_ERROR_ABORT);
25       do_test_finished();
26     }
27   },
28   QueryInterface: ChromeUtils.generateQI(["nsIDNSListener"]),
31 var listener2 = {
32   onLookupComplete(inRequest, inRecord, inStatus) {
33     // One request should be resolved and the other canceled.
34     if (inRequest == requestList2NotCanceled) {
35       // The request should not be canceled.
36       Assert.notEqual(inStatus, Cr.NS_ERROR_ABORT);
38       do_test_finished();
39     }
40   },
41   QueryInterface: ChromeUtils.generateQI(["nsIDNSListener"]),
44 const defaultOriginAttributes = {};
46 function run_test() {
47   var mainThread = Services.tm.currentThread;
49   var flags = Ci.nsIDNSService.RESOLVE_BYPASS_CACHE;
51   // This one will be canceled with cancelAsyncResolve.
52   Services.dns.asyncResolve(
53     hostname2,
54     Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
55     flags,
56     null, // resolverInfo
57     listener1,
58     mainThread,
59     defaultOriginAttributes
60   );
61   Services.dns.cancelAsyncResolve(
62     hostname2,
63     Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
64     flags,
65     null, // resolverInfo
66     listener1,
67     Cr.NS_ERROR_ABORT,
68     defaultOriginAttributes
69   );
71   // This one will not be canceled.
72   requestList1NotCanceled = Services.dns.asyncResolve(
73     hostname1,
74     Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
75     flags,
76     null, // resolverInfo
77     listener1,
78     mainThread,
79     defaultOriginAttributes
80   );
82   // This one will be canceled with cancel(Cr.NS_ERROR_ABORT).
83   requestList1Canceled2 = Services.dns.asyncResolve(
84     hostname1,
85     Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
86     flags,
87     null, // resolverInfo
88     listener1,
89     mainThread,
90     defaultOriginAttributes
91   );
92   requestList1Canceled2.cancel(Cr.NS_ERROR_ABORT);
94   // This one will not be canceled.
95   requestList2NotCanceled = Services.dns.asyncResolve(
96     hostname1,
97     Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
98     flags,
99     null, // resolverInfo
100     listener2,
101     mainThread,
102     defaultOriginAttributes
103   );
105   // This one will be canceled with cancel(Cr.NS_ERROR_ABORT).
106   requestList2Canceled = Services.dns.asyncResolve(
107     hostname2,
108     Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
109     flags,
110     null, // resolverInfo
111     listener2,
112     mainThread,
113     defaultOriginAttributes
114   );
115   requestList2Canceled.cancel(Cr.NS_ERROR_ABORT);
117   do_test_pending();
118   do_test_pending();