Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / head_cache.js
blobbf86e5d186bf6b6fffce4785c52ebe1b961ac295
1 "use strict";
3 var { XPCOMUtils } = ChromeUtils.importESModule(
4 "resource://gre/modules/XPCOMUtils.sys.mjs"
5 );
7 function evict_cache_entries(where) {
8 var clearDisk = !where || where == "disk" || where == "all";
9 var clearMem = !where || where == "memory" || where == "all";
11 var storage;
13 if (clearMem) {
14 storage = Services.cache2.memoryCacheStorage(
15 Services.loadContextInfo.default
17 storage.asyncEvictStorage(null);
20 if (clearDisk) {
21 storage = Services.cache2.diskCacheStorage(
22 Services.loadContextInfo.default
24 storage.asyncEvictStorage(null);
28 function createURI(urispec) {
29 return Services.io.newURI(urispec);
32 function getCacheStorage(where, lci) {
33 if (!lci) {
34 lci = Services.loadContextInfo.default;
36 switch (where) {
37 case "disk":
38 return Services.cache2.diskCacheStorage(lci);
39 case "memory":
40 return Services.cache2.memoryCacheStorage(lci);
41 case "pin":
42 return Services.cache2.pinningCacheStorage(lci);
44 return null;
47 function asyncOpenCacheEntry(key, where, flags, lci, callback) {
48 key = createURI(key);
50 function CacheListener() {}
51 CacheListener.prototype = {
52 QueryInterface: ChromeUtils.generateQI(["nsICacheEntryOpenCallback"]),
54 onCacheEntryCheck(entry) {
55 if (typeof callback === "object") {
56 return callback.onCacheEntryCheck(entry);
58 return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
61 onCacheEntryAvailable(entry, isnew, status) {
62 if (typeof callback === "object") {
63 // Root us at the callback
64 callback.__cache_listener_root = this;
65 callback.onCacheEntryAvailable(entry, isnew, status);
66 } else {
67 callback(status, entry);
71 run() {
72 var storage = getCacheStorage(where, lci);
73 storage.asyncOpenURI(key, "", flags, this);
77 new CacheListener().run();
80 function syncWithCacheIOThread(callback, force) {
81 if (force) {
82 asyncOpenCacheEntry(
83 "http://nonexistententry/",
84 "disk",
85 Ci.nsICacheStorage.OPEN_READONLY,
86 null,
87 function (status) {
88 Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
89 callback();
92 } else {
93 callback();
97 function get_device_entry_count(where, lci, continuation) {
98 var storage = getCacheStorage(where, lci);
99 if (!storage) {
100 continuation(-1, 0);
101 return;
104 var visitor = {
105 onCacheStorageInfo(entryCount, consumption) {
106 executeSoon(function () {
107 continuation(entryCount, consumption);
112 // get the device entry count
113 storage.asyncVisitStorage(visitor, false);
116 function asyncCheckCacheEntryPresence(key, where, shouldExist, continuation) {
117 asyncOpenCacheEntry(
118 key,
119 where,
120 Ci.nsICacheStorage.OPEN_READONLY,
121 null,
122 function (status, entry) {
123 if (shouldExist) {
124 dump("TEST-INFO | checking cache key " + key + " exists @ " + where);
125 Assert.equal(status, Cr.NS_OK);
126 Assert.ok(!!entry);
127 } else {
128 dump(
129 "TEST-INFO | checking cache key " + key + " doesn't exist @ " + where
131 Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
132 Assert.equal(null, entry);
134 continuation();