1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
"use strict";
add_task(setup);
add_task(async function test_TRRRacer_cleanRun() {
let deferred = PromiseUtils.defer();
let racer = new TRRRacer(() => {
deferred.resolve();
deferred.resolved = true;
}, trrList);
racer.run();
await deferred.promise;
Assert.equal(racer._retryCount, 1);
let events = Services.telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
true
).parent;
Assert.ok(events);
events = events.filter(e => e[1] == "security.doh.trrPerformance");
Assert.equal(events.length, racer._aggregator.totalLookups);
Services.telemetry.clearEvents();
// Simulate network changes and ensure no re-runs since it's already complete.
async function testNetworkChange(captivePortal = false) {
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login");
} else {
Services.obs.notifyObservers(null, "network:link-status-changed", "down");
}
Assert.ok(!racer._aggregator.aborted);
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login-success");
} else {
Services.obs.notifyObservers(null, "network:link-status-changed", "up");
}
Assert.equal(racer._retryCount, 1);
ensureNoTelemetry();
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login-abort");
}
}
testNetworkChange(false);
testNetworkChange(true);
});
async function test_TRRRacer_networkFlux_helper(captivePortal = false) {
let deferred = PromiseUtils.defer();
let racer = new TRRRacer(() => {
deferred.resolve();
deferred.resolved = true;
}, trrList);
racer.run();
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login");
} else {
Services.obs.notifyObservers(null, "network:link-status-changed", "down");
}
Assert.ok(racer._aggregator.aborted);
ensureNoTelemetry();
Assert.equal(racer._retryCount, 1);
Assert.ok(!deferred.resolved);
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login-success");
} else {
Services.obs.notifyObservers(null, "network:link-status-changed", "up");
}
Assert.ok(!racer._aggregator.aborted);
await deferred.promise;
Assert.equal(racer._retryCount, 2);
let events = Services.telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
true
).parent;
Assert.ok(events);
events = events.filter(e => e[1] == "security.doh.trrPerformance");
Assert.equal(events.length, racer._aggregator.totalLookups);
Services.telemetry.clearEvents();
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login-abort");
}
}
add_task(async function test_TRRRacer_networkFlux() {
await test_TRRRacer_networkFlux_helper(false);
await test_TRRRacer_networkFlux_helper(true);
});
async function test_TRRRacer_maxRetries_helper(captivePortal = false) {
let deferred = PromiseUtils.defer();
let racer = new TRRRacer(() => {
deferred.resolve();
deferred.resolved = true;
}, trrList);
racer.run();
info("ran new racer");
// Start at i = 1 since we're already at retry #1.
for (let i = 1; i < 5; ++i) {
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login");
} else {
Services.obs.notifyObservers(null, "network:link-status-changed", "down");
}
info("notified observers");
Assert.ok(racer._aggregator.aborted);
ensureNoTelemetry();
Assert.equal(racer._retryCount, i);
Assert.ok(!deferred.resolved);
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login-success");
} else {
Services.obs.notifyObservers(null, "network:link-status-changed", "up");
}
}
// Simulate a "down" network event and ensure we still send telemetry
// since we've maxed out our retry count.
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login");
} else {
Services.obs.notifyObservers(null, "network:link-status-changed", "down");
}
Assert.ok(!racer._aggregator.aborted);
await deferred.promise;
Assert.equal(racer._retryCount, 5);
let events = Services.telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
true
).parent;
Assert.ok(events);
events = events.filter(e => e[1] == "security.doh.trrPerformance");
Assert.equal(events.length, racer._aggregator.totalLookups);
Services.telemetry.clearEvents();
if (captivePortal) {
Services.obs.notifyObservers(null, "captive-portal-login-abort");
}
}
add_task(async function test_TRRRacer_maxRetries() {
await test_TRRRacer_maxRetries_helper(false);
await test_TRRRacer_maxRetries_helper(true);
});
add_task(async function test_TRRRacer_getFastestTRRFromResults() {
let results = [
{ trr: "trr1", time: 10 },
{ trr: "trr2", time: 100 },
{ trr: "trr1", time: 1000 },
{ trr: "trr2", time: 110 },
{ trr: "trr3", time: -1 },
{ trr: "trr4", time: -1 },
{ trr: "trr4", time: -1 },
{ trr: "trr4", time: 1 },
{ trr: "trr4", time: 1 },
{ trr: "trr5", time: 10 },
{ trr: "trr5", time: 20 },
{ trr: "trr5", time: 1000 },
];
let racer = new TRRRacer(undefined, trrList);
let fastest = racer._getFastestTRRFromResults(results);
// trr1's geometric mean is 100
// trr2's geometric mean is 110
// trr3 has no valid times, excluded
// trr4 has 50% invalid times, excluded
// trr5's geometric mean is ~58.5, it's the winner.
Assert.equal(fastest, "trr5");
// When no valid entries are available, undefined is the default output.
results = [
{ trr: "trr1", time: -1 },
{ trr: "trr2", time: -1 },
];
fastest = racer._getFastestTRRFromResults(results);
Assert.equal(fastest, undefined);
// When passing `returnRandomDefault = true`, verify that both TRRs are
// possible outputs. The probability that the randomization is working
// correctly and we consistently get the same output after 50 iterations is
// 0.5^50 ~= 8.9*10^-16.
let firstResult = racer._getFastestTRRFromResults(results, true);
while (racer._getFastestTRRFromResults(results, true) == firstResult) {
continue;
}
Assert.ok(true, "Both TRRs were possible outputs when all results invalid.");
});
|