Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | function gen_1MiB() |
michael@0 | 2 | { |
michael@0 | 3 | var i; |
michael@0 | 4 | var data="x"; |
michael@0 | 5 | for (i=0 ; i < 20 ; i++) |
michael@0 | 6 | data+=data; |
michael@0 | 7 | return data; |
michael@0 | 8 | } |
michael@0 | 9 | |
michael@0 | 10 | function write_and_check(str, data, len) |
michael@0 | 11 | { |
michael@0 | 12 | var written = str.write(data, len); |
michael@0 | 13 | if (written != len) { |
michael@0 | 14 | do_throw("str.write has not written all data!\n" + |
michael@0 | 15 | " Expected: " + len + "\n" + |
michael@0 | 16 | " Actual: " + written + "\n"); |
michael@0 | 17 | } |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | function write_big_datafile(status, entry) |
michael@0 | 21 | { |
michael@0 | 22 | do_check_eq(status, Cr.NS_OK); |
michael@0 | 23 | var os = entry.openOutputStream(0); |
michael@0 | 24 | var data = gen_1MiB(); |
michael@0 | 25 | |
michael@0 | 26 | // write 65MiB |
michael@0 | 27 | var i; |
michael@0 | 28 | for (i=0 ; i<65 ; i++) |
michael@0 | 29 | write_and_check(os, data, data.length); |
michael@0 | 30 | |
michael@0 | 31 | // another write should fail and the entry will be doomed |
michael@0 | 32 | try { |
michael@0 | 33 | write_and_check(os, data, data.length); |
michael@0 | 34 | do_throw("write should fail"); |
michael@0 | 35 | } catch (e) {} |
michael@0 | 36 | |
michael@0 | 37 | os.close(); |
michael@0 | 38 | entry.close(); |
michael@0 | 39 | |
michael@0 | 40 | // DoomEntry() is called while writing to the entry, but the data is really |
michael@0 | 41 | // deleted (and the cache size updated) on the background thread when |
michael@0 | 42 | // the entry is deactivated. We need to sync with the cache IO thread before |
michael@0 | 43 | // we continue with the test. |
michael@0 | 44 | syncWithCacheIOThread(run_test_2); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | function write_big_metafile(status, entry) |
michael@0 | 48 | { |
michael@0 | 49 | do_check_eq(status, Cr.NS_OK); |
michael@0 | 50 | var os = entry.openOutputStream(0); |
michael@0 | 51 | var data = gen_1MiB(); |
michael@0 | 52 | |
michael@0 | 53 | // > 64MiB |
michael@0 | 54 | var i; |
michael@0 | 55 | for (i=0 ; i<65 ; i++) |
michael@0 | 56 | entry.setMetaDataElement("metadata_"+i, data); |
michael@0 | 57 | |
michael@0 | 58 | entry.metaDataReady(); |
michael@0 | 59 | |
michael@0 | 60 | os.close(); |
michael@0 | 61 | entry.close(); |
michael@0 | 62 | |
michael@0 | 63 | // We don't check whether the cache is full while writing metadata. Also we |
michael@0 | 64 | // write the metadata when closing the entry, so we need to write some data |
michael@0 | 65 | // after closing this entry to invoke the cache cleanup. |
michael@0 | 66 | asyncOpenCacheEntry("http://smalldata/", |
michael@0 | 67 | "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null, |
michael@0 | 68 | write_and_doom_small_datafile); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | function write_and_doom_small_datafile(status, entry) |
michael@0 | 72 | { |
michael@0 | 73 | do_check_eq(status, Cr.NS_OK); |
michael@0 | 74 | var os = entry.openOutputStream(0); |
michael@0 | 75 | var data = "0123456789"; |
michael@0 | 76 | |
michael@0 | 77 | write_and_check(os, data, data.length); |
michael@0 | 78 | |
michael@0 | 79 | os.close(); |
michael@0 | 80 | entry.asyncDoom(null); |
michael@0 | 81 | entry.close(); |
michael@0 | 82 | syncWithCacheIOThread(run_test_3); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | function check_cache_size(cont) { |
michael@0 | 86 | get_device_entry_count("disk", null, function(count, consumption) { |
michael@0 | 87 | // Because the last entry we store is doomed using AsyncDoom and not Doom, it is still active |
michael@0 | 88 | // during the visit processing, hence consumption is larger then 0 (one block is allocated). |
michael@0 | 89 | // ...I really like all these small old-cache bugs, that will finally go away... :) |
michael@0 | 90 | do_check_true(consumption <= 1024) |
michael@0 | 91 | cont(); |
michael@0 | 92 | }); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | function run_test() { |
michael@0 | 96 | if (newCacheBackEndUsed()) { |
michael@0 | 97 | // browser.cache.disk.* (limits mainly) tests |
michael@0 | 98 | do_check_true(true, "This test doesn't run with the new cache backend, the test or the cache needs to be fixed"); |
michael@0 | 99 | return; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | var prefBranch = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 103 | getService(Ci.nsIPrefBranch); |
michael@0 | 104 | |
michael@0 | 105 | // set max entry size bigger than 64MiB |
michael@0 | 106 | prefBranch.setIntPref("browser.cache.disk.max_entry_size", 65*1024); |
michael@0 | 107 | // disk cache capacity must be at least 8 times bigger |
michael@0 | 108 | prefBranch.setIntPref("browser.cache.disk.capacity", 8*65*1024); |
michael@0 | 109 | // disable smart size |
michael@0 | 110 | prefBranch.setBoolPref("browser.cache.disk.smart_size.enabled", false); |
michael@0 | 111 | |
michael@0 | 112 | do_get_profile(); |
michael@0 | 113 | |
michael@0 | 114 | // clear the cache |
michael@0 | 115 | evict_cache_entries(); |
michael@0 | 116 | |
michael@0 | 117 | // write an entry with data > 64MiB |
michael@0 | 118 | asyncOpenCacheEntry("http://bigdata/", |
michael@0 | 119 | "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null, |
michael@0 | 120 | write_big_datafile); |
michael@0 | 121 | |
michael@0 | 122 | do_test_pending(); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | function run_test_2() |
michael@0 | 126 | { |
michael@0 | 127 | check_cache_size(run_test_2a); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | function run_test_2a() |
michael@0 | 131 | { |
michael@0 | 132 | var prefBranch = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 133 | getService(Ci.nsIPrefBranch); |
michael@0 | 134 | |
michael@0 | 135 | // set cache capacity lower than max entry size (see comment in |
michael@0 | 136 | // write_big_metafile) |
michael@0 | 137 | prefBranch.setIntPref("browser.cache.disk.capacity", 64*1024); |
michael@0 | 138 | |
michael@0 | 139 | // write an entry with metadata > 64MiB |
michael@0 | 140 | asyncOpenCacheEntry("http://bigmetadata/", |
michael@0 | 141 | "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null, |
michael@0 | 142 | write_big_metafile); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | function run_test_3() |
michael@0 | 146 | { |
michael@0 | 147 | check_cache_size(do_test_finished); |
michael@0 | 148 | } |