summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/request-event-ordering.html
blob: 71eda0dd1dfdc5a54fab4ed764e063936ac0905d (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
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
<!doctype html>
<meta charset="utf8">
<meta name="timeout" content="long">
<title>IndexedDB: request result events are delivered in order</title>
<link rel="help" href="https://w3c.github.io/IndexedDB/#abort-transaction">
<link rel="author" href="pwnall@chromium.org" title="Victor Costan">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/support-promises.js"></script>
<script>
'use strict';

// Should be large enough to trigger large value handling in the IndexedDB
// engines that have special code paths for large values.
const wrapThreshold = 128 * 1024;

function populateStore(store) {
  store.put({id: 1, key: 'k1', value: largeValue(wrapThreshold, 1) });
  store.put({id: 2, key: 'k2', value: ['small-2'] });
  store.put({id: 3, key: 'k3', value: largeValue(wrapThreshold, 3) });
  store.put({id: 4, key: 'k4', value: ['small-4'] });
}

// Assigns cursor indexes for operations that require open cursors.
//
// Returns the number of open cursors required to perform all operations.
function assignCursors(operations) {
  return cursorCount;
}

// Opens index cursors for operations that require open cursors.
//
// onsuccess is called if all cursors are opened successfully. Otherwise,
// onerror will be called at least once.
function openCursors(testCase, index, operations, onerror, onsuccess) {
  let pendingCursors = 0;

  for (let operation of operations) {
    const opcode = operation[0];
    const primaryKey = operation[1];
    let request;
    switch (opcode) {
      case 'continue':
        request = index.openCursor(
            IDBKeyRange.lowerBound(`k${primaryKey - 1}`));
        break;
      case 'continue-empty':
        // k4 is the last key in the data set, so calling continue() will get
        // the cursor past the end of the store.
        request = index.openCursor(IDBKeyRange.lowerBound('k4'));
        break;
      default:
        continue;
    }

    operation[2] = request;
    ++pendingCursors;

    request.onsuccess = testCase.step_func(() => {
      --pendingCursors;
      if (!pendingCursors)
        onsuccess();
    });
    request.onerror = testCase.step_func(onerror);
  }

  if (!pendingCursors)
    onsuccess();
}

function doOperation(testCase, store, index, operation, requestId, results) {
  const opcode = operation[0];
  const primaryKey = operation[1];
  const cursor = operation[2];

  return new Promise((resolve, reject) => {
    let request;
    switch (opcode) {
      case 'add':  // Tests returning a primary key.
        request = store.add(
            { key: `k${primaryKey}`, value: [`small-${primaryKey}`] });
        break;
      case 'put':  // Tests returning a primary key.
        request = store.put(
            { key: `k${primaryKey}`, value: [`small-${primaryKey}`] });
        break;
      case 'put-with-id':  // Tests returning success or a primary key.
        request = store.put(
            { key: `k${primaryKey}`, value: [`small-${primaryKey}`],
              id: primaryKey });
        break;
      case 'get':  // Tests returning a value.
      case 'get-empty':  // Tests returning undefined.
        request = store.get(primaryKey);
        break;
      case 'getall':  // Tests returning an array of values.
        request = store.getAll();
        break;
      case 'error':  // Tests returning an error.
        request = store.put(
            { key: `k${primaryKey}`, value: [`small-${primaryKey}`] });
        request.onerror = testCase.step_func(event => {
          event.preventDefault();
          results.push([requestId, request.error]);
          resolve();
        });
        request.onsuccess = testCase.step_func(() => {
          reject(new Error('put with duplicate primary key succeded'));
        });
        break;
      case 'continue':  // Tests returning a key, primary key, and value.
        request = cursor;
        cursor.result.continue();
        request.onsuccess = testCase.step_func(() => {
          const result = request.result;
          results.push(
              [requestId, result.key, result.primaryKey, result.value]);
          resolve();
        });
        request.onerror = null;
        break;
      case 'open':  // Tests returning a cursor, key, primary key, and value.
        request = index.openCursor(IDBKeyRange.lowerBound(`k${primaryKey}`));
        request.onsuccess = testCase.step_func(() => {
          const result = request.result;
          results.push(
              [requestId, result.key, result.primaryKey, result.value]);
          resolve();
        });
        break;
      case 'continue-empty':  // Tests returning a null result.
        request = cursor;
        cursor.result.continue();
        request.onsuccess = testCase.step_func(() => {
          results.push([requestId, request.result]);
          resolve();
        });
        request.onerror = null;
        break;
      case 'open-empty':  // Tests returning a null cursor.
        request = index.openCursor(IDBKeyRange.lowerBound(`k${primaryKey}`));
        request.onsuccess = testCase.step_func(() => {
          const result = request.result;
          results.push([requestId, request.result]);
          resolve();
        });
        break;
      case 'count':  // Tests returning a numeric result.
        request = index.count();
        request.onsuccess = testCase.step_func(() => {
          results.push([requestId, request.result]);
          resolve();
        });
        break;
    };

    if (!request.onsuccess) {
      request.onsuccess = testCase.step_func(() => {
        results.push([requestId, request.result]);
        resolve();
      });
    }
    if (!request.onerror)
      request.onerror = testCase.step_func(event => {
        event.preventDefault();
        reject(request.error);
      });
  });
}

function checkOperationResult(operation, result, requestId) {
  const opcode = operation[0];
  const primaryKey = operation[1];

  const expectedValue = (primaryKey == 1 || primaryKey == 3) ?
      largeValue(wrapThreshold, primaryKey) : [`small-${primaryKey}`];

  const requestIndex = result[0];
  assert_equals(
      requestIndex, requestId, 'result event order should match request order');
  switch (opcode) {
    case 'put':
    case 'put-with-id':
    case 'add':
      assert_equals(
          result[1], primaryKey,
          `${opcode} result should be the new object's primary key`);
      break;
    case 'get':
      assert_equals(
          result[1].id, primaryKey,
          'get result should match put value (primary key)');
      assert_equals(
          result[1].key, `k${primaryKey}`,
          'get result should match put value (key)');
      assert_equals(
          result[1].value.join(','), expectedValue.join(','),
          'get result should match put value (nested value)');
      break;
    case 'getall':
      assert_equals(
          result[1].length, primaryKey,
          'getAll should return all the objects in the store');
      for (let i = 0; i < primaryKey; ++i) {
        const object = result[1][i];
        assert_equals(
            object.id, i + 1,
            `getAll result ${i + 1} should match put value (primary key)`);
        assert_equals(
            object.key, `k${i + 1}`,
            `get result ${i + 1} should match put value (key)`);

        const expectedValue = (i == 0 || i == 2) ?
            largeValue(wrapThreshold, i + 1) : [`small-${i + 1}`];
        assert_equals(
            object.value.join(','), object.value.join(','),
            `get result ${i + 1} should match put value (nested value)`);
      }
      break;
    case 'get-empty':
      assert_equals(
          result[1], undefined, 'get-empty result should be undefined');
      break;
    case 'error':
      assert_equals(
          result[1].name, 'ConstraintError',
          'incorrect error from put with duplicate primary key');
      break;
    case 'continue':
    case 'open':
      assert_equals(
          result[1], `k${primaryKey}`,
          `${opcode} key should match the key in the put value`);
      assert_equals(
          result[2], primaryKey,
          `${opcode} primary key should match the put value's primary key`);
      assert_equals(
          result[3].id, primaryKey,
          `${opcode} value should match put value (primary key)`);
      assert_equals(
          result[3].key, `k${primaryKey}`,
          `${opcode} value should match put value (key)`);
      assert_equals(
          result[3].value.join(','), expectedValue.join(','),
          `${opcode} value should match put value (nested value)`);
      break;
    case 'continue-empty':
    case 'open-empty':
      assert_equals(result[1], null, `${opcode} result should be null`);
      break;
  }
}

function eventsTest(label, operations) {
  promise_test(testCase => {
    return createDatabase(testCase, (database, transaction) => {
      const store = database.createObjectStore(
          'test-store', { autoIncrement: true, keyPath: 'id' });
      store.createIndex('test-index', 'key', { unique: true });
      populateStore(store);
    }).then(database => {
      const transaction = database.transaction(['test-store'], 'readwrite');
      const store = transaction.objectStore('test-store');
      const index = store.index('test-index');
      return new Promise((resolve, reject) => {
        openCursors(testCase, index, operations, reject, () => {
          const results = [];
          const promises = [];
          for (let i = 0; i < operations.length; ++i) {
            const promise = doOperation(
                testCase, store, index, operations[i], i, results);
            promises.push(promise);
          };
          resolve(Promise.all(promises).then(() => results));
        });
      });
    }).then(results => {
      assert_equals(
          results.length, operations.length,
          'Promise.all should resolve after all sub-promises resolve');
      for (let i = 0; i < operations.length; ++i)
        checkOperationResult(operations[i], results[i], i);
    });
  }, label);
}

eventsTest('small values', [
  ['get', 2],
  ['count', 4],
  ['continue-empty', null],
  ['get-empty', 5],
  ['add', 5],
  ['open', 2],
  ['continue', 2],
  ['get', 4],
  ['get-empty', 6],
  ['count', 5],
  ['put-with-id', 5],
  ['put', 6],
  ['error', 3],
  ['continue', 4],
  ['count', 6],
  ['get-empty', 7],
  ['open', 4],
  ['open-empty', 7],
  ['add', 7],
]);

eventsTest('large values', [
  ['open', 1],
  ['get', 1],
  ['getall', 4],
  ['get', 3],
  ['continue', 3],
  ['open', 3],
]);

eventsTest('large value followed by small values', [
  ['get', 1],
  ['getall', 4],
  ['open', 2],
  ['continue-empty', null],
  ['get', 2],
  ['get-empty', 5],
  ['count', 4],
  ['continue-empty', null],
  ['open-empty', 5],
  ['add', 5],
  ['error', 1],
  ['continue', 2],
  ['get-empty', 6],
  ['put-with-id', 5],
  ['put', 6],
]);

eventsTest('large values mixed with small values', [
  ['get', 1],
  ['get', 2],
  ['get-empty', 5],
  ['count', 4],
  ['continue-empty', null],
  ['open', 1],
  ['continue', 2],
  ['open-empty', 5],
  ['getall', 4],
  ['open', 2],
  ['continue-empty', null],
  ['add', 5],
  ['get', 3],
  ['count', 5],
  ['get-empty', 6],
  ['put-with-id', 5],
  ['getall', 5],
  ['continue', 3],
  ['open-empty', 6],
  ['put', 6],
  ['error', 1],
  ['continue', 2],
  ['open', 4],
  ['get-empty', 7],
  ['count', 6],
  ['continue', 3],
  ['add', 7],
  ['getall', 7],
  ['error', 3],
  ['count', 7],
]);

</script>