2011-05-20 21:27:11 +04:00
|
|
|
function gen_1MiB()
|
|
|
|
{
|
|
|
|
var i;
|
|
|
|
var data="x";
|
|
|
|
for (i=0 ; i < 20 ; i++)
|
|
|
|
data+=data;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function write_and_check(str, data, len)
|
|
|
|
{
|
|
|
|
var written = str.write(data, len);
|
|
|
|
if (written != len) {
|
|
|
|
do_throw("str.write has not written all data!\n" +
|
|
|
|
" Expected: " + len + "\n" +
|
|
|
|
" Actual: " + written + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 01:22:28 +04:00
|
|
|
function write_big_datafile(status, entry)
|
2011-05-20 21:27:11 +04:00
|
|
|
{
|
2012-08-02 01:22:28 +04:00
|
|
|
do_check_eq(status, Cr.NS_OK);
|
|
|
|
var os = entry.openOutputStream(0);
|
2011-05-20 21:27:11 +04:00
|
|
|
var data = gen_1MiB();
|
|
|
|
|
2013-01-04 03:30:48 +04:00
|
|
|
// write 65MiB
|
2011-05-20 21:27:11 +04:00
|
|
|
var i;
|
|
|
|
for (i=0 ; i<65 ; i++)
|
2012-08-02 01:22:28 +04:00
|
|
|
write_and_check(os, data, data.length);
|
|
|
|
|
2013-01-04 03:30:48 +04:00
|
|
|
// another write should fail and the entry will be doomed
|
|
|
|
try {
|
|
|
|
write_and_check(os, data, data.length);
|
|
|
|
do_throw("write should fail");
|
|
|
|
} catch (e) {}
|
|
|
|
|
2012-08-02 01:22:28 +04:00
|
|
|
os.close();
|
|
|
|
entry.close();
|
2011-05-20 21:27:11 +04:00
|
|
|
|
2013-01-04 03:30:48 +04:00
|
|
|
// DoomEntry() is called while writing to the entry, but the data is really
|
|
|
|
// deleted (and the cache size updated) on the background thread when
|
|
|
|
// the entry is deactivated. We need to sync with the cache IO thread before
|
|
|
|
// we continue with the test.
|
2012-08-02 01:22:28 +04:00
|
|
|
syncWithCacheIOThread(run_test_2);
|
2011-05-20 21:27:11 +04:00
|
|
|
}
|
|
|
|
|
2012-08-02 01:22:28 +04:00
|
|
|
function write_big_metafile(status, entry)
|
2011-05-20 21:27:11 +04:00
|
|
|
{
|
2012-08-02 01:22:28 +04:00
|
|
|
do_check_eq(status, Cr.NS_OK);
|
|
|
|
var os = entry.openOutputStream(0);
|
2011-05-20 21:27:11 +04:00
|
|
|
var data = gen_1MiB();
|
|
|
|
|
2013-01-04 03:30:48 +04:00
|
|
|
// > 64MiB
|
2011-05-20 21:27:11 +04:00
|
|
|
var i;
|
|
|
|
for (i=0 ; i<65 ; i++)
|
2012-08-02 01:22:28 +04:00
|
|
|
entry.setMetaDataElement("metadata_"+i, data);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
entry.metaDataReady();
|
|
|
|
|
2012-08-02 01:22:28 +04:00
|
|
|
os.close();
|
|
|
|
entry.close();
|
|
|
|
|
|
|
|
// We don't check whether the cache is full while writing metadata. Also we
|
|
|
|
// write the metadata when closing the entry, so we need to write some data
|
|
|
|
// after closing this entry to invoke the cache cleanup.
|
2013-09-20 13:11:25 +04:00
|
|
|
asyncOpenCacheEntry("http://smalldata/",
|
|
|
|
"disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
|
2012-08-02 01:22:28 +04:00
|
|
|
write_and_doom_small_datafile);
|
|
|
|
}
|
|
|
|
|
|
|
|
function write_and_doom_small_datafile(status, entry)
|
|
|
|
{
|
|
|
|
do_check_eq(status, Cr.NS_OK);
|
|
|
|
var os = entry.openOutputStream(0);
|
|
|
|
var data = "0123456789";
|
2011-05-20 21:27:11 +04:00
|
|
|
|
2012-08-02 01:22:28 +04:00
|
|
|
write_and_check(os, data, data.length);
|
2011-05-20 21:27:11 +04:00
|
|
|
|
2012-08-02 01:22:28 +04:00
|
|
|
os.close();
|
2013-09-20 13:11:25 +04:00
|
|
|
entry.asyncDoom(null);
|
2012-08-02 01:22:28 +04:00
|
|
|
entry.close();
|
|
|
|
syncWithCacheIOThread(run_test_3);
|
2011-05-20 21:27:11 +04:00
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
function check_cache_size(cont) {
|
|
|
|
get_device_entry_count("disk", null, function(count, consumption) {
|
|
|
|
// Because the last entry we store is doomed using AsyncDoom and not Doom, it is still active
|
|
|
|
// during the visit processing, hence consumption is larger then 0 (one block is allocated).
|
|
|
|
// ...I really like all these small old-cache bugs, that will finally go away... :)
|
|
|
|
do_check_true(consumption <= 1024)
|
|
|
|
cont();
|
|
|
|
});
|
2011-05-20 21:27:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function run_test() {
|
2013-09-20 13:11:25 +04:00
|
|
|
if (newCacheBackEndUsed()) {
|
|
|
|
// browser.cache.disk.* (limits mainly) tests
|
|
|
|
do_check_true(true, "This test doesn't run with the new cache backend, the test or the cache needs to be fixed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-20 21:27:11 +04:00
|
|
|
var prefBranch = Cc["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Ci.nsIPrefBranch);
|
2013-01-04 03:30:48 +04:00
|
|
|
|
|
|
|
// set max entry size bigger than 64MiB
|
|
|
|
prefBranch.setIntPref("browser.cache.disk.max_entry_size", 65*1024);
|
|
|
|
// disk cache capacity must be at least 8 times bigger
|
|
|
|
prefBranch.setIntPref("browser.cache.disk.capacity", 8*65*1024);
|
|
|
|
// disable smart size
|
|
|
|
prefBranch.setBoolPref("browser.cache.disk.smart_size.enabled", false);
|
2011-05-20 21:27:11 +04:00
|
|
|
|
|
|
|
do_get_profile();
|
|
|
|
|
|
|
|
// clear the cache
|
2012-08-02 01:22:28 +04:00
|
|
|
evict_cache_entries();
|
2011-05-20 21:27:11 +04:00
|
|
|
|
|
|
|
// write an entry with data > 64MiB
|
2013-09-20 13:11:25 +04:00
|
|
|
asyncOpenCacheEntry("http://bigdata/",
|
|
|
|
"disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
|
2012-08-02 01:22:28 +04:00
|
|
|
write_big_datafile);
|
2011-05-20 21:27:11 +04:00
|
|
|
|
|
|
|
do_test_pending();
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_test_2()
|
|
|
|
{
|
2013-09-20 13:11:25 +04:00
|
|
|
check_cache_size(run_test_2a);
|
|
|
|
}
|
2011-05-20 21:27:11 +04:00
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
function run_test_2a()
|
|
|
|
{
|
2013-01-04 03:30:48 +04:00
|
|
|
var prefBranch = Cc["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Ci.nsIPrefBranch);
|
|
|
|
|
|
|
|
// set cache capacity lower than max entry size (see comment in
|
|
|
|
// write_big_metafile)
|
|
|
|
prefBranch.setIntPref("browser.cache.disk.capacity", 64*1024);
|
|
|
|
|
2011-05-20 21:27:11 +04:00
|
|
|
// write an entry with metadata > 64MiB
|
2013-09-20 13:11:25 +04:00
|
|
|
asyncOpenCacheEntry("http://bigmetadata/",
|
|
|
|
"disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
|
2012-08-02 01:22:28 +04:00
|
|
|
write_big_metafile);
|
2011-05-20 21:27:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function run_test_3()
|
|
|
|
{
|
2013-09-20 13:11:25 +04:00
|
|
|
check_cache_size(do_test_finished);
|
2011-05-20 21:27:11 +04:00
|
|
|
}
|