OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>IndexedDB: Verify transaction activation behavior around microtasks</titl
e> |
| 3 <script src="../../resources/testharness.js"></script> |
| 4 <script src="../../resources/testharnessreport.js"></script> |
| 5 <script> |
| 6 |
| 7 function idb_test(description, onUpgrade, onOpen) { |
| 8 var t = async_test(description); |
| 9 t.step(function() { |
| 10 var dbName = 'db' + location.pathname + '-' + description; |
| 11 var deleteRequest = indexedDB.deleteDatabase(dbName); |
| 12 deleteRequest.onerror = t.unreached_func('deleteDatabase should not fail
'); |
| 13 deleteRequest.onsuccess = t.step_func(function(e) { |
| 14 var openRequest = indexedDB.open(dbName); |
| 15 openRequest.onerror = t.unreached_func('open should not fail'); |
| 16 openRequest.onupgradeneeded = t.step_func(function(e) { |
| 17 onUpgrade(t, openRequest.result); |
| 18 }); |
| 19 openRequest.onsuccess = t.step_func(function(e) { |
| 20 onOpen(t, openRequest.result); |
| 21 }); |
| 22 }); |
| 23 }); |
| 24 } |
| 25 |
| 26 function isTransactionActive(tx, storeName) { |
| 27 try { |
| 28 tx.objectStore(storeName).get(0); |
| 29 return true; |
| 30 } catch (ex) { |
| 31 assert_equals(ex.name, 'TransactionInactiveError', |
| 32 'Failure should be due to inactive transaction'); |
| 33 return false; |
| 34 } |
| 35 } |
| 36 |
| 37 idb_test( |
| 38 'Transactions created in microtasks are deactivated when control returns to
the event loop', |
| 39 function(t, db) { |
| 40 db.createObjectStore('store'); |
| 41 }, |
| 42 function(t, db) { |
| 43 Promise.resolve().then(t.step_func(function() { |
| 44 var tx = db.transaction('store'); |
| 45 var request = tx.objectStore('store').get(0); |
| 46 request.onerror = t.unreached_func('request should not fail'); |
| 47 request.onsuccess = t.step_func(function() { |
| 48 assert_true(isTransactionActive(tx, 'store'), |
| 49 'Transaction should be active during event dispatch'
); |
| 50 setTimeout(function() { |
| 51 assert_false(isTransactionActive(tx, 'store'), |
| 52 'Transaction should be inactive once control re
turns to event loop'); |
| 53 t.done(); |
| 54 }, 0); |
| 55 }); |
| 56 })); |
| 57 } |
| 58 ); |
| 59 |
| 60 idb_test( |
| 61 'Transactions created in microtasks remain active in subsequent microtasks', |
| 62 function(t, db) { |
| 63 db.createObjectStore('store'); |
| 64 }, |
| 65 function(t, db) { |
| 66 var tx; |
| 67 Promise.resolve().then(function() { |
| 68 tx = db.transaction('store'); |
| 69 assert_true(isTransactionActive(tx, 'store'), |
| 70 'Transaction should be active when created'); |
| 71 }).then(t.step_func(function() { |
| 72 assert_true(isTransactionActive(tx, 'store'), |
| 73 'Transaction should be active until control returns to e
vent loop'); |
| 74 setTimeout(function() { |
| 75 assert_false(isTransactionActive(tx, 'store'), |
| 76 'Transaction should be inactive once control returns
to event loop'); |
| 77 t.done(); |
| 78 }, 0); |
| 79 })); |
| 80 } |
| 81 ); |
| 82 |
| 83 idb_test( |
| 84 'Within request event dispatch, transactions remain active across microtasks
', |
| 85 function(t, db) { |
| 86 db.createObjectStore('store'); |
| 87 }, |
| 88 function(t, db) { |
| 89 var tx = db.transaction('store'); |
| 90 var request = tx.objectStore('store').get(0); |
| 91 request.onerror = t.unreached_func('request should not fail'); |
| 92 request.onsuccess = t.step_func(function() { |
| 93 assert_true(isTransactionActive(tx, 'store'), |
| 94 'Transaction should be active during event dispatch'); |
| 95 Promise.resolve().then(t.step_func(function() { |
| 96 assert_true(isTransactionActive(tx, 'store'), |
| 97 'Microtasks are run as part of event dispatch, so tr
ansaction should still be active'); |
| 98 setTimeout(function() { |
| 99 assert_false(isTransactionActive(tx, 'store'), |
| 100 'Transaction should be inactive once control ret
urns to the event loop'); |
| 101 t.done(); |
| 102 }, 0); |
| 103 })); |
| 104 }); |
| 105 } |
| 106 ); |
| 107 |
| 108 </script> |
OLD | NEW |