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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
Cu.import("resource://testing-common/httpd.js");
XPCOMUtils.defineLazyModuleGetter(this, "Experiments",
"resource:///modules/experiments/Experiments.jsm");
const MANIFEST_HANDLER = "manifests/handler";
const SEC_IN_ONE_DAY = 24 * 60 * 60;
const MS_IN_ONE_DAY = SEC_IN_ONE_DAY * 1000;
var gHttpServer = null;
var gHttpRoot = null;
var gDataRoot = null;
var gPolicy = null;
var gManifestObject = null;
var gManifestHandlerURI = null;
function run_test() {
run_next_test();
}
add_task(function* test_setup() {
loadAddonManager();
yield removeCacheFile();
gHttpServer = new HttpServer();
gHttpServer.start(-1);
let port = gHttpServer.identity.primaryPort;
gHttpRoot = "http://localhost:" + port + "/";
gDataRoot = gHttpRoot + "data/";
gManifestHandlerURI = gHttpRoot + MANIFEST_HANDLER;
gHttpServer.registerDirectory("/data/", do_get_cwd());
gHttpServer.registerPathHandler("/" + MANIFEST_HANDLER, (request, response) => {
response.setStatusLine(null, 200, "OK");
response.write(JSON.stringify(gManifestObject));
response.processAsync();
response.finish();
});
do_register_cleanup(() => gHttpServer.stop(() => {}));
Services.prefs.setBoolPref(PREF_EXPERIMENTS_ENABLED, true);
Services.prefs.setIntPref(PREF_LOGGING_LEVEL, 0);
Services.prefs.setBoolPref(PREF_LOGGING_DUMP, true);
Services.prefs.setCharPref(PREF_MANIFEST_URI, gManifestHandlerURI);
Services.prefs.setIntPref(PREF_FETCHINTERVAL, 0);
gPolicy = new Experiments.Policy();
patchPolicy(gPolicy, {
updatechannel: () => "nightly",
oneshotTimer: (callback, timeout, thisObj, name) => {},
});
});
function checkExperimentListsEqual(list, list2) {
Assert.equal(list.length, list2.length, "Lists should have the same length.")
for (let i = 0; i < list.length; ++i) {
for (let k of Object.keys(list[i])) {
Assert.equal(list[i][k], list2[i][k],
"Field '" + k + "' should match for list entry " + i + ".");
}
}
}
function checkExperimentSerializations(experimentEntryIterator) {
for (let experiment of experimentEntryIterator) {
let experiment2 = new Experiments.ExperimentEntry(gPolicy);
let jsonStr = JSON.stringify(experiment.toJSON());
Assert.ok(experiment2.initFromCacheData(JSON.parse(jsonStr)),
"Should have initialized successfully from JSON serialization.");
Assert.equal(JSON.stringify(experiment), JSON.stringify(experiment2),
"Object stringifications should match.");
}
}
function validateCache(cachedExperiments, experimentIds) {
let cachedExperimentIds = new Set(cachedExperiments);
Assert.equal(cachedExperimentIds.size, experimentIds.length,
"The number of cached experiments does not match with the provided list");
for (let id of experimentIds) {
Assert.ok(cachedExperimentIds.has(id), "The cache must contain the experiment with id " + id);
}
}
// Set up an experiments instance and check if it is properly restored from cache.
add_task(function* test_cache() {
// The manifest data we test with.
gManifestObject = {
"version": 1,
experiments: [
{
id: EXPERIMENT1_ID,
xpiURL: gDataRoot + EXPERIMENT1_XPI_NAME,
xpiHash: EXPERIMENT1_XPI_SHA1,
maxActiveSeconds: 10 * SEC_IN_ONE_DAY,
appName: ["XPCShell"],
channel: ["nightly"],
},
{
id: EXPERIMENT2_ID,
xpiURL: gDataRoot + EXPERIMENT2_XPI_NAME,
xpiHash: EXPERIMENT2_XPI_SHA1,
maxActiveSeconds: 10 * SEC_IN_ONE_DAY,
appName: ["XPCShell"],
channel: ["nightly"],
},
{
id: EXPERIMENT3_ID,
xpiURL: "https://inval.id/foo.xpi",
xpiHash: "sha1:0000000000000000000000000000000000000000",
maxActiveSeconds: 10 * SEC_IN_ONE_DAY,
appName: ["XPCShell"],
channel: ["nightly"],
},
],
};
// Setup dates for the experiments.
let baseDate = new Date(2014, 5, 1, 12);
let startDates = [];
let endDates = [];
for (let i = 0; i < gManifestObject.experiments.length; ++i) {
let experiment = gManifestObject.experiments[i];
startDates.push(futureDate(baseDate, (50 + (150 * i)) * MS_IN_ONE_DAY));
endDates .push(futureDate(startDates[i], 50 * MS_IN_ONE_DAY));
experiment.startTime = dateToSeconds(startDates[i]);
experiment.endTime = dateToSeconds(endDates[i]);
}
// Data to compare the result of Experiments.getExperiments() against.
let experimentListData = [
{
id: EXPERIMENT2_ID,
name: "Test experiment 2",
description: "And yet another experiment that experiments experimentally.",
},
{
id: EXPERIMENT1_ID,
name: EXPERIMENT1_NAME,
description: "Yet another experiment that experiments experimentally.",
},
];
// Trigger update & re-init, clock set to before any activation.
let now = baseDate;
defineNow(gPolicy, now);
let experiments = new Experiments.Experiments(gPolicy);
yield experiments.updateManifest();
let list = yield experiments.getExperiments();
Assert.equal(list.length, 0, "Experiment list should be empty.");
checkExperimentSerializations(experiments._experiments.values());
yield promiseRestartManager();
experiments = new Experiments.Experiments(gPolicy);
yield experiments._run();
list = yield experiments.getExperiments();
Assert.equal(list.length, 0, "Experiment list should be empty.");
checkExperimentSerializations(experiments._experiments.values());
// Re-init, clock set for experiment 1 to start.
now = futureDate(startDates[0], 5 * MS_IN_ONE_DAY);
defineNow(gPolicy, now);
yield promiseRestartManager();
experiments = new Experiments.Experiments(gPolicy);
yield experiments._run();
list = yield experiments.getExperiments();
Assert.equal(list.length, 1, "Experiment list should have 1 entry now.");
experimentListData[1].active = true;
experimentListData[1].endDate = now.getTime() + 10 * MS_IN_ONE_DAY;
checkExperimentListsEqual(experimentListData.slice(1), list);
checkExperimentSerializations(experiments._experiments.values());
let branch = yield experiments.getExperimentBranch(EXPERIMENT1_ID);
Assert.strictEqual(branch, null);
yield experiments.setExperimentBranch(EXPERIMENT1_ID, "testbranch");
branch = yield experiments.getExperimentBranch(EXPERIMENT1_ID);
Assert.strictEqual(branch, "testbranch");
// Re-init, clock set for experiment 1 to stop.
now = futureDate(now, 20 * MS_IN_ONE_DAY);
defineNow(gPolicy, now);
yield promiseRestartManager();
experiments = new Experiments.Experiments(gPolicy);
yield experiments._run();
list = yield experiments.getExperiments();
Assert.equal(list.length, 1, "Experiment list should have 1 entry.");
experimentListData[1].active = false;
experimentListData[1].endDate = now.getTime();
checkExperimentListsEqual(experimentListData.slice(1), list);
checkExperimentSerializations(experiments._experiments.values());
branch = yield experiments.getExperimentBranch(EXPERIMENT1_ID);
Assert.strictEqual(branch, "testbranch");
// Re-init, clock set for experiment 2 to start.
now = futureDate(startDates[1], 20 * MS_IN_ONE_DAY);
defineNow(gPolicy, now);
yield promiseRestartManager();
experiments = new Experiments.Experiments(gPolicy);
yield experiments._run();
list = yield experiments.getExperiments();
Assert.equal(list.length, 2, "Experiment list should have 2 entries.");
experimentListData[0].active = true;
experimentListData[0].endDate = now.getTime() + 10 * MS_IN_ONE_DAY;
checkExperimentListsEqual(experimentListData, list);
checkExperimentSerializations(experiments._experiments.values());
// Re-init, clock set for experiment 2 to stop.
now = futureDate(now, 20 * MS_IN_ONE_DAY);
defineNow(gPolicy, now);
yield promiseRestartManager();
experiments = new Experiments.Experiments(gPolicy);
yield experiments._run();
list = yield experiments.getExperiments();
Assert.equal(list.length, 2, "Experiment list should have 2 entries.");
experimentListData[0].active = false;
experimentListData[0].endDate = now.getTime();
checkExperimentListsEqual(experimentListData, list);
checkExperimentSerializations(experiments._experiments.values());
// Cleanup.
yield experiments._toggleExperimentsEnabled(false);
yield promiseRestartManager();
yield removeCacheFile();
});
add_task(function* test_expiration() {
// The manifest data we test with.
gManifestObject = {
"version": 1,
experiments: [
{
id: EXPERIMENT1_ID,
xpiURL: gDataRoot + EXPERIMENT1_XPI_NAME,
xpiHash: EXPERIMENT1_XPI_SHA1,
maxActiveSeconds: 10 * SEC_IN_ONE_DAY,
appName: ["XPCShell"],
channel: ["nightly"],
},
{
id: EXPERIMENT2_ID,
xpiURL: gDataRoot + EXPERIMENT2_XPI_NAME,
xpiHash: EXPERIMENT2_XPI_SHA1,
maxActiveSeconds: 50 * SEC_IN_ONE_DAY,
appName: ["XPCShell"],
channel: ["nightly"],
},
// The 3rd experiment will never run, so it's ok to use experiment's 2 data.
{
id: EXPERIMENT3_ID,
xpiURL: gDataRoot + EXPERIMENT2_XPI_NAME,
xpiHash: EXPERIMENT2_XPI_SHA1,
maxActiveSeconds: 10 * SEC_IN_ONE_DAY,
appName: ["XPCShell"],
channel: ["nightly"],
}
],
};
// Data to compare the result of Experiments.getExperiments() against.
let experimentListData = [
{
id: EXPERIMENT2_ID,
name: "Test experiment 2",
description: "And yet another experiment that experiments experimentally.",
},
{
id: EXPERIMENT1_ID,
name: EXPERIMENT1_NAME,
description: "Yet another experiment that experiments experimentally.",
},
];
// Setup dates for the experiments.
let baseDate = new Date(2014, 5, 1, 12);
let startDates = [];
let endDates = [];
for (let i = 0; i < gManifestObject.experiments.length; ++i) {
let experiment = gManifestObject.experiments[i];
// Spread out experiments in time so that one experiment can end and expire while
// the next is still running.
startDates.push(futureDate(baseDate, (50 + (200 * i)) * MS_IN_ONE_DAY));
endDates .push(futureDate(startDates[i], 50 * MS_IN_ONE_DAY));
experiment.startTime = dateToSeconds(startDates[i]);
experiment.endTime = dateToSeconds(endDates[i]);
}
let now = null;
let experiments = null;
let setDateAndRestartExperiments = new Task.async(function* (newDate) {
now = newDate;
defineNow(gPolicy, now);
yield promiseRestartManager();
experiments = new Experiments.Experiments(gPolicy);
yield experiments._run();
});
// Trigger update & re-init, clock set to before any activation.
now = baseDate;
defineNow(gPolicy, now);
experiments = new Experiments.Experiments(gPolicy);
yield experiments.updateManifest();
let list = yield experiments.getExperiments();
Assert.equal(list.length, 0, "Experiment list should be empty.");
// Re-init, clock set for experiment 1 to start...
yield setDateAndRestartExperiments(startDates[0]);
list = yield experiments.getExperiments();
Assert.equal(list.length, 1, "The first experiment should have started.");
// ... init again, and set the clock so that the first experiment ends.
yield setDateAndRestartExperiments(endDates[0]);
// The experiment just ended, it should still be in the cache, but marked
// as finished.
list = yield experiments.getExperiments();
Assert.equal(list.length, 1, "Experiment list should have 1 entry.");
experimentListData[1].active = false;
experimentListData[1].endDate = now.getTime();
checkExperimentListsEqual(experimentListData.slice(1), list);
validateCache([...experiments._experiments.keys()], [EXPERIMENT1_ID, EXPERIMENT2_ID, EXPERIMENT3_ID]);
// Start the second experiment.
yield setDateAndRestartExperiments(startDates[1]);
// The experiments cache should contain the finished experiment and the
// one that's still running.
list = yield experiments.getExperiments();
Assert.equal(list.length, 2, "Experiment list should have 2 entries.");
experimentListData[0].active = true;
experimentListData[0].endDate = now.getTime() + 50 * MS_IN_ONE_DAY;
checkExperimentListsEqual(experimentListData, list);
// Move the clock in the future, just 31 days after the start date of the second experiment,
// so that the cache for the first experiment expires and the second experiment is still running.
yield setDateAndRestartExperiments(futureDate(startDates[1], 31 * MS_IN_ONE_DAY));
validateCache([...experiments._experiments.keys()], [EXPERIMENT2_ID, EXPERIMENT3_ID]);
// Make sure that the expired experiment is not reported anymore.
let history = yield experiments.getExperiments();
Assert.equal(history.length, 1, "Experiments older than 180 days must be removed from the cache.");
// Test that we don't write expired experiments in the cache.
yield setDateAndRestartExperiments(now);
validateCache([...experiments._experiments.keys()], [EXPERIMENT2_ID, EXPERIMENT3_ID]);
// The first experiment should be expired and not in the cache, it ended more than
// 180 days ago. We should see the one still running in the cache.
history = yield experiments.getExperiments();
Assert.equal(history.length, 1, "Expired experiments must not be saved to cache.");
checkExperimentListsEqual(experimentListData.slice(0, 1), history);
// Test that experiments that are cached locally but never ran are removed from cache
// when they are removed from the manifest (this is cached data, not really history).
gManifestObject["experiments"] = gManifestObject["experiments"].slice(1, 1);
yield experiments.updateManifest();
validateCache([...experiments._experiments.keys()], [EXPERIMENT2_ID]);
// Cleanup.
yield experiments._toggleExperimentsEnabled(false);
yield promiseRestartManager();
yield removeCacheFile();
});
|