test_cache_shrink.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!-- Any copyright is dedicated to the Public Domain.
  2. - http://creativecommons.org/publicdomain/zero/1.0/ -->
  3. <!DOCTYPE HTML>
  4. <html>
  5. <head>
  6. <title>Test Cache with QuotaManager Restart</title>
  7. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  8. <script type="text/javascript" src="large_url_list.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  10. </head>
  11. <body>
  12. <script class="testbody" type="text/javascript">
  13. function setupTestIframe() {
  14. return new Promise(function(resolve) {
  15. var iframe = document.createElement("iframe");
  16. iframe.src = "empty.html";
  17. iframe.onload = function() {
  18. window.caches = iframe.contentWindow.caches;
  19. resolve();
  20. };
  21. document.body.appendChild(iframe);
  22. });
  23. }
  24. function clearStorage() {
  25. return new Promise(function(resolve, reject) {
  26. var qms = SpecialPowers.Services.qms;
  27. var principal = SpecialPowers.wrap(document).nodePrincipal;
  28. var request = qms.clearStoragesForPrincipal(principal);
  29. var cb = SpecialPowers.wrapCallback(resolve);
  30. request.callback = cb;
  31. });
  32. }
  33. function storageUsage() {
  34. return new Promise(function(resolve, reject) {
  35. var qms = SpecialPowers.Services.qms;
  36. var principal = SpecialPowers.wrap(document).nodePrincipal;
  37. var cb = SpecialPowers.wrapCallback(function(request) {
  38. var result = request.result;
  39. resolve(result.usage, result.fileUsage);
  40. });
  41. qms.getUsageForPrincipal(principal, cb);
  42. });
  43. }
  44. function resetStorage() {
  45. return new Promise(function(resolve, reject) {
  46. var qms = SpecialPowers.Services.qms;
  47. var request = qms.reset();
  48. var cb = SpecialPowers.wrapCallback(resolve);
  49. request.callback = cb;
  50. });
  51. }
  52. function gc() {
  53. return new Promise(function(resolve, reject) {
  54. SpecialPowers.exactGC(resolve);
  55. });
  56. }
  57. SimpleTest.waitForExplicitFinish();
  58. SpecialPowers.pushPrefEnv({
  59. "set": [["dom.caches.enabled", true],
  60. ["dom.caches.testing.enabled", true],
  61. ["dom.quotaManager.testing", true]],
  62. }, function() {
  63. var name = 'foo';
  64. var cache = null;
  65. var initialUsage = 0;
  66. var fullUsage = 0;
  67. var endUsage = 0;
  68. // start from a fresh origin directory so other tests do not influence our
  69. // results
  70. setupTestIframe().then(function() {
  71. return clearStorage();
  72. }).then(function() {
  73. return storageUsage();
  74. }).then(function(usage) {
  75. is(0, usage, 'disk usage should be zero to start');
  76. return caches.open(name);
  77. }).then(function(c) {
  78. cache = c;
  79. return storageUsage();
  80. }).then(function(usage) {
  81. initialUsage = usage;
  82. return Promise.all(largeUrlList.map(function(url) {
  83. return cache.put(new Request(url), new Response());
  84. }));
  85. }).then(function() {
  86. return cache.keys();
  87. }).then(function(keyList) {
  88. is(keyList.length, largeUrlList.length, 'Large URL list is stored in cache');
  89. cache = null;
  90. // Ensure the Cache DOM object is gone before proceeding. If its alive
  91. // it will keep the related entries on-disk as well.
  92. return gc();
  93. }).then(function() {
  94. // reset the quota manager storage to ensure the DB connection is flushed
  95. return resetStorage();
  96. }).then(function() {
  97. return storageUsage();
  98. }).then(function(usage) {
  99. fullUsage = usage;
  100. ok(fullUsage > initialUsage, 'disk usage should have grown');
  101. return caches.delete(name);
  102. }).then(function(result) {
  103. ok(result, 'cache should be deleted');
  104. // This is a bit superfluous, but its necessary to make sure the Cache is
  105. // fully deleted before we proceed. The deletion actually takes place in
  106. // two async steps. We don't want to resetStorage() until the second step
  107. // has taken place. This extra Cache operation ensure that all the
  108. // runnables have been flushed through the threads, etc.
  109. return caches.has(name);
  110. }).then(function(result) {
  111. ok(!result, 'cache should not exist in storage');
  112. // reset the quota manager storage to ensure the DB connection is flushed
  113. return resetStorage();
  114. }).then(function() {
  115. return storageUsage();
  116. }).then(function(usage) {
  117. endUsage = usage;
  118. dump("### ### initial:" + initialUsage + ", full:" + fullUsage +
  119. ", end:" + endUsage + "\n");
  120. ok(endUsage < (fullUsage / 2), 'disk usage should have shrank significantly');
  121. ok(endUsage > initialUsage, 'disk usage should not shrink back to orig size');
  122. SimpleTest.finish();
  123. });
  124. });
  125. </script>
  126. </body>
  127. </html>