Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: third_party/WebKit/Source/core/fetch/MemoryCacheTest.cpp

Issue 2411243004: [WeakMemoryCache] Remove LRU lists, prune order control and live/dead distinction (Closed)
Patch Set: Reflect yhirano's comment Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013, Google Inc. All rights reserved. 2 * Copyright (c) 2013, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); 87 replaceMemoryCacheForTesting(m_globalMemoryCache.release());
88 } 88 }
89 89
90 Persistent<MemoryCache> m_globalMemoryCache; 90 Persistent<MemoryCache> m_globalMemoryCache;
91 }; 91 };
92 92
93 // Verifies that setters and getters for cache capacities work correcty. 93 // Verifies that setters and getters for cache capacities work correcty.
94 TEST_F(MemoryCacheTest, CapacityAccounting) { 94 TEST_F(MemoryCacheTest, CapacityAccounting) {
95 const size_t sizeMax = ~static_cast<size_t>(0); 95 const size_t sizeMax = ~static_cast<size_t>(0);
96 const size_t totalCapacity = sizeMax / 4; 96 const size_t totalCapacity = sizeMax / 4;
97 const size_t minDeadCapacity = sizeMax / 16; 97 memoryCache()->setCapacity(totalCapacity);
98 const size_t maxDeadCapacity = sizeMax / 8;
99 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);
100 EXPECT_EQ(totalCapacity, memoryCache()->capacity()); 98 EXPECT_EQ(totalCapacity, memoryCache()->capacity());
101 EXPECT_EQ(minDeadCapacity, memoryCache()->minDeadCapacity());
102 EXPECT_EQ(maxDeadCapacity, memoryCache()->maxDeadCapacity());
103 } 99 }
104 100
105 TEST_F(MemoryCacheTest, VeryLargeResourceAccounting) { 101 TEST_F(MemoryCacheTest, VeryLargeResourceAccounting) {
106 const size_t sizeMax = ~static_cast<size_t>(0); 102 const size_t sizeMax = ~static_cast<size_t>(0);
107 const size_t totalCapacity = sizeMax / 4; 103 const size_t totalCapacity = sizeMax / 4;
108 const size_t minDeadCapacity = sizeMax / 16;
109 const size_t maxDeadCapacity = sizeMax / 8;
110 const size_t resourceSize1 = sizeMax / 16; 104 const size_t resourceSize1 = sizeMax / 16;
111 const size_t resourceSize2 = sizeMax / 20; 105 const size_t resourceSize2 = sizeMax / 20;
112 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity); 106 memoryCache()->setCapacity(totalCapacity);
113 FakeResource* cachedResource = FakeResource::create( 107 FakeResource* cachedResource = FakeResource::create(
114 ResourceRequest("http://test/resource"), Resource::Raw); 108 ResourceRequest("http://test/resource"), Resource::Raw);
115 cachedResource->fakeEncodedSize(resourceSize1); 109 cachedResource->fakeEncodedSize(resourceSize1);
116 110
117 EXPECT_EQ(0u, memoryCache()->deadSize()); 111 EXPECT_EQ(0u, memoryCache()->size());
118 EXPECT_EQ(0u, memoryCache()->liveSize());
119 memoryCache()->add(cachedResource); 112 memoryCache()->add(cachedResource);
120 EXPECT_EQ(cachedResource->size(), memoryCache()->deadSize()); 113 EXPECT_EQ(cachedResource->size(), memoryCache()->size());
121 EXPECT_EQ(0u, memoryCache()->liveSize());
122 114
123 Persistent<MockResourceClient> client = 115 Persistent<MockResourceClient> client =
124 new MockResourceClient(cachedResource); 116 new MockResourceClient(cachedResource);
125 EXPECT_EQ(0u, memoryCache()->deadSize()); 117 EXPECT_EQ(cachedResource->size(), memoryCache()->size());
126 EXPECT_EQ(cachedResource->size(), memoryCache()->liveSize());
127 118
128 cachedResource->fakeEncodedSize(resourceSize2); 119 cachedResource->fakeEncodedSize(resourceSize2);
129 EXPECT_EQ(0u, memoryCache()->deadSize()); 120 EXPECT_EQ(cachedResource->size(), memoryCache()->size());
130 EXPECT_EQ(cachedResource->size(), memoryCache()->liveSize());
131 } 121 }
132 122
133 // Verifies that dead resources that exceed dead resource capacity are evicted 123 static void runTask1(Resource* resource1, Resource* resource2) {
134 // from cache when pruning.
135 static void TestDeadResourceEviction(Resource* resource1, Resource* resource2) {
136 memoryCache()->setDelayBeforeLiveDecodedPrune(0);
137 memoryCache()->setMaxPruneDeferralDelay(0);
138 const unsigned totalCapacity = 1000000;
139 const unsigned minDeadCapacity = 0;
140 const unsigned maxDeadCapacity = 0;
141 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);
142
143 const char data[5] = "abcd";
144 resource1->appendData(data, 3u);
145 resource2->appendData(data, 2u);
146
147 // The resource size has to be nonzero for this test to be meaningful, but 124 // The resource size has to be nonzero for this test to be meaningful, but
148 // we do not rely on it having any particular value. 125 // we do not rely on it having any particular value.
149 EXPECT_GT(resource1->size(), 0u); 126 EXPECT_GT(resource1->size(), 0u);
150 EXPECT_GT(resource2->size(), 0u); 127 EXPECT_GT(resource2->size(), 0u);
151 128
152 EXPECT_EQ(0u, memoryCache()->deadSize()); 129 EXPECT_EQ(0u, memoryCache()->size());
153 EXPECT_EQ(0u, memoryCache()->liveSize());
154 130
155 memoryCache()->add(resource1); 131 memoryCache()->add(resource1);
156 EXPECT_EQ(resource1->size(), memoryCache()->deadSize()); 132 memoryCache()->add(resource2);
157 EXPECT_EQ(0u, memoryCache()->liveSize());
158 133
159 memoryCache()->add(resource2); 134 size_t totalSize = resource1->size() + resource2->size();
160 EXPECT_EQ(resource1->size() + resource2->size(), memoryCache()->deadSize()); 135 EXPECT_EQ(totalSize, memoryCache()->size());
161 EXPECT_EQ(0u, memoryCache()->liveSize()); 136 EXPECT_GT(resource1->decodedSize(), 0u);
137 EXPECT_GT(resource2->decodedSize(), 0u);
162 138
139 // We expect actual pruning doesn't occur here synchronously but deferred
140 // to the end of this task, due to the previous pruning invoked in
141 // testResourcePruningAtEndOfTask().
163 memoryCache()->prune(); 142 memoryCache()->prune();
164 EXPECT_EQ(0u, memoryCache()->deadSize()); 143 EXPECT_EQ(totalSize, memoryCache()->size());
165 EXPECT_EQ(0u, memoryCache()->liveSize()); 144 EXPECT_GT(resource1->decodedSize(), 0u);
145 EXPECT_GT(resource2->decodedSize(), 0u);
166 } 146 }
167 147
168 TEST_F(MemoryCacheTest, DeadResourceEviction_Basic) { 148 static void runTask2(unsigned sizeWithoutDecode) {
169 Resource* resource1 = RawResource::create( 149 // Next task: now, the resources was pruned.
170 ResourceRequest("http://test/resource1"), Resource::Raw); 150 EXPECT_EQ(sizeWithoutDecode, memoryCache()->size());
171 Resource* resource2 = RawResource::create(
172 ResourceRequest("http://test/resource2"), Resource::Raw);
173 TestDeadResourceEviction(resource1, resource2);
174 } 151 }
175 152
176 TEST_F(MemoryCacheTest, DeadResourceEviction_MultipleResourceMaps) { 153 static void testResourcePruningAtEndOfTask(Resource* resource1,
177 Resource* resource1 = RawResource::create( 154 Resource* resource2) {
178 ResourceRequest("http://test/resource1"), Resource::Raw); 155 memoryCache()->setDelayBeforeLiveDecodedPrune(0);
179 Resource* resource2 = RawResource::create(
180 ResourceRequest("http://test/resource2"), Resource::Raw);
181 resource2->setCacheIdentifier("foo");
182 TestDeadResourceEviction(resource1, resource2);
183 }
184 156
185 static void runTask1(Resource* live, Resource* dead) { 157 // Enforce pruning by adding |dummyResource| and then call prune().
186 // The resource size has to be nonzero for this test to be meaningful, but 158 Resource* dummyResource =
187 // we do not rely on it having any particular value. 159 RawResource::create(ResourceRequest("http://dummy"), Resource::Raw);
188 EXPECT_GT(live->size(), 0u); 160 memoryCache()->add(dummyResource);
189 EXPECT_GT(dead->size(), 0u); 161 EXPECT_GT(memoryCache()->size(), 1u);
162 const unsigned totalCapacity = 1;
163 memoryCache()->setCapacity(totalCapacity);
164 memoryCache()->prune();
165 memoryCache()->remove(dummyResource);
166 EXPECT_EQ(0u, memoryCache()->size());
190 167
191 EXPECT_EQ(0u, memoryCache()->deadSize());
192 EXPECT_EQ(0u, memoryCache()->liveSize());
193
194 memoryCache()->add(dead);
195 memoryCache()->add(live);
196 memoryCache()->updateDecodedResource(live, UpdateForPropertyChange);
197 EXPECT_EQ(dead->size(), memoryCache()->deadSize());
198 EXPECT_EQ(live->size(), memoryCache()->liveSize());
199 EXPECT_GT(live->decodedSize(), 0u);
200
201 memoryCache()->prune(); // Dead resources are pruned immediately
202 EXPECT_EQ(dead->size(), memoryCache()->deadSize());
203 EXPECT_EQ(live->size(), memoryCache()->liveSize());
204 EXPECT_GT(live->decodedSize(), 0u);
205 }
206
207 static void runTask2(unsigned liveSizeWithoutDecode) {
208 // Next task: now, the live resource was evicted.
209 EXPECT_EQ(0u, memoryCache()->deadSize());
210 EXPECT_EQ(liveSizeWithoutDecode, memoryCache()->liveSize());
211 }
212
213 static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource,
214 Resource* cachedLiveResource) {
215 memoryCache()->setDelayBeforeLiveDecodedPrune(0);
216 const unsigned totalCapacity = 1;
217 const unsigned minDeadCapacity = 0;
218 const unsigned maxDeadCapacity = 0;
219 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);
220 const char data[6] = "abcde"; 168 const char data[6] = "abcde";
221 cachedDeadResource->appendData(data, 3u); 169 resource1->appendData(data, 3u);
222 cachedDeadResource->finish(); 170 resource1->finish();
223 Persistent<MockResourceClient> client = 171 Persistent<MockResourceClient> client = new MockResourceClient(resource2);
224 new MockResourceClient(cachedLiveResource); 172 resource2->appendData(data, 4u);
225 cachedLiveResource->appendData(data, 4u); 173 resource2->finish();
226 cachedLiveResource->finish();
227 174
228 Platform::current()->currentThread()->getWebTaskRunner()->postTask( 175 Platform::current()->currentThread()->getWebTaskRunner()->postTask(
229 BLINK_FROM_HERE, WTF::bind(&runTask1, wrapPersistent(cachedLiveResource), 176 BLINK_FROM_HERE, WTF::bind(&runTask1, wrapPersistent(resource1),
230 wrapPersistent(cachedDeadResource))); 177 wrapPersistent(resource2)));
231 Platform::current()->currentThread()->getWebTaskRunner()->postTask( 178 Platform::current()->currentThread()->getWebTaskRunner()->postTask(
232 BLINK_FROM_HERE, 179 BLINK_FROM_HERE,
233 WTF::bind(&runTask2, cachedLiveResource->encodedSize() + 180 WTF::bind(&runTask2,
234 cachedLiveResource->overheadSize())); 181 resource1->encodedSize() + resource1->overheadSize() +
182 resource2->encodedSize() + resource2->overheadSize()));
235 testing::runPendingTasks(); 183 testing::runPendingTasks();
236 } 184 }
237 185
238 // Verified that when ordering a prune in a runLoop task, the prune 186 // Verified that when ordering a prune in a runLoop task, the prune
239 // is deferred to the end of the task. 187 // is deferred to the end of the task.
240 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask_Basic) { 188 TEST_F(MemoryCacheTest, ResourcePruningAtEndOfTask_Basic) {
241 Resource* cachedDeadResource = 189 Resource* resource1 = FakeDecodedResource::create(
242 RawResource::create(ResourceRequest("hhtp://foo"), Resource::Raw); 190 ResourceRequest("http://test/resource1"), Resource::Raw);
243 Resource* cachedLiveResource = FakeDecodedResource::create( 191 Resource* resource2 = FakeDecodedResource::create(
244 ResourceRequest("http://test/resource"), Resource::Raw); 192 ResourceRequest("http://test/resource2"), Resource::Raw);
245 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResource); 193 testResourcePruningAtEndOfTask(resource1, resource2);
246 } 194 }
247 195
248 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask_MultipleResourceMaps) { 196 TEST_F(MemoryCacheTest, ResourcePruningAtEndOfTask_MultipleResourceMaps) {
249 { 197 {
250 Resource* cachedDeadResource = 198 Resource* resource1 = FakeDecodedResource::create(
251 RawResource::create(ResourceRequest("hhtp://foo"), Resource::Raw); 199 ResourceRequest("http://test/resource1"), Resource::Raw);
252 cachedDeadResource->setCacheIdentifier("foo"); 200 Resource* resource2 = FakeDecodedResource::create(
253 Resource* cachedLiveResource = FakeDecodedResource::create( 201 ResourceRequest("http://test/resource2"), Resource::Raw);
254 ResourceRequest("http://test/resource"), Resource::Raw); 202 resource1->setCacheIdentifier("foo");
255 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResource); 203 testResourcePruningAtEndOfTask(resource1, resource2);
256 memoryCache()->evictResources(); 204 memoryCache()->evictResources();
257 } 205 }
258 { 206 {
259 Resource* cachedDeadResource = 207 Resource* resource1 = FakeDecodedResource::create(
260 RawResource::create(ResourceRequest("hhtp://foo"), Resource::Raw); 208 ResourceRequest("http://test/resource1"), Resource::Raw);
261 Resource* cachedLiveResource = FakeDecodedResource::create( 209 Resource* resource2 = FakeDecodedResource::create(
262 ResourceRequest("http://test/resource"), Resource::Raw); 210 ResourceRequest("http://test/resource2"), Resource::Raw);
263 cachedLiveResource->setCacheIdentifier("foo"); 211 resource1->setCacheIdentifier("foo");
264 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResource); 212 resource2->setCacheIdentifier("bar");
265 memoryCache()->evictResources(); 213 testResourcePruningAtEndOfTask(resource1, resource2);
266 }
267 {
268 Resource* cachedDeadResource = RawResource::create(
269 ResourceRequest("hhtp://test/resource"), Resource::Raw);
270 cachedDeadResource->setCacheIdentifier("foo");
271 Resource* cachedLiveResource = FakeDecodedResource::create(
272 ResourceRequest("http://test/resource"), Resource::Raw);
273 cachedLiveResource->setCacheIdentifier("bar");
274 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResource);
275 memoryCache()->evictResources(); 214 memoryCache()->evictResources();
276 } 215 }
277 } 216 }
278 217
279 // Verifies that cached resources are evicted immediately after release when 218 // Verifies that
280 // the total dead resource size is more than double the dead resource capacity. 219 // - Resources are not pruned synchronously when ResourceClient is removed.
281 static void TestClientRemoval(Resource* resource1, Resource* resource2) { 220 // - size() is updated appropriately when Resources are added to MemoryCache
221 // and garbage collected.
222 static void testClientRemoval(Resource* resource1, Resource* resource2) {
282 const char data[6] = "abcde"; 223 const char data[6] = "abcde";
283 Persistent<MockResourceClient> client1 = new MockResourceClient(resource1); 224 Persistent<MockResourceClient> client1 = new MockResourceClient(resource1);
284 resource1->appendData(data, 4u); 225 resource1->appendData(data, 4u);
285 Persistent<MockResourceClient> client2 = new MockResourceClient(resource2); 226 Persistent<MockResourceClient> client2 = new MockResourceClient(resource2);
286 resource2->appendData(data, 4u); 227 resource2->appendData(data, 4u);
287 228
288 const unsigned minDeadCapacity = 0; 229 memoryCache()->setCapacity(0);
289 const unsigned maxDeadCapacity =
290 ((resource1->size() + resource2->size()) / 2) - 1;
291 const unsigned totalCapacity = maxDeadCapacity;
292 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);
293 memoryCache()->add(resource1); 230 memoryCache()->add(resource1);
294 memoryCache()->add(resource2); 231 memoryCache()->add(resource2);
232
233 size_t originalTotalSize = resource1->size() + resource2->size();
234
295 // Call prune. There is nothing to prune, but this will initialize 235 // Call prune. There is nothing to prune, but this will initialize
296 // the prune timestamp, allowing future prunes to be deferred. 236 // the prune timestamp, allowing future prunes to be deferred.
297 memoryCache()->prune(); 237 memoryCache()->prune();
298 EXPECT_GT(resource1->decodedSize(), 0u); 238 EXPECT_GT(resource1->decodedSize(), 0u);
299 EXPECT_GT(resource2->decodedSize(), 0u); 239 EXPECT_GT(resource2->decodedSize(), 0u);
300 EXPECT_EQ(0u, memoryCache()->deadSize()); 240 EXPECT_EQ(originalTotalSize, memoryCache()->size());
301 EXPECT_EQ(resource1->size() + resource2->size(), memoryCache()->liveSize());
302 241
303 // Removing the client from resource1 should not trigger pruning. 242 // Removing the client from resource1 should not trigger pruning.
304 client1->removeAsClient(); 243 client1->removeAsClient();
305 EXPECT_GT(resource1->decodedSize(), 0u); 244 EXPECT_GT(resource1->decodedSize(), 0u);
306 EXPECT_GT(resource2->decodedSize(), 0u); 245 EXPECT_GT(resource2->decodedSize(), 0u);
307 EXPECT_EQ(resource1->size(), memoryCache()->deadSize()); 246 EXPECT_EQ(originalTotalSize, memoryCache()->size());
308 EXPECT_EQ(resource2->size(), memoryCache()->liveSize());
309 EXPECT_TRUE(memoryCache()->contains(resource1)); 247 EXPECT_TRUE(memoryCache()->contains(resource1));
310 EXPECT_TRUE(memoryCache()->contains(resource2)); 248 EXPECT_TRUE(memoryCache()->contains(resource2));
311 249
312 // Removing the client from resource2 should not trigger pruning. 250 // Removing the client from resource2 should not trigger pruning.
313 client2->removeAsClient(); 251 client2->removeAsClient();
314 EXPECT_GT(resource1->decodedSize(), 0u); 252 EXPECT_GT(resource1->decodedSize(), 0u);
315 EXPECT_GT(resource2->decodedSize(), 0u); 253 EXPECT_GT(resource2->decodedSize(), 0u);
316 EXPECT_EQ(resource1->size() + resource2->size(), memoryCache()->deadSize()); 254 EXPECT_EQ(originalTotalSize, memoryCache()->size());
317 EXPECT_EQ(0u, memoryCache()->liveSize());
318 EXPECT_TRUE(memoryCache()->contains(resource1)); 255 EXPECT_TRUE(memoryCache()->contains(resource1));
319 EXPECT_TRUE(memoryCache()->contains(resource2)); 256 EXPECT_TRUE(memoryCache()->contains(resource2));
320 257
321 WeakPersistent<Resource> resource1Weak = resource1; 258 WeakPersistent<Resource> resource1Weak = resource1;
322 WeakPersistent<Resource> resource2Weak = resource2; 259 WeakPersistent<Resource> resource2Weak = resource2;
323 260
324 ThreadState::current()->collectGarbage( 261 ThreadState::current()->collectGarbage(
325 BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSweep, BlinkGC::ForcedGC); 262 BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSweep, BlinkGC::ForcedGC);
326 // Resources are garbage-collected (WeakMemoryCache) and thus removed 263 // Resources are garbage-collected (WeakMemoryCache) and thus removed
327 // from MemoryCache. 264 // from MemoryCache.
328 EXPECT_FALSE(resource1Weak); 265 EXPECT_FALSE(resource1Weak);
329 EXPECT_FALSE(resource2Weak); 266 EXPECT_FALSE(resource2Weak);
330 EXPECT_EQ(0u, memoryCache()->deadSize()); 267 EXPECT_EQ(0u, memoryCache()->size());
331 EXPECT_EQ(0u, memoryCache()->liveSize());
332 } 268 }
333 269
334 TEST_F(MemoryCacheTest, ClientRemoval_Basic) { 270 TEST_F(MemoryCacheTest, ClientRemoval_Basic) {
335 Resource* resource1 = FakeDecodedResource::create( 271 Resource* resource1 = FakeDecodedResource::create(
336 ResourceRequest("http://foo.com"), Resource::Raw); 272 ResourceRequest("http://foo.com"), Resource::Raw);
337 Resource* resource2 = FakeDecodedResource::create( 273 Resource* resource2 = FakeDecodedResource::create(
338 ResourceRequest("http://test/resource"), Resource::Raw); 274 ResourceRequest("http://test/resource"), Resource::Raw);
339 TestClientRemoval(resource1, resource2); 275 testClientRemoval(resource1, resource2);
340 } 276 }
341 277
342 TEST_F(MemoryCacheTest, ClientRemoval_MultipleResourceMaps) { 278 TEST_F(MemoryCacheTest, ClientRemoval_MultipleResourceMaps) {
343 { 279 {
344 Resource* resource1 = FakeDecodedResource::create( 280 Resource* resource1 = FakeDecodedResource::create(
345 ResourceRequest("http://foo.com"), Resource::Raw); 281 ResourceRequest("http://foo.com"), Resource::Raw);
346 resource1->setCacheIdentifier("foo"); 282 resource1->setCacheIdentifier("foo");
347 Resource* resource2 = FakeDecodedResource::create( 283 Resource* resource2 = FakeDecodedResource::create(
348 ResourceRequest("http://test/resource"), Resource::Raw); 284 ResourceRequest("http://test/resource"), Resource::Raw);
349 TestClientRemoval(resource1, resource2); 285 testClientRemoval(resource1, resource2);
350 memoryCache()->evictResources(); 286 memoryCache()->evictResources();
351 } 287 }
352 { 288 {
353 Resource* resource1 = FakeDecodedResource::create( 289 Resource* resource1 = FakeDecodedResource::create(
354 ResourceRequest("http://foo.com"), Resource::Raw); 290 ResourceRequest("http://foo.com"), Resource::Raw);
355 Resource* resource2 = FakeDecodedResource::create( 291 Resource* resource2 = FakeDecodedResource::create(
356 ResourceRequest("http://test/resource"), Resource::Raw); 292 ResourceRequest("http://test/resource"), Resource::Raw);
357 resource2->setCacheIdentifier("foo"); 293 resource2->setCacheIdentifier("foo");
358 TestClientRemoval(resource1, resource2); 294 testClientRemoval(resource1, resource2);
359 memoryCache()->evictResources(); 295 memoryCache()->evictResources();
360 } 296 }
361 { 297 {
362 Resource* resource1 = FakeDecodedResource::create( 298 Resource* resource1 = FakeDecodedResource::create(
363 ResourceRequest("http://test/resource"), Resource::Raw); 299 ResourceRequest("http://test/resource"), Resource::Raw);
364 resource1->setCacheIdentifier("foo"); 300 resource1->setCacheIdentifier("foo");
365 Resource* resource2 = FakeDecodedResource::create( 301 Resource* resource2 = FakeDecodedResource::create(
366 ResourceRequest("http://test/resource"), Resource::Raw); 302 ResourceRequest("http://test/resource"), Resource::Raw);
367 resource2->setCacheIdentifier("bar"); 303 resource2->setCacheIdentifier("bar");
368 TestClientRemoval(resource1, resource2); 304 testClientRemoval(resource1, resource2);
369 memoryCache()->evictResources(); 305 memoryCache()->evictResources();
370 } 306 }
371 } 307 }
372 308
373 TEST_F(MemoryCacheTest, RemoveDuringRevalidation) { 309 TEST_F(MemoryCacheTest, RemoveDuringRevalidation) {
374 FakeResource* resource1 = FakeResource::create( 310 FakeResource* resource1 = FakeResource::create(
375 ResourceRequest("http://test/resource"), Resource::Raw); 311 ResourceRequest("http://test/resource"), Resource::Raw);
376 memoryCache()->add(resource1); 312 memoryCache()->add(resource1);
377 313
378 FakeResource* resource2 = FakeResource::create( 314 FakeResource* resource2 = FakeResource::create(
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 FakeResource::create(ResourceRequest(url1), Resource::Raw); 368 FakeResource::create(ResourceRequest(url1), Resource::Raw);
433 memoryCache()->add(resource); 369 memoryCache()->add(resource);
434 EXPECT_TRUE(memoryCache()->contains(resource)); 370 EXPECT_TRUE(memoryCache()->contains(resource));
435 371
436 EXPECT_EQ(resource, memoryCache()->resourceForURL(url1)); 372 EXPECT_EQ(resource, memoryCache()->resourceForURL(url1));
437 373
438 const KURL url2 = MemoryCache::removeFragmentIdentifierIfNeeded(url1); 374 const KURL url2 = MemoryCache::removeFragmentIdentifierIfNeeded(url1);
439 EXPECT_EQ(resource, memoryCache()->resourceForURL(url2)); 375 EXPECT_EQ(resource, memoryCache()->resourceForURL(url2));
440 } 376 }
441 377
442 TEST_F(MemoryCacheTest, MakeLiveAndDead) {
443 FakeResource* resource = FakeResource::create(
444 ResourceRequest("http://test/resource"), Resource::Raw);
445 const char data[6] = "abcde";
446 resource->appendData(data, 5u);
447 memoryCache()->add(resource);
448
449 const size_t deadSize = memoryCache()->deadSize();
450 const size_t liveSize = memoryCache()->liveSize();
451
452 memoryCache()->makeLive(resource);
453 EXPECT_EQ(deadSize, memoryCache()->deadSize() + resource->size());
454 EXPECT_EQ(liveSize, memoryCache()->liveSize() - resource->size());
455
456 memoryCache()->makeDead(resource);
457 EXPECT_EQ(deadSize, memoryCache()->deadSize());
458 EXPECT_EQ(liveSize, memoryCache()->liveSize());
459 }
460
461 TEST_F(MemoryCacheTest, RemoveURLFromCache) { 378 TEST_F(MemoryCacheTest, RemoveURLFromCache) {
462 const KURL url1 = KURL(ParsedURLString, "http://test/resource1"); 379 const KURL url1 = KURL(ParsedURLString, "http://test/resource1");
463 FakeResource* resource1 = 380 Persistent<FakeResource> resource1 =
464 FakeResource::create(ResourceRequest(url1), Resource::Raw); 381 FakeResource::create(ResourceRequest(url1), Resource::Raw);
465 memoryCache()->add(resource1); 382 memoryCache()->add(resource1);
466 EXPECT_TRUE(memoryCache()->contains(resource1)); 383 EXPECT_TRUE(memoryCache()->contains(resource1));
467 384
468 memoryCache()->removeURLFromCache(url1); 385 memoryCache()->removeURLFromCache(url1);
469 EXPECT_FALSE(memoryCache()->contains(resource1)); 386 EXPECT_FALSE(memoryCache()->contains(resource1));
470 387
471 const KURL url2 = KURL(ParsedURLString, "http://test/resource2#foo"); 388 const KURL url2 = KURL(ParsedURLString, "http://test/resource2#foo");
472 FakeResource* resource2 = 389 FakeResource* resource2 =
473 FakeResource::create(ResourceRequest(url2), Resource::Raw); 390 FakeResource::create(ResourceRequest(url2), Resource::Raw);
474 memoryCache()->add(resource2); 391 memoryCache()->add(resource2);
475 EXPECT_TRUE(memoryCache()->contains(resource2)); 392 EXPECT_TRUE(memoryCache()->contains(resource2));
476 393
477 memoryCache()->removeURLFromCache(url2); 394 memoryCache()->removeURLFromCache(url2);
478 EXPECT_FALSE(memoryCache()->contains(resource2)); 395 EXPECT_FALSE(memoryCache()->contains(resource2));
479 } 396 }
480 397
481 } // namespace blink 398 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/fetch/MemoryCache.cpp ('k') | third_party/WebKit/Source/core/fetch/Resource.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698