3 This is a complex test checking the internal "deferred doom" functionality in both CacheEntry and CacheFileHandle.
5 - We create a batch of 10 non-pinned and 10 pinned entries, write something to them.
6 - Then we purge them from memory, so they have to reload from disk.
7 - After that the IO thread is suspended not to process events on the READ (3) level. This forces opening operation and eviction
8 sync operations happen before we know actual pinning status of already cached entries.
9 - We async-open the same batch of the 10+10 entries again, all should open as existing with the expected, previously stored
11 - After all these entries are made to open, we clear the cache. This does some synchronous operations on the entries
12 being open and also on the handles being in an already open state (but before the entry metadata has started to be read.)
13 Expected is to leave the pinned entries only.
14 - Now, we resume the IO thread, so it start reading. One could say this is a hack, but this can very well happen in reality
15 on slow disk or when a large number of entries is about to be open at once. Suspending the IO thread is just doing this
16 simulation is a fully deterministic way and actually very easily and elegantly.
17 - After the resume we want to open all those 10+10 entries once again (no purgin involved this time.). It is expected
18 to open all the pinning entries intact and loose all the non-pinned entries (get them as new and empty again.)
24 const kENTRYCOUNT = 10;
28 dump(">>>>>>>>>>>>> " + msg + "\n");
35 var lci = Services.loadContextInfo.default;
36 var testingInterface = Services.cache2.QueryInterface(Ci.nsICacheTesting);
37 Assert.ok(testingInterface);
39 var mc = new MultipleCallbacks(
44 mc = new MultipleCallbacks(1, finish_cache2_test);
45 // Release all references to cache entries so that they can be purged
46 // Calling gc() four times is needed to force it to actually release
47 // entries that are obviously unreferenced. Yeah, I know, this is wacky...
55 // Invokes cacheservice:purge-memory-pools when done.
56 Services.cache2.purgeFromMemory(
57 Ci.nsICacheStorageService.PURGE_EVERYTHING
67 for (i = 0; i < kENTRYCOUNT; ++i) {
68 log_("first set of opens");
73 "http://pinned" + i + "/",
75 Ci.nsICacheStorage.OPEN_TRUNCATE,
77 new OpenCallback(NEW | WAITFORWRITE, "m" + i, "p" + i, function (entry) {
84 "http://common" + i + "/",
86 Ci.nsICacheStorage.OPEN_TRUNCATE,
88 new OpenCallback(NEW | WAITFORWRITE, "m" + i, "d" + i, function (entry) {
94 mc.fired(); // Goes to (2)
96 Services.obs.addObserver(
98 observe(subject, topic, data) {
101 log_("after purge, second set of opens");
102 // Prevent the I/O thread from reading the data. We first want to schedule clear of the cache.
103 // This deterministically emulates a slow hard drive.
104 testingInterface.suspendCacheIOThread(3);
106 // All entries should load
108 for (i = 0; i < kENTRYCOUNT; ++i) {
111 "http://pinned" + i + "/",
113 Ci.nsICacheStorage.OPEN_NORMALLY,
115 new OpenCallback(NORMAL, "m" + i, "p" + i, function (entry) {
120 // Unfortunately we cannot ensure that entries existing in the cache will be delivered to the consumer
121 // when soon after are evicted by some cache API call. It's better to not ensure getting an entry
122 // than allowing to get an entry that was just evicted from the cache. Entries may be delievered
123 // as new, but are already doomed. Output stream cannot be openned, or the file handle is already
124 // writing to a doomed file.
126 // The API now just ensures that entries removed by any of the cache eviction APIs are never more
127 // available to consumers.
130 "http://common" + i + "/",
132 Ci.nsICacheStorage.OPEN_NORMALLY,
134 new OpenCallback(MAYBE_NEW | DOOMED, "m" + i, "d" + i, function (
143 // Now clear everything except pinned, all entries are in state of reading
144 Services.cache2.clear();
147 // Resume reading the cache data, only now the pinning status on entries will be discovered,
148 // the deferred dooming code will trigger.
149 testingInterface.resumeCacheIOThread();
151 log_("third set of opens");
152 // Now open again. Pinned entries should be there, disk entries should be the renewed entries.
154 for (i = 0; i < kENTRYCOUNT; ++i) {
157 "http://pinned" + i + "/",
159 Ci.nsICacheStorage.OPEN_NORMALLY,
161 new OpenCallback(NORMAL, "m" + i, "p" + i, function (entry) {
168 "http://common" + i + "/",
170 Ci.nsICacheStorage.OPEN_NORMALLY,
172 new OpenCallback(NEW, "m2" + i, "d2" + i, function (entry) {
178 mc.fired(); // Finishes this test
181 "cacheservice:purge-memory-pools"