test_table_locks.js 2.9 KB

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