summaryrefslogtreecommitdiffstats
path: root/dom/localstorage/test/unit/test_snapshotting.js
blob: 4b639395f7dde27b1635d4d129fa539a7cab6aa7 (plain)
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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

add_task(async function testSteps() {
  const url = "http://example.com";

  info("Setting pref");

  Services.prefs.setBoolPref("dom.storage.snapshot_reusing", false);

  const items = [
    { key: "key01", value: "value01" },
    { key: "key02", value: "value02" },
    { key: "key03", value: "value03" },
    { key: "key04", value: "value04" },
    { key: "key05", value: "value05" },
    { key: "key06", value: "value06" },
    { key: "key07", value: "value07" },
    { key: "key08", value: "value08" },
    { key: "key09", value: "value09" },
    { key: "key10", value: "value10" },
  ];

  let sizeOfOneKey;
  let sizeOfOneValue;
  let sizeOfOneItem;
  let sizeOfKeys = 0;
  let sizeOfItems = 0;

  for (let i = 0; i < items.length; i++) {
    let item = items[i];
    let sizeOfKey = item.key.length;
    let sizeOfValue = item.value.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 three times one item length prefill
    3 * sizeOfOneItem,
    // Precisely four times one item length prefill
    4 * 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 keys plus seven times one value length prefill
    sizeOfKeys + 7 * sizeOfOneValue,
    // Precisely size of keys plus eight times one value length prefill
    sizeOfKeys + 8 * sizeOfOneValue,
    // Precisely size of keys plus nine times one value length prefill
    sizeOfKeys + 9 * sizeOfOneValue,
    // Precisely size of items prefill
    sizeOfItems,
    // Unlimited prefill
    -1,
  ];

  for (let prefillValue of prefillValues) {
    info("Setting prefill value to " + prefillValue);

    Services.prefs.setIntPref("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 seven times one item length gradual prefill
      7 * sizeOfOneItem,
      // Precisely eight times one item length gradual prefill
      8 * sizeOfOneItem,
      // Precisely nine times one item length gradual prefill
      9 * sizeOfOneItem,
      // Precisely size of items prefill
      sizeOfItems,
      // Unlimited gradual prefill
      -1,
    ];

    for (let gradualPrefillValue of gradualPrefillValues) {
      info("Setting gradual prefill value to " + gradualPrefillValue);

      Services.prefs.setIntPref(
        "dom.storage.snapshot_gradual_prefill",
        gradualPrefillValue
      );

      info("Getting storage");

      let storage = getLocalStorage(getPrincipal(url));

      // 1st snapshot

      info("Adding data");

      for (let item of items) {
        storage.setItem(item.key, item.value);
      }

      info("Saving key order");

      // This forces GetKeys to be called internally.
      let savedKeys = Object.keys(storage);

      // GetKey should match GetKeys
      for (let i = 0; i < savedKeys.length; i++) {
        is(storage.key(i), savedKeys[i], "Correct key");
      }

      info("Returning to event loop");

      // Returning to event loop forces the internal snapshot to finish.
      await returnToEventLoop();

      // 2nd snapshot

      info("Verifying length");

      is(storage.length, items.length, "Correct length");

      info("Verifying key order");

      let keys = Object.keys(storage);

      is(keys.length, savedKeys.length);

      for (let i = 0; i < keys.length; i++) {
        is(keys[i], savedKeys[i], "Correct key");
      }

      info("Verifying values");

      for (let item of items) {
        is(storage.getItem(item.key), item.value, "Correct value");
      }

      info("Returning to event loop");

      await returnToEventLoop();

      // 3rd snapshot

      // Force key2 to load.
      storage.getItem("key02");

      // Fill out write infos a bit.
      storage.removeItem("key05");
      storage.setItem("key05", "value05");
      storage.removeItem("key05");
      storage.setItem("key11", "value11");
      storage.setItem("key05", "value05");

      items.push({ key: "key11", value: "value11" });

      info("Verifying length");

      is(storage.length, items.length, "Correct length");

      // This forces to get all keys from the parent and then apply write infos
      // on already cached values.
      savedKeys = Object.keys(storage);

      info("Verifying values");

      for (let item of items) {
        is(storage.getItem(item.key), item.value, "Correct value");
      }

      storage.removeItem("key11");

      items.pop();

      info("Returning to event loop");

      await returnToEventLoop();

      // 4th snapshot

      // Force loading of all items.
      info("Verifying length");

      is(storage.length, items.length, "Correct length");

      info("Verifying values");

      for (let item of items) {
        is(storage.getItem(item.key), item.value, "Correct value");
      }

      is(storage.getItem("key11"), null, "Correct value");

      info("Returning to event loop");

      await returnToEventLoop();

      // 5th snapshot

      // Force loading of all keys.
      info("Saving key order");

      savedKeys = Object.keys(storage);

      // Force loading of all items.
      info("Verifying length");

      is(storage.length, items.length, "Correct length");

      info("Verifying values");

      for (let item of items) {
        is(storage.getItem(item.key), item.value, "Correct value");
      }

      is(storage.getItem("key11"), null, "Correct value");

      info("Returning to event loop");

      await returnToEventLoop();

      // 6th snapshot
      info("Verifying unknown item");

      is(storage.getItem("key11"), null, "Correct value");

      info("Verifying unknown item again");

      is(storage.getItem("key11"), null, "Correct value");

      info("Returning to event loop");

      await returnToEventLoop();

      // 7th snapshot

      // Save actual key order.
      info("Saving key order");

      savedKeys = Object.keys(storage);

      await returnToEventLoop();

      // 8th snapshot

      // Force loading of all items, but in reverse order.
      info("Getting values");

      for (let i = items.length - 1; i >= 0; i--) {
        let item = items[i];
        storage.getItem(item.key);
      }

      info("Verifying key order");

      keys = Object.keys(storage);

      is(keys.length, savedKeys.length);

      for (let i = 0; i < keys.length; i++) {
        is(keys[i], savedKeys[i], "Correct key");
      }

      await returnToEventLoop();

      // 9th snapshot

      info("Clearing");

      storage.clear();

      info("Returning to event loop");

      await returnToEventLoop();
    }
  }
});