test_quotaExceeded_recovery.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var disableWorkerTest = "Need a way to set temporary prefs from a worker";
  6. var testGenerator = testSteps();
  7. function testSteps()
  8. {
  9. const spec = "http://foo.com";
  10. const name =
  11. this.window ? window.location.pathname : "test_quotaExceeded_recovery";
  12. const objectStoreName = "foo";
  13. const android = mozinfo.os == "android";
  14. // We want 512 KB database on Android and 4 MB database on other platforms.
  15. const groupLimitKB = android ? 512 : 4096;
  16. // The group limit is calculated as 20% of the global temporary storage limit.
  17. const tempStorageLimitKB = groupLimitKB * 5;
  18. // We want 64 KB chunks on Android and 512 KB chunks on other platforms.
  19. const dataSizeKB = android ? 64 : 512;
  20. const dataSize = dataSizeKB * 1024;
  21. const maxIter = 5;
  22. for (let blobs of [false, true]) {
  23. setTemporaryStorageLimit(tempStorageLimitKB);
  24. clearAllDatabases(continueToNextStepSync);
  25. yield undefined;
  26. info("Opening database");
  27. let request = indexedDB.openForPrincipal(getPrincipal(spec), name);
  28. request.onerror = errorHandler;
  29. request.onupgradeneeded = grabEventAndContinueHandler;;
  30. request.onsuccess = unexpectedSuccessHandler;
  31. yield undefined;
  32. // upgradeneeded
  33. request.onupgradeneeded = unexpectedSuccessHandler;
  34. request.onsuccess = grabEventAndContinueHandler;
  35. info("Creating objectStore");
  36. request.result.createObjectStore(objectStoreName, { autoIncrement: true });
  37. yield undefined;
  38. // success
  39. let db = request.result;
  40. db.onerror = errorHandler;
  41. ok(true, "Filling database");
  42. let obj = {
  43. name: "foo"
  44. }
  45. if (!blobs) {
  46. obj.data = getRandomView(dataSize);
  47. }
  48. let iter = 1;
  49. let i = 1;
  50. let j = 1;
  51. while (true) {
  52. if (blobs) {
  53. obj.data = getBlob(getView(dataSize));
  54. }
  55. let trans = db.transaction(objectStoreName, "readwrite");
  56. request = trans.objectStore(objectStoreName).add(obj);
  57. request.onerror = function(event)
  58. {
  59. event.stopPropagation();
  60. }
  61. trans.oncomplete = function(event) {
  62. if (iter == 1) {
  63. i++;
  64. }
  65. j++;
  66. testGenerator.send(true);
  67. }
  68. trans.onabort = function(event) {
  69. is(trans.error.name, "QuotaExceededError", "Reached quota limit");
  70. testGenerator.send(false);
  71. }
  72. let completeFired = yield undefined;
  73. if (completeFired) {
  74. ok(true, "Got complete event");
  75. continue;
  76. }
  77. ok(true, "Got abort event");
  78. if (iter++ == maxIter) {
  79. break;
  80. }
  81. if (iter > 1) {
  82. ok(i == j, "Recycled entire database");
  83. j = 1;
  84. }
  85. trans = db.transaction(objectStoreName, "readwrite");
  86. // Don't use a cursor for deleting stored blobs (Cursors prolong live
  87. // of stored files since each record must be fetched from the database
  88. // first which creates a memory reference to the stored blob.)
  89. if (blobs) {
  90. request = trans.objectStore(objectStoreName).clear();
  91. } else {
  92. request = trans.objectStore(objectStoreName).openCursor();
  93. request.onsuccess = function(event) {
  94. let cursor = event.target.result;
  95. if (cursor) {
  96. cursor.delete();
  97. cursor.continue();
  98. }
  99. }
  100. }
  101. trans.onabort = unexpectedSuccessHandler;;
  102. trans.oncomplete = grabEventAndContinueHandler;
  103. yield undefined;
  104. }
  105. }
  106. finishTest();
  107. yield undefined;
  108. }