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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
const HELPER_PAGE_URL =
"http://example.com/browser/dom/tests/browser/page_localstorage_snapshotting_e10s.html";
const HELPER_PAGE_ORIGIN = "http://example.com/";
/* import-globals-from helper_localStorage_e10s.js */
let testDir = gTestPath.substr(0, gTestPath.lastIndexOf("/"));
Services.scriptloader.loadSubScript(
testDir + "/helper_localStorage_e10s.js",
this
);
function clearOrigin() {
let principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
HELPER_PAGE_ORIGIN
);
let request = Services.qms.clearStoragesForPrincipal(
principal,
"default",
"ls"
);
let promise = new Promise(resolve => {
request.callback = () => {
resolve();
};
});
return promise;
}
async function applyMutations(knownTab, mutations) {
await SpecialPowers.spawn(knownTab.tab.linkedBrowser, [mutations], function(
mutations
) {
return content.wrappedJSObject.applyMutations(
Cu.cloneInto(mutations, content)
);
});
}
async function verifyState(knownTab, expectedState) {
let actualState = await SpecialPowers.spawn(
knownTab.tab.linkedBrowser,
[],
function() {
return content.wrappedJSObject.getState();
}
);
for (let [expectedKey, expectedValue] of Object.entries(expectedState)) {
ok(actualState.hasOwnProperty(expectedKey), "key present: " + expectedKey);
is(actualState[expectedKey], expectedValue, "value correct");
}
for (let actualKey of Object.keys(actualState)) {
if (!expectedState.hasOwnProperty(actualKey)) {
ok(false, "actual state has key it shouldn't have: " + actualKey);
}
}
}
async function getKeys(knownTab) {
let keys = await SpecialPowers.spawn(
knownTab.tab.linkedBrowser,
[],
function() {
return content.wrappedJSObject.getKeys();
}
);
return keys;
}
async function beginExplicitSnapshot(knownTab) {
await SpecialPowers.spawn(knownTab.tab.linkedBrowser, [], function() {
return content.wrappedJSObject.beginExplicitSnapshot();
});
}
async function endExplicitSnapshot(knownTab) {
await SpecialPowers.spawn(knownTab.tab.linkedBrowser, [], function() {
return content.wrappedJSObject.endExplicitSnapshot();
});
}
// We spin up a ton of child processes.
requestLongerTimeout(4);
/**
* Verify snapshotting of our localStorage implementation in multi-e10s setup.
*/
add_task(async function() {
if (!Services.domStorageManager.nextGenLocalStorageEnabled) {
ok(true, "Test ignored when the next gen local storage is not enabled.");
return;
}
await SpecialPowers.pushPrefEnv({
set: [
// Enable LocalStorage's testing API so we can explicitly create
// snapshots when needed.
["dom.storage.testing", true],
],
});
// Ensure that there is no localstorage data by forcing the origin to be
// cleared prior to the start of our test..
await clearOrigin();
// - Open tabs. Don't configure any of them yet.
const knownTabs = new KnownTabs();
const writerTab1 = await openTestTabInOwnProcess(
HELPER_PAGE_URL,
"writer1",
knownTabs
);
const writerTab2 = await openTestTabInOwnProcess(
HELPER_PAGE_URL,
"writer2",
knownTabs
);
const readerTab1 = await openTestTabInOwnProcess(
HELPER_PAGE_URL,
"reader1",
knownTabs
);
const readerTab2 = await openTestTabInOwnProcess(
HELPER_PAGE_URL,
"reader2",
knownTabs
);
const initialMutations = [
[null, null],
["key1", "initial1"],
["key2", "initial2"],
["key3", "initial3"],
["key5", "initial5"],
["key6", "initial6"],
["key7", "initial7"],
["key8", "initial8"],
];
const initialState = {
key1: "initial1",
key2: "initial2",
key3: "initial3",
key5: "initial5",
key6: "initial6",
key7: "initial7",
key8: "initial8",
};
let sizeOfOneKey;
let sizeOfOneValue;
let sizeOfOneItem;
let sizeOfKeys = 0;
let sizeOfItems = 0;
let entries = Object.entries(initialState);
for (let i = 0; i < entries.length; i++) {
let entry = entries[i];
let sizeOfKey = entry[0].length;
let sizeOfValue = entry[1].length;
let sizeOfItem = sizeOfKey + sizeOfValue;
if (i == 0) {
sizeOfOneKey = sizeOfKey;
sizeOfOneValue = sizeOfValue;
sizeOfOneItem = sizeOfItem;
}
sizeOfKeys += sizeOfKey;
sizeOfItems += sizeOfItem;
}
info("Size of one key is " + sizeOfOneKey);
info("Size of one value is " + sizeOfOneValue);
info("Size of one item is " + sizeOfOneItem);
info("Size of keys is " + sizeOfKeys);
info("Size of items is " + sizeOfItems);
const prefillValues = [
// Zero prefill (prefill disabled)
0,
// Less than one key length prefill
sizeOfOneKey - 1,
// Greater than one key length and less than one item length prefill
sizeOfOneKey + 1,
// Precisely one item length prefill
sizeOfOneItem,
// Precisely two times one item length prefill
2 * sizeOfOneItem,
// Precisely size of keys prefill
sizeOfKeys,
// Less than size of keys plus one value length prefill
sizeOfKeys + sizeOfOneValue - 1,
// Precisely size of keys plus one value length prefill
sizeOfKeys + sizeOfOneValue,
// Greater than size of keys plus one value length and less than size of
// keys plus two times one value length prefill
sizeOfKeys + sizeOfOneValue + 1,
// Precisely size of keys plus two times one value length prefill
sizeOfKeys + 2 * sizeOfOneValue,
// Precisely size of keys plus three times one value length prefill
sizeOfKeys + 3 * sizeOfOneValue,
// Precisely size of keys plus four times one value length prefill
sizeOfKeys + 4 * sizeOfOneValue,
// Precisely size of keys plus five times one value length prefill
sizeOfKeys + 5 * sizeOfOneValue,
// Precisely size of keys plus six times one value length prefill
sizeOfKeys + 6 * sizeOfOneValue,
// Precisely size of items prefill
sizeOfItems,
// Unlimited prefill
-1,
];
for (let prefillValue of prefillValues) {
info("Setting prefill value to " + prefillValue);
await SpecialPowers.pushPrefEnv({
set: [["dom.storage.snapshot_prefill", prefillValue]],
});
const gradualPrefillValues = [
// Zero gradual prefill
0,
// Less than one key length gradual prefill
sizeOfOneKey - 1,
// Greater than one key length and less than one item length gradual
// prefill
sizeOfOneKey + 1,
// Precisely one item length gradual prefill
sizeOfOneItem,
// Precisely two times one item length gradual prefill
2 * sizeOfOneItem,
// Precisely three times one item length gradual prefill
3 * sizeOfOneItem,
// Precisely four times one item length gradual prefill
4 * sizeOfOneItem,
// Precisely five times one item length gradual prefill
5 * sizeOfOneItem,
// Precisely six times one item length gradual prefill
6 * sizeOfOneItem,
// Precisely size of items prefill
sizeOfItems,
// Unlimited gradual prefill
-1,
];
for (let gradualPrefillValue of gradualPrefillValues) {
info("Setting gradual prefill value to " + gradualPrefillValue);
await SpecialPowers.pushPrefEnv({
set: [["dom.storage.snapshot_gradual_prefill", gradualPrefillValue]],
});
info("Stage 1");
const setRemoveMutations1 = [
["key0", "setRemove10"],
["key1", "setRemove11"],
["key2", null],
["key3", "setRemove13"],
["key4", "setRemove14"],
["key5", "setRemove15"],
["key6", "setRemove16"],
["key7", "setRemove17"],
["key8", null],
["key9", "setRemove19"],
];
const setRemoveState1 = {
key0: "setRemove10",
key1: "setRemove11",
key3: "setRemove13",
key4: "setRemove14",
key5: "setRemove15",
key6: "setRemove16",
key7: "setRemove17",
key9: "setRemove19",
};
const setRemoveMutations2 = [
["key0", "setRemove20"],
["key1", null],
["key2", "setRemove22"],
["key3", "setRemove23"],
["key4", "setRemove24"],
["key5", "setRemove25"],
["key6", "setRemove26"],
["key7", null],
["key8", "setRemove28"],
["key9", "setRemove29"],
];
const setRemoveState2 = {
key0: "setRemove20",
key2: "setRemove22",
key3: "setRemove23",
key4: "setRemove24",
key5: "setRemove25",
key6: "setRemove26",
key8: "setRemove28",
key9: "setRemove29",
};
// Apply initial mutations using an explicit snapshot. The explicit
// snapshot here ensures that the parent process have received the
// changes.
await beginExplicitSnapshot(writerTab1);
await applyMutations(writerTab1, initialMutations);
await endExplicitSnapshot(writerTab1);
// Begin explicit snapshots in all tabs except readerTab2. All these tabs
// should see the initial state regardless what other tabs are doing.
await beginExplicitSnapshot(writerTab1);
await beginExplicitSnapshot(writerTab2);
await beginExplicitSnapshot(readerTab1);
// Apply first array of set/remove mutations in writerTab1 and end the
// explicit snapshot. This will trigger saving of values in other active
// snapshots.
await applyMutations(writerTab1, setRemoveMutations1);
await endExplicitSnapshot(writerTab1);
// Begin an explicit snapshot in readerTab2. writerTab1 already ended its
// explicit snapshot, so readerTab2 should see mutations done by
// writerTab1.
await beginExplicitSnapshot(readerTab2);
// Apply second array of set/remove mutations in writerTab2 and end the
// explicit snapshot. This will trigger saving of values in other active
// snapshots, but only if they haven't been saved already.
await applyMutations(writerTab2, setRemoveMutations2);
await endExplicitSnapshot(writerTab2);
// Verify state in readerTab1, it should match the initial state.
await verifyState(readerTab1, initialState);
await endExplicitSnapshot(readerTab1);
// Verify state in readerTab2, it should match the state after the first
// array of set/remove mutatations have been applied and "commited".
await verifyState(readerTab2, setRemoveState1);
await endExplicitSnapshot(readerTab2);
// Verify final state, it should match the state after the second array of
// set/remove mutation have been applied and "commited". An explicit
// snapshot is used.
await beginExplicitSnapshot(readerTab1);
await verifyState(readerTab1, setRemoveState2);
await endExplicitSnapshot(readerTab1);
info("Stage 2");
const setRemoveClearMutations1 = [
["key0", "setRemoveClear10"],
["key1", null],
[null, null],
];
const setRemoveClearState1 = {};
const setRemoveClearMutations2 = [
["key8", null],
["key9", "setRemoveClear29"],
[null, null],
];
const setRemoveClearState2 = {};
// This is very similar to previous stage except that in addition to
// set/remove, the clear operation is involved too.
await beginExplicitSnapshot(writerTab1);
await applyMutations(writerTab1, initialMutations);
await endExplicitSnapshot(writerTab1);
await beginExplicitSnapshot(writerTab1);
await beginExplicitSnapshot(writerTab2);
await beginExplicitSnapshot(readerTab1);
await applyMutations(writerTab1, setRemoveClearMutations1);
await endExplicitSnapshot(writerTab1);
await beginExplicitSnapshot(readerTab2);
await applyMutations(writerTab2, setRemoveClearMutations2);
await endExplicitSnapshot(writerTab2);
await verifyState(readerTab1, initialState);
await endExplicitSnapshot(readerTab1);
await verifyState(readerTab2, setRemoveClearState1);
await endExplicitSnapshot(readerTab2);
await beginExplicitSnapshot(readerTab1);
await verifyState(readerTab1, setRemoveClearState2);
await endExplicitSnapshot(readerTab1);
info("Stage 3");
const changeOrderMutations = [
["key1", null],
["key2", null],
["key3", null],
["key5", null],
["key6", null],
["key7", null],
["key8", null],
["key8", "initial8"],
["key7", "initial7"],
["key6", "initial6"],
["key5", "initial5"],
["key3", "initial3"],
["key2", "initial2"],
["key1", "initial1"],
];
// Apply initial mutations using an explicit snapshot. The explicit
// snapshot here ensures that the parent process have received the
// changes.
await beginExplicitSnapshot(writerTab1);
await applyMutations(writerTab1, initialMutations);
await endExplicitSnapshot(writerTab1);
// Begin explicit snapshots in all tabs except writerTab2 which is not
// used in this stage. All these tabs should see the initial order
// regardless what other tabs are doing.
await beginExplicitSnapshot(readerTab1);
await beginExplicitSnapshot(writerTab1);
await beginExplicitSnapshot(readerTab2);
// Get all keys in readerTab1 and end the explicit snapshot. No mutations
// have been applied yet.
let tab1Keys = await getKeys(readerTab1);
await endExplicitSnapshot(readerTab1);
// Apply mutations that change the order of keys and end the explicit
// snapshot. The state is unchanged. This will trigger saving of key order
// in other active snapshots, but only if the order hasn't been saved
// already.
await applyMutations(writerTab1, changeOrderMutations);
await endExplicitSnapshot(writerTab1);
// Get all keys in readerTab2 and end the explicit snapshot. Change order
// mutations have been applied, but the order should stay unchanged.
let tab2Keys = await getKeys(readerTab2);
await endExplicitSnapshot(readerTab2);
// Verify the key order is the same.
is(tab2Keys.length, tab1Keys.length, "Correct keys length");
for (let i = 0; i < tab2Keys.length; i++) {
is(tab2Keys[i], tab1Keys[i], "Correct key");
}
// Verify final state, it should match the initial state since applied
// mutations only changed the key order. An explicit snapshot is used.
await beginExplicitSnapshot(readerTab1);
await verifyState(readerTab1, initialState);
await endExplicitSnapshot(readerTab1);
}
}
// - Clean up.
await cleanupTabs(knownTabs);
clearOrigin();
});
|