test_table_locks.js (3046B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 const dbName = "window" in this ? window.location.pathname : "test"; 7 const dbVersion = 1; 8 const objName1 = "o1"; 9 const objName2 = "o2"; 10 const idxName1 = "i1"; 11 const idxName2 = "i2"; 12 const idxKeyPathProp = "idx"; 13 const objDataProp = "data"; 14 const objData = "1234567890"; 15 const objDataCount = 5; 16 const loopCount = 100; 17 18 /* exported testGenerator */ 19 var testGenerator = testSteps(); 20 21 function* testSteps() { 22 let req = indexedDB.open(dbName, dbVersion); 23 req.onerror = errorHandler; 24 req.onupgradeneeded = grabEventAndContinueHandler; 25 req.onsuccess = grabEventAndContinueHandler; 26 27 let event = yield undefined; 28 29 is(event.type, "upgradeneeded", "Got upgradeneeded event"); 30 31 let db = event.target.result; 32 33 let objectStore1 = db.createObjectStore(objName1); 34 objectStore1.createIndex(idxName1, idxKeyPathProp); 35 36 let objectStore2 = db.createObjectStore(objName2); 37 objectStore2.createIndex(idxName2, idxKeyPathProp); 38 39 for (let i = 0; i < objDataCount; i++) { 40 var data = {}; 41 data[objDataProp] = objData; 42 data[idxKeyPathProp] = objDataCount - i - 1; 43 44 objectStore1.add(data, i); 45 objectStore2.add(data, i); 46 } 47 48 event = yield undefined; 49 50 is(event.type, "success", "Got success event"); 51 52 doReadOnlyTransaction(db, 0, loopCount); 53 doReadWriteTransaction(db, 0, loopCount); 54 55 // Wait for readonly and readwrite transaction loops to complete. 56 yield undefined; 57 yield undefined; 58 59 finishTest(); 60 } 61 62 function doReadOnlyTransaction(db, key, remaining) { 63 if (!remaining) { 64 info("Finished all readonly transactions"); 65 continueToNextStep(); 66 return; 67 } 68 69 info( 70 "Starting readonly transaction for key " + 71 key + 72 ", " + 73 remaining + 74 " loops left" 75 ); 76 77 let objectStore = db.transaction(objName1, "readonly").objectStore(objName1); 78 let index = objectStore.index(idxName1); 79 80 index.openKeyCursor(key, "prev").onsuccess = function (event) { 81 let cursor = event.target.result; 82 ok(cursor, "Got readonly cursor"); 83 84 objectStore.get(cursor.primaryKey).onsuccess = function () { 85 if (++key == objDataCount) { 86 key = 0; 87 } 88 doReadOnlyTransaction(db, key, remaining - 1); 89 }; 90 }; 91 } 92 93 function doReadWriteTransaction(db, key, remaining) { 94 if (!remaining) { 95 info("Finished all readwrite transactions"); 96 continueToNextStep(); 97 return; 98 } 99 100 info( 101 "Starting readwrite transaction for key " + 102 key + 103 ", " + 104 remaining + 105 " loops left" 106 ); 107 108 let objectStore = db.transaction(objName2, "readwrite").objectStore(objName2); 109 objectStore.openCursor(key).onsuccess = function (event) { 110 let cursor = event.target.result; 111 ok(cursor, "Got readwrite cursor"); 112 113 let value = cursor.value; 114 value[idxKeyPathProp]++; 115 116 cursor.update(value).onsuccess = function () { 117 if (++key == objDataCount) { 118 key = 0; 119 } 120 doReadWriteTransaction(db, key, remaining - 1); 121 }; 122 }; 123 }