summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_transaction_abort.js
blob: 68293928424c58765ebfbc565c7c380297ed77b9 (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
370
371
372
373
374
375
376
377
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

var testGenerator = testSteps();

var abortFired = false;

function abortListener(evt) {
  abortFired = true;
  is(evt.target.error, null, "Expect a null error for an aborted transaction");
}

function* testSteps() {
  const name = this.window ? window.location.pathname : "Splendid Test";

  let request = indexedDB.open(name, 1);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = grabEventAndContinueHandler;
  let event = yield undefined;

  let db = event.target.result;
  db.onabort = abortListener;

  let transaction;
  let objectStore;
  let index;

  transaction = event.target.transaction;

  is(transaction.error, null, "Expect a null error");

  objectStore = db.createObjectStore("foo", { autoIncrement: true });
  index = objectStore.createIndex("fooindex", "indexKey", { unique: true });

  is(transaction.db, db, "Correct database");
  is(transaction.mode, "versionchange", "Correct mode");
  is(transaction.objectStoreNames.length, 1, "Correct names length");
  is(transaction.objectStoreNames.item(0), "foo", "Correct name");
  is(transaction.objectStore("foo"), objectStore, "Can get stores");
  is(transaction.oncomplete, null, "No complete listener");
  is(transaction.onabort, null, "No abort listener");

  is(objectStore.name, "foo", "Correct name");
  is(objectStore.keyPath, null, "Correct keyPath");

  is(objectStore.indexNames.length, 1, "Correct indexNames length");
  is(objectStore.indexNames[0], "fooindex", "Correct indexNames name");
  is(objectStore.index("fooindex"), index, "Can get index");

  // Wait until it's complete!
  transaction.oncomplete = grabEventAndContinueHandler;
  event = yield undefined;

  is(transaction.db, db, "Correct database");
  is(transaction.mode, "versionchange", "Correct mode");
  is(transaction.objectStoreNames.length, 1, "Correct names length");
  is(transaction.objectStoreNames.item(0), "foo", "Correct name");
  is(transaction.onabort, null, "No abort listener");

  try {
    is(transaction.objectStore("foo").name, "foo", "Can't get stores");
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "Out of scope transaction can't make stores");
  }

  is(objectStore.name, "foo", "Correct name");
  is(objectStore.keyPath, null, "Correct keyPath");

  is(objectStore.indexNames.length, 1, "Correct indexNames length");
  is(objectStore.indexNames[0], "fooindex", "Correct indexNames name");

  try {
    objectStore.add({});
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "Add threw");
  }

  try {
    objectStore.put({}, 1);
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "Put threw");
  }

  try {
    objectStore.put({}, 1);
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "Put threw");
  }

  try {
    objectStore.delete(1);
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "Remove threw");
  }

  try {
    objectStore.get(1);
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "Get threw");
  }

  try {
    objectStore.getAll(null);
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "GetAll threw");
  }

  try {
    objectStore.openCursor();
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "OpenCursor threw");
  }

  try {
    objectStore.createIndex("bar", "id");
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "CreateIndex threw");
  }

  try {
    objectStore.index("bar");
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "Index threw");
  }

  try {
    objectStore.deleteIndex("bar");
    ok(false, "Should have thrown");
  } catch (e) {
    ok(true, "RemoveIndex threw");
  }

  yield undefined;

  request = db.transaction("foo", "readwrite").objectStore("foo").add({});
  request.onerror = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  event.target.transaction.onabort = function (event) {
    ok(false, "Shouldn't see an abort event!");
  };
  event.target.transaction.oncomplete = grabEventAndContinueHandler;
  event = yield undefined;

  is(event.type, "complete", "Right kind of event");

  let key;

  request = db.transaction("foo", "readwrite").objectStore("foo").add({});
  request.onerror = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  key = event.target.result;

  event.target.transaction.onabort = grabEventAndContinueHandler;
  event.target.transaction.oncomplete = function (event) {
    ok(false, "Shouldn't see a complete event here!");
  };

  event.target.transaction.abort();

  event = yield undefined;

  is(event.type, "abort", "Right kind of event");

  request = db.transaction("foo").objectStore("foo").get(key);
  request.onerror = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  is(event.target.result, undefined, "Object was removed");

  executeSoon(function () {
    testGenerator.next();
  });
  yield undefined;

  let keys = [];
  let abortEventCount = 0;
  function abortErrorHandler(event) {
    is(event.target.error.name, "AbortError", "Good error");
    abortEventCount++;
    event.preventDefault();
  }
  objectStore = db.transaction("foo", "readwrite").objectStore("foo");

  for (let i = 0; i < 10; i++) {
    request = objectStore.add({});
    request.onerror = abortErrorHandler;
    request.onsuccess = function (event) {
      keys.push(event.target.result);
      if (keys.length == 5) {
        event.target.transaction.onabort = grabEventAndContinueHandler;
        event.target.transaction.abort();
      }
    };
  }
  event = yield undefined;

  is(event.type, "abort", "Got abort event");
  is(keys.length, 5, "Added 5 items in this transaction");
  is(abortEventCount, 5, "Got 5 abort error events");

  for (let i in keys) {
    request = db.transaction("foo").objectStore("foo").get(keys[i]);
    request.onerror = errorHandler;
    request.onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    is(event.target.result, undefined, "Object was removed by abort");
  }

  // Set up some predictible data
  transaction = db.transaction("foo", "readwrite");
  objectStore = transaction.objectStore("foo");
  objectStore.clear();
  objectStore.add({}, 1);
  objectStore.add({}, 2);
  request = objectStore.add({}, 1);
  request.onsuccess = function () {
    ok(false, "inserting duplicate key should fail");
  };
  request.onerror = function (event) {
    ok(true, "inserting duplicate key should fail");
    event.preventDefault();
  };
  transaction.oncomplete = grabEventAndContinueHandler;
  yield undefined;

  // Check when aborting is allowed
  abortEventCount = 0;
  let expectedAbortEventCount = 0;

  // During INITIAL
  transaction = db.transaction("foo");
  transaction.abort();
  try {
    transaction.abort();
    ok(false, "second abort should throw an error");
  } catch (ex) {
    ok(true, "second abort should throw an error");
  }

  // During LOADING
  transaction = db.transaction("foo");
  transaction.objectStore("foo").get(1).onerror = abortErrorHandler;
  expectedAbortEventCount++;
  transaction.abort();
  try {
    transaction.abort();
    ok(false, "second abort should throw an error");
  } catch (ex) {
    ok(true, "second abort should throw an error");
  }

  // During LOADING from callback
  transaction = db.transaction("foo");
  transaction.objectStore("foo").get(1).onsuccess = grabEventAndContinueHandler;
  event = yield undefined;
  transaction.objectStore("foo").get(1).onerror = abortErrorHandler;
  expectedAbortEventCount++;
  transaction.abort();
  try {
    transaction.abort();
    ok(false, "second abort should throw an error");
  } catch (ex) {
    ok(true, "second abort should throw an error");
  }

  // During LOADING from error callback
  transaction = db.transaction("foo", "readwrite");
  transaction.objectStore("foo").add({}, 1).onerror = function (event) {
    event.preventDefault();

    transaction.objectStore("foo").get(1).onerror = abortErrorHandler;
    expectedAbortEventCount++;

    transaction.abort();
    continueToNextStep();
  };
  yield undefined;

  // In between callbacks
  transaction = db.transaction("foo");
  function makeNewRequest() {
    let r = transaction.objectStore("foo").get(1);
    r.onsuccess = makeNewRequest;
    r.onerror = abortErrorHandler;
  }
  makeNewRequest();
  transaction.objectStore("foo").get(1).onsuccess = function (event) {
    executeSoon(function () {
      transaction.abort();
      expectedAbortEventCount++;
      continueToNextStep();
    });
  };
  yield undefined;

  // During COMMITTING
  transaction = db.transaction("foo", "readwrite");
  transaction.objectStore("foo").put({ hello: "world" }, 1).onsuccess =
    function (event) {
      continueToNextStep();
    };
  yield undefined;
  try {
    transaction.abort();
    ok(false, "second abort should throw an error");
  } catch (ex) {
    ok(true, "second abort should throw an error");
  }
  transaction.oncomplete = grabEventAndContinueHandler;
  event = yield undefined;

  // Since the previous transaction shouldn't have caused any error events,
  // we know that all events should have fired by now.
  is(abortEventCount, expectedAbortEventCount, "All abort errors fired");

  // Abort both failing and succeeding requests
  transaction = db.transaction("foo", "readwrite");
  transaction.onabort = transaction.oncomplete = grabEventAndContinueHandler;
  transaction.objectStore("foo").add({ indexKey: "key" }).onsuccess = function (
    event
  ) {
    transaction.abort();
  };
  let request1 = transaction.objectStore("foo").add({ indexKey: "key" });
  request1.onsuccess = grabEventAndContinueHandler;
  request1.onerror = grabEventAndContinueHandler;
  let request2 = transaction.objectStore("foo").get(1);
  request2.onsuccess = grabEventAndContinueHandler;
  request2.onerror = grabEventAndContinueHandler;

  event = yield undefined;
  is(event.type, "error", "abort() should make all requests fail");
  is(event.target, request1, "abort() should make all requests fail");
  is(
    event.target.error.name,
    "AbortError",
    "abort() should make all requests fail"
  );
  event.preventDefault();

  event = yield undefined;
  is(event.type, "error", "abort() should make all requests fail");
  is(event.target, request2, "abort() should make all requests fail");
  is(
    event.target.error.name,
    "AbortError",
    "abort() should make all requests fail"
  );
  event.preventDefault();

  event = yield undefined;
  is(event.type, "abort", "transaction should fail");
  is(event.target, transaction, "transaction should fail");

  ok(abortFired, "Abort should have fired!");

  finishTest();
}