Bug 737615 - Remove use of synchronous cache API from unit tests - necko tests part2, r=honzab

This commit is contained in:
Michal Novotny 2012-08-01 23:22:28 +02:00
Родитель 3c94126220
Коммит b7ae28693b
4 изменённых файлов: 145 добавлений и 178 удалений

Просмотреть файл

@ -43,4 +43,15 @@ function asyncOpenCacheEntry(key, sessionName, storagePolicy, access, callback)
(new CacheListener()).run();
}
function syncWithCacheIOThread(callback)
{
asyncOpenCacheEntry(
"nonexistententry",
"HTTP",
Components.interfaces.nsICache.STORE_ANYWHERE,
Components.interfaces.nsICache.ACCESS_READ,
function(status, entry) {
do_check_eq(status, Components.results.NS_ERROR_CACHE_KEY_NOT_FOUND);
callback();
});
}

Просмотреть файл

@ -17,31 +17,6 @@ function repeatToLargerThan1K(data) {
return data;
}
function SyncWithCacheThread(aFunc) {
do_check_eq(sync_with_cache_IO_thread_cb.listener, null);
sync_with_cache_IO_thread_cb.listener = aFunc;
var cache = get_cache_service();
var session = cache.createSession(
"HTTP",
Ci.nsICache.STORE_ANYWHERE,
Ci.nsICache.STREAM_BASED);
session.asyncOpenCacheEntry("nonexistententry",
Ci.nsICache.ACCESS_READ,
sync_with_cache_IO_thread_cb);
}
var sync_with_cache_IO_thread_cb = {
listener: null,
onCacheEntryAvailable: function oCEA(descriptor, accessGranted, status) {
do_check_neq(status, Cr.NS_OK);
cb = this.listener;
this.listener = null;
do_execute_soon(cb);
}
};
function setupChannel(suffix, value) {
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
@ -79,9 +54,9 @@ var tests = [
function nextTest() {
// We really want each test to be self-contained. Make sure cache is
// cleared and also let all operations finish before starting a new test
SyncWithCacheThread(function() {
syncWithCacheIOThread(function() {
evict_cache_entries();
SyncWithCacheThread(runNextTest);
syncWithCacheIOThread(runNextTest);
});
}

Просмотреть файл

@ -2,29 +2,6 @@ const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
var _CSvc;
function get_cache_service() {
if (_CSvc)
return _CSvc;
return _CSvc = Cc["@mozilla.org/network/cache-service;1"].
getService(Ci.nsICacheService);
}
function get_ostream_for_entry(key, asFile, entryRef)
{
var cache = get_cache_service();
var session = cache.createSession(
"HTTP",
asFile ? Ci.nsICache.STORE_ON_DISK_AS_FILE
: Ci.nsICache.STORE_ON_DISK,
Ci.nsICache.STREAM_BASED);
var cacheEntry = session.openCacheEntry(key, Ci.nsICache.ACCESS_WRITE, true);
var oStream = cacheEntry.openOutputStream(0);
entryRef.value = cacheEntry;
return oStream;
}
function gen_1MiB()
{
var i;
@ -44,37 +21,63 @@ function write_and_check(str, data, len)
}
}
function write_big_datafile()
function write_big_datafile(status, entry)
{
var entry = {};
var oStr = get_ostream_for_entry("bigdata", true, entry);
do_check_eq(status, Cr.NS_OK);
var os = entry.openOutputStream(0);
var data = gen_1MiB();
// >64MiB
var i;
for (i=0 ; i<65 ; i++)
write_and_check(oStr, data, data.length);
write_and_check(os, data, data.length);
oStr.close();
entry.value.close();
os.close();
entry.close();
// DoomEntry() is called when the cache is full, 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.
syncWithCacheIOThread(run_test_2);
}
function write_and_doom_big_metafile()
function write_big_metafile(status, entry)
{
var entry = {};
var oStr = get_ostream_for_entry("bigmetadata", true, entry);
do_check_eq(status, Cr.NS_OK);
var os = entry.openOutputStream(0);
var data = gen_1MiB();
// >64MiB
var i;
for (i=0 ; i<65 ; i++)
entry.value.setMetaDataElement("metadata_"+i, data);
entry.setMetaDataElement("metadata_"+i, data);
write_and_check(oStr, data, data.length);
os.close();
entry.close();
oStr.close();
entry.value.doom();
entry.value.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.
asyncOpenCacheEntry("smalldata",
"HTTP",
Ci.nsICache.STORE_ON_DISK_AS_FILE,
Ci.nsICache.ACCESS_WRITE,
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";
write_and_check(os, data, data.length);
os.close();
entry.doom();
entry.close();
syncWithCacheIOThread(run_test_3);
}
function check_cache_size() {
@ -98,34 +101,6 @@ function check_cache_size() {
do_check_true(diskDeviceVisited);
}
var _continue_with = null;
function sync_with_cache_IO_thread(aFunc)
{
do_check_eq(sync_with_cache_IO_thread_cb.listener, null);
sync_with_cache_IO_thread_cb.listener = aFunc;
var cache = get_cache_service();
var session = cache.createSession(
"HTTP",
Ci.nsICache.STORE_ON_DISK,
Ci.nsICache.STREAM_BASED);
var cacheEntry = session.asyncOpenCacheEntry(
"nonexistententry",
Ci.nsICache.ACCESS_READ,
sync_with_cache_IO_thread_cb);
}
var sync_with_cache_IO_thread_cb = {
listener: null,
onCacheEntryAvailable: function oCEA(descriptor, accessGranted, status) {
do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
cb = this.listener;
this.listener = null;
do_timeout(0, cb);
}
};
function run_test() {
var prefBranch = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
@ -134,16 +109,14 @@ function run_test() {
do_get_profile();
// clear the cache
get_cache_service().evictEntries(Ci.nsICache.STORE_ANYWHERE);
evict_cache_entries();
// write an entry with data > 64MiB
write_big_datafile();
// DoomEntry() is called when the cache is full, 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.
sync_with_cache_IO_thread(run_test_2);
asyncOpenCacheEntry("bigdata",
"HTTP",
Ci.nsICache.STORE_ON_DISK_AS_FILE,
Ci.nsICache.ACCESS_WRITE,
write_big_datafile);
do_test_pending();
}
@ -153,9 +126,11 @@ function run_test_2()
check_cache_size();
// write an entry with metadata > 64MiB
write_and_doom_big_metafile();
sync_with_cache_IO_thread(run_test_3);
asyncOpenCacheEntry("bigmetadata",
"HTTP",
Ci.nsICache.STORE_ON_DISK_AS_FILE,
Ci.nsICache.ACCESS_WRITE,
write_big_metafile);
}
function run_test_3()

Просмотреть файл

@ -165,42 +165,40 @@ function check_bug() {
let appcache =
appcache_service.chooseApplicationCache(kHttpLocation + "pages/foo1");
let clientID = appcache.clientID;
let cache_service = Cc[kNS_CACHESERVICE_CONTRACTID].
getService(Ci.nsICacheService);
let session = cache_service.createSession(clientID,
Ci.nsICache.STORE_OFFLINE,
true);
// activate foo1
let entry = session.openCacheEntry(kHttpLocation + "pages/foo1",
Ci.nsICache.ACCESS_READ,
true);
asyncOpenCacheEntry(
kHttpLocation + "pages/foo1",
clientID,
Ci.nsICache.STORE_OFFLINE,
Ci.nsICache.ACCESS_READ,
function(status, entry) {
var count = 0;
var count = 0;
let listener = {
onCacheEntryDoomed: function (status) {
do_print("doomed " + count);
do_check_eq(status, 0);
count = count + 1;
do_check_true(count <= 2, "too much callbacks");
if (count == 2) {
do_timeout(100, function () { check_evict_cache(clientID); });
}
},
};
let listener = {
onCacheEntryAvailable: function onCacheEntryAvailable(descriptor,
accessGranted,
status) {
},
onCacheEntryDoomed: function onCacheEntryDoomed(status) {
do_print("doomed " + count);
do_check_eq(status, 0);
count = count + 1;
do_check_true(count <= 2, "too much callbacks");
if (count == 2) {
do_timeout(100, function () { check_evict_cache(session); });
}
},
};
let session = get_cache_service().createSession(clientID,
Ci.nsICache.STORE_OFFLINE,
Ci.nsICache.STREAM_BASED);
// Doom foo1 & foo2
session.doomEntry(kHttpLocation + "pages/foo1", listener);
session.doomEntry(kHttpLocation + "pages/foo2", listener);
// Doom foo1 & foo2
session.doomEntry(kHttpLocation + "pages/foo1", listener);
session.doomEntry(kHttpLocation + "pages/foo2", listener);
hold_entry_foo1 = entry;
hold_entry_foo1 = entry;
});
}
function check_evict_cache(session) {
function check_evict_cache(clientID) {
// Only foo2 should be removed.
let file = do_get_profile().clone();
file.append("OfflineCache");
@ -217,60 +215,68 @@ function check_evict_cache(session) {
do_check_eq(file.exists(), false);
// activate foo3
hold_entry_foo3 = session.openCacheEntry(kHttpLocation + "pages/foo3",
Ci.nsICache.ACCESS_READ,
true);
asyncOpenCacheEntry(
kHttpLocation + "pages/foo3",
clientID,
Ci.nsICache.STORE_OFFLINE,
Ci.nsICache.ACCESS_READ,
function(status, entry) {
hold_entry_foo3 = entry;
// evict all documents.
session.evictEntries();
// evict all documents.
get_cache_service().createSession(clientID,
Ci.nsICache.STORE_OFFLINE,
Ci.nsICache.STREAM_BASED).
evictEntries();
// All documents are removed except foo1 & foo3.
// All documents are removed except foo1 & foo3.
// foo1
let file = do_get_profile().clone();
file.append("OfflineCache");
file.append("5");
file.append("9");
file.append("8379C6596B8CA4-0");
do_check_eq(file.exists(), true);
// foo1
let file = do_get_profile().clone();
file.append("OfflineCache");
file.append("5");
file.append("9");
file.append("8379C6596B8CA4-0");
do_check_eq(file.exists(), true);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("0");
file.append("0");
file.append("61FEE819921D39-0");
do_check_eq(file.exists(), false);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("0");
file.append("0");
file.append("61FEE819921D39-0");
do_check_eq(file.exists(), false);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("3");
file.append("9");
file.append("0D8759F1DE5452-0");
do_check_eq(file.exists(), false);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("3");
file.append("9");
file.append("0D8759F1DE5452-0");
do_check_eq(file.exists(), false);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("C");
file.append("2");
file.append("5F356A168B5E3B-0");
do_check_eq(file.exists(), false);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("C");
file.append("2");
file.append("5F356A168B5E3B-0");
do_check_eq(file.exists(), false);
// foo3
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("D");
file.append("C");
file.append("1ADCCC843B5C00-0");
do_check_eq(file.exists(), true);
// foo3
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("D");
file.append("C");
file.append("1ADCCC843B5C00-0");
do_check_eq(file.exists(), true);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("F");
file.append("0");
file.append("FC3E6D6C1164E9-0");
do_check_eq(file.exists(), false);
file = do_get_profile().clone();
file.append("OfflineCache");
file.append("F");
file.append("0");
file.append("FC3E6D6C1164E9-0");
do_check_eq(file.exists(), false);
httpServer.stop(do_test_finished);
httpServer.stop(do_test_finished);
});
}
function run_test() {