netwerk/test/unit/test_bug651100.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug651100.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,148 @@
     1.4 +function gen_1MiB()
     1.5 +{
     1.6 +  var i;
     1.7 +  var data="x";
     1.8 +  for (i=0 ; i < 20 ; i++)
     1.9 +    data+=data;
    1.10 +  return data;
    1.11 +}
    1.12 +
    1.13 +function write_and_check(str, data, len)
    1.14 +{
    1.15 +  var written = str.write(data, len);
    1.16 +  if (written != len) {
    1.17 +    do_throw("str.write has not written all data!\n" +
    1.18 +             "  Expected: " + len  + "\n" +
    1.19 +             "  Actual: " + written + "\n");
    1.20 +  }
    1.21 +}
    1.22 +
    1.23 +function write_big_datafile(status, entry)
    1.24 +{
    1.25 +  do_check_eq(status, Cr.NS_OK);
    1.26 +  var os = entry.openOutputStream(0);
    1.27 +  var data = gen_1MiB();
    1.28 +
    1.29 +  // write 65MiB
    1.30 +  var i;
    1.31 +  for (i=0 ; i<65 ; i++)
    1.32 +    write_and_check(os, data, data.length);
    1.33 +
    1.34 +  // another write should fail and the entry will be doomed
    1.35 +  try {
    1.36 +    write_and_check(os, data, data.length);
    1.37 +    do_throw("write should fail");
    1.38 +  } catch (e) {}
    1.39 +
    1.40 +  os.close();
    1.41 +  entry.close();
    1.42 +
    1.43 +  // DoomEntry() is called while writing to the entry, but the data is really
    1.44 +  // deleted (and the cache size updated) on the background thread when
    1.45 +  // the entry is deactivated. We need to sync with the cache IO thread before
    1.46 +  // we continue with the test.
    1.47 +  syncWithCacheIOThread(run_test_2);
    1.48 +}
    1.49 +
    1.50 +function write_big_metafile(status, entry)
    1.51 +{
    1.52 +  do_check_eq(status, Cr.NS_OK);
    1.53 +  var os = entry.openOutputStream(0);
    1.54 +  var data = gen_1MiB();
    1.55 +
    1.56 +  // > 64MiB
    1.57 +  var i;
    1.58 +  for (i=0 ; i<65 ; i++)
    1.59 +    entry.setMetaDataElement("metadata_"+i, data);
    1.60 +
    1.61 +  entry.metaDataReady();
    1.62 +
    1.63 +  os.close();
    1.64 +  entry.close();
    1.65 +
    1.66 +  // We don't check whether the cache is full while writing metadata. Also we
    1.67 +  // write the metadata when closing the entry, so we need to write some data
    1.68 +  // after closing this entry to invoke the cache cleanup.
    1.69 +  asyncOpenCacheEntry("http://smalldata/",
    1.70 +                      "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
    1.71 +                      write_and_doom_small_datafile);
    1.72 +}
    1.73 +
    1.74 +function write_and_doom_small_datafile(status, entry)
    1.75 +{
    1.76 +  do_check_eq(status, Cr.NS_OK);
    1.77 +  var os = entry.openOutputStream(0);
    1.78 +  var data = "0123456789";
    1.79 +
    1.80 +  write_and_check(os, data, data.length);
    1.81 +
    1.82 +  os.close();
    1.83 +  entry.asyncDoom(null);
    1.84 +  entry.close();
    1.85 +  syncWithCacheIOThread(run_test_3);
    1.86 +}
    1.87 +
    1.88 +function check_cache_size(cont) {
    1.89 +  get_device_entry_count("disk", null, function(count, consumption) {
    1.90 +    // Because the last entry we store is doomed using AsyncDoom and not Doom, it is still active
    1.91 +    // during the visit processing, hence consumption is larger then 0 (one block is allocated).
    1.92 +    // ...I really like all these small old-cache bugs, that will finally go away... :)
    1.93 +    do_check_true(consumption <= 1024)
    1.94 +    cont();
    1.95 +  });
    1.96 +}
    1.97 +
    1.98 +function run_test() {
    1.99 +  if (newCacheBackEndUsed()) {
   1.100 +    // browser.cache.disk.* (limits mainly) tests
   1.101 +    do_check_true(true, "This test doesn't run with the new cache backend, the test or the cache needs to be fixed");
   1.102 +    return;
   1.103 +  }
   1.104 +
   1.105 +  var prefBranch = Cc["@mozilla.org/preferences-service;1"].
   1.106 +                     getService(Ci.nsIPrefBranch);
   1.107 +
   1.108 +  // set max entry size bigger than 64MiB
   1.109 +  prefBranch.setIntPref("browser.cache.disk.max_entry_size", 65*1024);
   1.110 +  // disk cache capacity must be at least 8 times bigger
   1.111 +  prefBranch.setIntPref("browser.cache.disk.capacity", 8*65*1024);
   1.112 +  // disable smart size
   1.113 +  prefBranch.setBoolPref("browser.cache.disk.smart_size.enabled", false);
   1.114 +
   1.115 +  do_get_profile();
   1.116 +
   1.117 +  // clear the cache
   1.118 +  evict_cache_entries();
   1.119 +
   1.120 +  // write an entry with data > 64MiB
   1.121 +  asyncOpenCacheEntry("http://bigdata/",
   1.122 +                      "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
   1.123 +                      write_big_datafile);
   1.124 +
   1.125 +  do_test_pending();
   1.126 +}
   1.127 +
   1.128 +function run_test_2()
   1.129 +{
   1.130 +  check_cache_size(run_test_2a);
   1.131 +}
   1.132 +
   1.133 +function run_test_2a()
   1.134 +{
   1.135 +  var prefBranch = Cc["@mozilla.org/preferences-service;1"].
   1.136 +                     getService(Ci.nsIPrefBranch);
   1.137 +
   1.138 +  // set cache capacity lower than max entry size (see comment in
   1.139 +  // write_big_metafile)
   1.140 +  prefBranch.setIntPref("browser.cache.disk.capacity", 64*1024);
   1.141 +
   1.142 +  // write an entry with metadata > 64MiB
   1.143 +  asyncOpenCacheEntry("http://bigmetadata/",
   1.144 +                      "disk", Ci.nsICacheStorage.OPEN_TRUNCATE, null,
   1.145 +                      write_big_metafile);
   1.146 +}
   1.147 +
   1.148 +function run_test_3()
   1.149 +{
   1.150 +  check_cache_size(do_test_finished);
   1.151 +}

mercurial