summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_clear_object_store_with_indexes.js
blob: a6e15cc15c72225d028fb484420767b261f0f656 (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

const isInChaosMode = () => {
  return !!parseInt(Services.env.get("MOZ_CHAOSMODE"), 16);
};

// Reduce the amount of data on slow platforms.
const getDataBlockSize = () => {
  if (mozinfo.os == "android") {
    // Android is much slower than desktop.
    if (mozinfo.verify) {
      return 54; // Chaos mode on android
    }

    return 3333;
  }

  if (isInChaosMode()) {
    return 333;
  }

  return 33333;
};

/* exported testSteps */
async function testSteps() {
  const name = this.window ? window.location.pathname : "Splendid Test";
  const request = indexedDB.open(name, 1);

  (ev => {
    const os = ev.target.result.createObjectStore("testObjectStore", {
      keyPath: "id",
      autoIncrement: false,
    });

    os.createIndex("testObjectStoreIndexA", "indexA", { unique: true });
    os.createIndex("testObjectStoreIndexB", "indexB", { unique: false });
    os.createIndex("testObjectStoreIndexC", "indexC", { unique: false });
  })(await expectingUpgrade(request));

  const db = (await expectingSuccess(request)).target.result;

  const objectStore = db
    .transaction(["testObjectStore"], "readwrite")
    .objectStore("testObjectStore");

  const dataBlock = getDataBlockSize();
  const lastIndex = 3 * dataBlock;

  info("We will now add " + lastIndex + " blobs to our object store");

  const addSegment = async (from, dataValue) => {
    for (let i = from; i <= from + dataBlock - 1; ++i) {
      await objectStore.add({
        id: i,
        indexA: i,
        indexB: lastIndex + 1 - i,
        indexC: i % 3,
        value: dataValue,
      });
    }
  };

  const expectedBegin = getRandomView(512);
  const expectedMiddle = getRandomView(512);
  const expectedEnd = getRandomView(512);
  ok(
    !compareBuffers(expectedBegin, expectedMiddle),
    "Are all buffers different?"
  );
  ok(!compareBuffers(expectedBegin, expectedEnd), "Are all buffers different?");
  ok(
    !compareBuffers(expectedMiddle, expectedEnd),
    "Are all buffers different?"
  );

  const dataValueBegin = getBlob(expectedBegin);
  await addSegment(1, dataValueBegin);

  const dataValueMiddle = getBlob(expectedMiddle);
  await addSegment(dataBlock + 1, dataValueMiddle);

  const dataValueEnd = getBlob(expectedEnd);
  await addSegment(2 * dataBlock + 1, dataValueEnd);

  // Performance issue of 1860486 occurs here
  await new Promise((res, rej) => {
    let isDone = false;
    const deleteReq = objectStore.delete(
      IDBKeyRange.bound(6, lastIndex - 5, false, false)
    );
    deleteReq.onsuccess = () => {
      isDone = true;
      res();
    };
    deleteReq.onerror = err => {
      isDone = true;
      rej(err);
    };

    /**
     * The deletion should be over in 20 seconds or less on desktop. With the
     * regression, the operation can take more than 30 minutes. We use one
     * minute to reduce intermittent failures due to the CI environment.
     *
     * Note that this is not a magical timeout for the completion of an
     * asynchronous request: we are testing a hang and using an explicit timeout
     * will avoid the much longer default timeout which is way too long to be
     * acceptable in real use cases.
     *
     * Maintenance plan: If disk contention and slow hardware lead to too many
     * intermittent failures, the regression cutoff could be increased to 2-3
     * minutes or the test could be turned into a raptor performance test.
     */
    const minutes = 60 * 1000;
    const performance_regression_cutoff = 1 * minutes;
    do_timeout(performance_regression_cutoff, () => {
      if (!isDone) {
        rej(Error("Performance regression detected"));
      }
    });
  });

  const getIndexedItems = async indexName => {
    let actuals = [];
    return new Promise(res => {
      db
        .transaction(["testObjectStore"], "readonly")
        .objectStore("testObjectStore")
        .index(indexName)
        .openCursor().onsuccess = ev => {
        const cursor = ev.target.result;
        if (!cursor) {
          res(actuals);
        } else {
          actuals.push(cursor.value.value);
          cursor.continue();
        }
      };
    });
  };

  const checkValuesEqualTo = async (actuals, from, to, expected) => {
    for (let i = from; i < to; ++i) {
      const actual = new Uint8Array(await actuals[i].arrayBuffer());
      if (!compareBuffers(actual, expected)) {
        return i;
      }
    }
    return undefined;
  };

  const itemsA = await getIndexedItems("testObjectStoreIndexA");
  equal(itemsA.length, 10);

  const mismatchABegin = await checkValuesEqualTo(itemsA, 0, 5, expectedBegin);
  equal(
    mismatchABegin,
    undefined,
    "First index with value mismatch is " + mismatchABegin
  );
  const mismatchAEnd = await checkValuesEqualTo(itemsA, 5, 10, expectedEnd);
  equal(
    mismatchAEnd,
    undefined,
    "First index with value mismatch is " + mismatchAEnd
  );

  const itemsB = await getIndexedItems("testObjectStoreIndexB");

  equal(itemsB.length, 10);
  const mismatchBEnd = await checkValuesEqualTo(itemsB, 0, 5, expectedEnd);
  equal(
    mismatchBEnd,
    undefined,
    "First index with value mismatch is " + mismatchBEnd
  );
  const mismatchBBegin = await checkValuesEqualTo(itemsB, 5, 10, expectedBegin);
  equal(
    mismatchBBegin,
    undefined,
    "First index with value mismatch is " + mismatchBBegin
  );

  const actualsC = await getIndexedItems("testObjectStoreIndexC");

  equal(actualsC.length, 10);
  let countBegin = 0;
  let countEnd = 0;
  for (let i = 0; i < 10; ++i) {
    const actual = new Uint8Array(await actualsC[i].arrayBuffer());
    if (compareBuffers(actual, expectedBegin)) {
      ++countBegin;
    } else if (compareBuffers(actual, expectedEnd)) {
      ++countEnd;
    }
  }

  equal(countBegin, 5);
  equal(countEnd, 5);

  await db.close();
}