Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /** |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | var self = this; |
michael@0 | 8 | |
michael@0 | 9 | var testGenerator = testSteps(); |
michael@0 | 10 | |
michael@0 | 11 | function testSteps() |
michael@0 | 12 | { |
michael@0 | 13 | const dbName = self.window ? |
michael@0 | 14 | window.location.pathname : |
michael@0 | 15 | "test_transaction_abort_hang"; |
michael@0 | 16 | const objStoreName = "foo"; |
michael@0 | 17 | const transactionCount = 30; |
michael@0 | 18 | |
michael@0 | 19 | let completedTransactionCount = 0; |
michael@0 | 20 | let caughtError = false; |
michael@0 | 21 | |
michael@0 | 22 | let abortedTransactionIndex = Math.floor(transactionCount / 2); |
michael@0 | 23 | if (abortedTransactionIndex % 2 == 0) { |
michael@0 | 24 | abortedTransactionIndex++; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | let request = indexedDB.open(dbName, 1); |
michael@0 | 28 | request.onerror = errorHandler; |
michael@0 | 29 | request.onupgradeneeded = grabEventAndContinueHandler; |
michael@0 | 30 | let event = yield undefined; |
michael@0 | 31 | |
michael@0 | 32 | request.result.createObjectStore(objStoreName, { autoIncrement: true }); |
michael@0 | 33 | |
michael@0 | 34 | request.onupgradeneeded = null; |
michael@0 | 35 | request.onsuccess = grabEventAndContinueHandler; |
michael@0 | 36 | event = yield undefined; |
michael@0 | 37 | |
michael@0 | 38 | let db = event.target.result; |
michael@0 | 39 | |
michael@0 | 40 | for (let i = 0; i < transactionCount; i++) { |
michael@0 | 41 | const readonly = i % 2 == 0; |
michael@0 | 42 | const mode = readonly ? "readonly" : "readwrite"; |
michael@0 | 43 | |
michael@0 | 44 | let transaction = db.transaction(objStoreName, mode); |
michael@0 | 45 | |
michael@0 | 46 | if (i == transactionCount - 1) { |
michael@0 | 47 | // Last one, finish the test. |
michael@0 | 48 | transaction.oncomplete = grabEventAndContinueHandler; |
michael@0 | 49 | } else if (i == abortedTransactionIndex - 1) { |
michael@0 | 50 | transaction.oncomplete = function(event) { |
michael@0 | 51 | ok(true, "Completed transaction " + ++completedTransactionCount + |
michael@0 | 52 | " (We may hang after this!)"); |
michael@0 | 53 | }; |
michael@0 | 54 | } else if (i == abortedTransactionIndex) { |
michael@0 | 55 | // Special transaction that we abort outside the normal event flow. |
michael@0 | 56 | transaction.onerror = function(event) { |
michael@0 | 57 | ok(true, "Aborted transaction " + ++completedTransactionCount + |
michael@0 | 58 | " (We didn't hang!)"); |
michael@0 | 59 | is(event.target.error.name, "AbortError", |
michael@0 | 60 | "AbortError set as the error on the request"); |
michael@0 | 61 | is(event.target.transaction.error, null, |
michael@0 | 62 | "No error set on the transaction"); |
michael@0 | 63 | ok(!caughtError, "Haven't seen the error event yet"); |
michael@0 | 64 | caughtError = true; |
michael@0 | 65 | event.preventDefault(); |
michael@0 | 66 | }; |
michael@0 | 67 | // This has to happen after the we return to the event loop but before the |
michael@0 | 68 | // transaction starts running. |
michael@0 | 69 | executeSoon(function() { transaction.abort(); }); |
michael@0 | 70 | } else { |
michael@0 | 71 | transaction.oncomplete = function(event) { |
michael@0 | 72 | ok(true, "Completed transaction " + ++completedTransactionCount); |
michael@0 | 73 | }; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | if (readonly) { |
michael@0 | 77 | transaction.objectStore(objStoreName).get(0); |
michael@0 | 78 | } else { |
michael@0 | 79 | try { transaction.objectStore(objStoreName).add({}); } catch(e) { } |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | ok(true, "Created all transactions"); |
michael@0 | 83 | |
michael@0 | 84 | event = yield undefined; |
michael@0 | 85 | |
michael@0 | 86 | ok(true, "Completed transaction " + ++completedTransactionCount); |
michael@0 | 87 | ok(caughtError, "Caught the error event when we aborted the transaction"); |
michael@0 | 88 | |
michael@0 | 89 | finishTest(); |
michael@0 | 90 | yield undefined; |
michael@0 | 91 | } |