OLD | NEW |
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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 ResourcePtr<FakeResource> cachedResource = | 118 ResourcePtr<FakeResource> cachedResource = |
119 new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw)
; | 119 new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw)
; |
120 cachedResource->fakeEncodedSize(resourceSize1); | 120 cachedResource->fakeEncodedSize(resourceSize1); |
121 | 121 |
122 ASSERT_EQ(0u, memoryCache()->deadSize()); | 122 ASSERT_EQ(0u, memoryCache()->deadSize()); |
123 ASSERT_EQ(0u, memoryCache()->liveSize()); | 123 ASSERT_EQ(0u, memoryCache()->liveSize()); |
124 memoryCache()->add(cachedResource.get()); | 124 memoryCache()->add(cachedResource.get()); |
125 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); | 125 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); |
126 ASSERT_EQ(0u, memoryCache()->liveSize()); | 126 ASSERT_EQ(0u, memoryCache()->liveSize()); |
127 | 127 |
128 MockImageResourceClient client; | 128 MockImageResourceClient client(cachedResource); |
129 cachedResource->addClient(&client); | |
130 ASSERT_EQ(0u, memoryCache()->deadSize()); | 129 ASSERT_EQ(0u, memoryCache()->deadSize()); |
131 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); | 130 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); |
132 | 131 |
133 cachedResource->fakeEncodedSize(resourceSize2); | 132 cachedResource->fakeEncodedSize(resourceSize2); |
134 ASSERT_EQ(0u, memoryCache()->deadSize()); | 133 ASSERT_EQ(0u, memoryCache()->deadSize()); |
135 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); | 134 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); |
136 | |
137 cachedResource->removeClient(&client); | |
138 } | 135 } |
139 | 136 |
140 // Verifies that dead resources that exceed dead resource capacity are evicted | 137 // Verifies that dead resources that exceed dead resource capacity are evicted |
141 // from cache when pruning. | 138 // from cache when pruning. |
142 static void TestDeadResourceEviction(Resource* resource1, Resource* resource2) | 139 static void TestDeadResourceEviction(Resource* resource1, Resource* resource2) |
143 { | 140 { |
144 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 141 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
145 memoryCache()->setMaxPruneDeferralDelay(0); | 142 memoryCache()->setMaxPruneDeferralDelay(0); |
146 const unsigned totalCapacity = 1000000; | 143 const unsigned totalCapacity = 1000000; |
147 const unsigned minDeadCapacity = 0; | 144 const unsigned minDeadCapacity = 0; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 191 |
195 static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource, co
nst ResourcePtr<Resource>& cachedLiveResource) | 192 static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource, co
nst ResourcePtr<Resource>& cachedLiveResource) |
196 { | 193 { |
197 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 194 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
198 const unsigned totalCapacity = 1; | 195 const unsigned totalCapacity = 1; |
199 const unsigned minDeadCapacity = 0; | 196 const unsigned minDeadCapacity = 0; |
200 const unsigned maxDeadCapacity = 0; | 197 const unsigned maxDeadCapacity = 0; |
201 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 198 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
202 const char data[6] = "abcde"; | 199 const char data[6] = "abcde"; |
203 cachedDeadResource->appendData(data, 3u); | 200 cachedDeadResource->appendData(data, 3u); |
204 MockImageResourceClient client; | 201 MockImageResourceClient client(cachedLiveResource); |
205 cachedLiveResource->addClient(&client); | |
206 cachedLiveResource->appendData(data, 4u); | 202 cachedLiveResource->appendData(data, 4u); |
207 | 203 |
208 class Task1 : public WebThread::Task { | 204 class Task1 : public WebThread::Task { |
209 public: | 205 public: |
210 Task1(const ResourcePtr<Resource>& live, Resource* dead) | 206 Task1(const ResourcePtr<Resource>& live, Resource* dead) |
211 : m_live(live) | 207 : m_live(live) |
212 , m_dead(dead) | 208 , m_dead(dead) |
213 { } | 209 { } |
214 | 210 |
215 void run() override | 211 void run() override |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 } | 249 } |
254 | 250 |
255 private: | 251 private: |
256 unsigned m_liveSizeWithoutDecode; | 252 unsigned m_liveSizeWithoutDecode; |
257 }; | 253 }; |
258 | 254 |
259 | 255 |
260 Platform::current()->currentThread()->postTask(FROM_HERE, new Task1(cachedLi
veResource, cachedDeadResource)); | 256 Platform::current()->currentThread()->postTask(FROM_HERE, new Task1(cachedLi
veResource, cachedDeadResource)); |
261 Platform::current()->currentThread()->postTask(FROM_HERE, new Task2(cachedLi
veResource->encodedSize() + cachedLiveResource->overheadSize())); | 257 Platform::current()->currentThread()->postTask(FROM_HERE, new Task2(cachedLi
veResource->encodedSize() + cachedLiveResource->overheadSize())); |
262 testing::runPendingTasks(); | 258 testing::runPendingTasks(); |
263 cachedLiveResource->removeClient(&client); | |
264 } | 259 } |
265 | 260 |
266 // Verified that when ordering a prune in a runLoop task, the prune | 261 // Verified that when ordering a prune in a runLoop task, the prune |
267 // is deferred to the end of the task. | 262 // is deferred to the end of the task. |
268 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask_Basic) | 263 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask_Basic) |
269 { | 264 { |
270 Resource* cachedDeadResource = | 265 Resource* cachedDeadResource = |
271 new Resource(ResourceRequest("hhtp://foo"), Resource::Raw); | 266 new Resource(ResourceRequest("hhtp://foo"), Resource::Raw); |
272 ResourcePtr<Resource> cachedLiveResource = | 267 ResourcePtr<Resource> cachedLiveResource = |
273 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); | 268 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); |
(...skipping 30 matching lines...) Expand all Loading... |
304 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResour
ce); | 299 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResour
ce); |
305 memoryCache()->evictResources(); | 300 memoryCache()->evictResources(); |
306 } | 301 } |
307 } | 302 } |
308 | 303 |
309 // Verifies that cached resources are evicted immediately after release when | 304 // Verifies that cached resources are evicted immediately after release when |
310 // the total dead resource size is more than double the dead resource capacity. | 305 // the total dead resource size is more than double the dead resource capacity. |
311 static void TestClientRemoval(const ResourcePtr<Resource>& resource1, const Reso
urcePtr<Resource>& resource2) | 306 static void TestClientRemoval(const ResourcePtr<Resource>& resource1, const Reso
urcePtr<Resource>& resource2) |
312 { | 307 { |
313 const char data[6] = "abcde"; | 308 const char data[6] = "abcde"; |
314 MockImageResourceClient client1; | 309 MockImageResourceClient client1(resource1); |
315 resource1->addClient(&client1); | |
316 resource1->appendData(data, 4u); | 310 resource1->appendData(data, 4u); |
317 MockImageResourceClient client2; | 311 MockImageResourceClient client2(resource2); |
318 resource2->addClient(&client2); | |
319 resource2->appendData(data, 4u); | 312 resource2->appendData(data, 4u); |
320 | 313 |
321 const unsigned minDeadCapacity = 0; | 314 const unsigned minDeadCapacity = 0; |
322 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; | 315 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; |
323 const unsigned totalCapacity = maxDeadCapacity; | 316 const unsigned totalCapacity = maxDeadCapacity; |
324 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 317 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
325 memoryCache()->add(resource1.get()); | 318 memoryCache()->add(resource1.get()); |
326 memoryCache()->add(resource2.get()); | 319 memoryCache()->add(resource2.get()); |
327 // Call prune. There is nothing to prune, but this will initialize | 320 // Call prune. There is nothing to prune, but this will initialize |
328 // the prune timestamp, allowing future prunes to be deferred. | 321 // the prune timestamp, allowing future prunes to be deferred. |
329 memoryCache()->prune(); | 322 memoryCache()->prune(); |
330 ASSERT_GT(resource1->decodedSize(), 0u); | 323 ASSERT_GT(resource1->decodedSize(), 0u); |
331 ASSERT_GT(resource2->decodedSize(), 0u); | 324 ASSERT_GT(resource2->decodedSize(), 0u); |
332 ASSERT_EQ(memoryCache()->deadSize(), 0u); | 325 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
333 ASSERT_EQ(memoryCache()->liveSize(), resource1->size() + resource2->size()); | 326 ASSERT_EQ(memoryCache()->liveSize(), resource1->size() + resource2->size()); |
334 | 327 |
335 // Removing the client from resource1 should result in all resources | 328 // Removing the client from resource1 should result in all resources |
336 // remaining in cache since the prune is deferred. | 329 // remaining in cache since the prune is deferred. |
337 resource1->removeClient(&client1); | 330 client1.removeAsClient(); |
338 ASSERT_GT(resource1->decodedSize(), 0u); | 331 ASSERT_GT(resource1->decodedSize(), 0u); |
339 ASSERT_GT(resource2->decodedSize(), 0u); | 332 ASSERT_GT(resource2->decodedSize(), 0u); |
340 ASSERT_EQ(memoryCache()->deadSize(), resource1->size()); | 333 ASSERT_EQ(memoryCache()->deadSize(), resource1->size()); |
341 ASSERT_EQ(memoryCache()->liveSize(), resource2->size()); | 334 ASSERT_EQ(memoryCache()->liveSize(), resource2->size()); |
342 ASSERT_TRUE(memoryCache()->contains(resource1.get())); | 335 ASSERT_TRUE(memoryCache()->contains(resource1.get())); |
343 ASSERT_TRUE(memoryCache()->contains(resource2.get())); | 336 ASSERT_TRUE(memoryCache()->contains(resource2.get())); |
344 | 337 |
345 // Removing the client from resource2 should result in immediate | 338 // Removing the client from resource2 should result in immediate |
346 // eviction of resource2 because we are over the prune deferral limit. | 339 // eviction of resource2 because we are over the prune deferral limit. |
347 resource2->removeClient(&client2); | 340 client2.removeAsClient(); |
348 ASSERT_GT(resource1->decodedSize(), 0u); | 341 ASSERT_GT(resource1->decodedSize(), 0u); |
349 ASSERT_GT(resource2->decodedSize(), 0u); | 342 ASSERT_GT(resource2->decodedSize(), 0u); |
350 ASSERT_EQ(memoryCache()->deadSize(), resource1->size()); | 343 ASSERT_EQ(memoryCache()->deadSize(), resource1->size()); |
351 ASSERT_EQ(memoryCache()->liveSize(), 0u); | 344 ASSERT_EQ(memoryCache()->liveSize(), 0u); |
352 ASSERT_TRUE(memoryCache()->contains(resource1.get())); | 345 ASSERT_TRUE(memoryCache()->contains(resource1.get())); |
353 ASSERT_FALSE(memoryCache()->contains(resource2.get())); | 346 ASSERT_FALSE(memoryCache()->contains(resource2.get())); |
354 } | 347 } |
355 | 348 |
356 TEST_F(MemoryCacheTest, ClientRemoval_Basic) | 349 TEST_F(MemoryCacheTest, ClientRemoval_Basic) |
357 { | 350 { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 } | 387 } |
395 } | 388 } |
396 | 389 |
397 // Verifies that CachedResources are evicted from the decode cache | 390 // Verifies that CachedResources are evicted from the decode cache |
398 // according to their DecodeCachePriority. | 391 // according to their DecodeCachePriority. |
399 static void TestDecodeCacheOrder(const ResourcePtr<Resource>& cachedImageLowPrio
rity, const ResourcePtr<Resource>& cachedImageHighPriority) | 392 static void TestDecodeCacheOrder(const ResourcePtr<Resource>& cachedImageLowPrio
rity, const ResourcePtr<Resource>& cachedImageHighPriority) |
400 { | 393 { |
401 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 394 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
402 memoryCache()->setMaxPruneDeferralDelay(0); | 395 memoryCache()->setMaxPruneDeferralDelay(0); |
403 | 396 |
404 MockImageResourceClient clientLowPriority; | 397 MockImageResourceClient clientLowPriority(cachedImageLowPriority); |
405 MockImageResourceClient clientHighPriority; | 398 MockImageResourceClient clientHighPriority(cachedImageHighPriority); |
406 cachedImageLowPriority->addClient(&clientLowPriority); | |
407 cachedImageHighPriority->addClient(&clientHighPriority); | |
408 | 399 |
409 const char data[5] = "abcd"; | 400 const char data[5] = "abcd"; |
410 cachedImageLowPriority->appendData(data, 1u); | 401 cachedImageLowPriority->appendData(data, 1u); |
411 cachedImageHighPriority->appendData(data, 4u); | 402 cachedImageHighPriority->appendData(data, 4u); |
412 const unsigned lowPrioritySize = cachedImageLowPriority->size(); | 403 const unsigned lowPrioritySize = cachedImageLowPriority->size(); |
413 const unsigned highPrioritySize = cachedImageHighPriority->size(); | 404 const unsigned highPrioritySize = cachedImageHighPriority->size(); |
414 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi
ze(); | 405 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi
ze(); |
415 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded
Size(); | 406 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded
Size(); |
416 const unsigned totalSize = lowPrioritySize + highPrioritySize; | 407 const unsigned totalSize = lowPrioritySize + highPrioritySize; |
417 | 408 |
(...skipping 29 matching lines...) Expand all Loading... |
447 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()
->liveSize() - 10, memoryCache()->liveSize() - 10); | 438 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()
->liveSize() - 10, memoryCache()->liveSize() - 10); |
448 memoryCache()->prune(); | 439 memoryCache()->prune(); |
449 ASSERT_EQ(memoryCache()->deadSize(), 0u); | 440 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
450 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize); | 441 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize); |
451 | 442 |
452 // Should prune the HighPriority item. | 443 // Should prune the HighPriority item. |
453 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()
->liveSize() - 10, memoryCache()->liveSize() - 10); | 444 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()
->liveSize() - 10, memoryCache()->liveSize() - 10); |
454 memoryCache()->prune(); | 445 memoryCache()->prune(); |
455 ASSERT_EQ(memoryCache()->deadSize(), 0u); | 446 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
456 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize -
highPriorityMockDecodeSize); | 447 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize -
highPriorityMockDecodeSize); |
457 | |
458 cachedImageLowPriority->removeClient(&clientLowPriority); | |
459 cachedImageHighPriority->removeClient(&clientHighPriority); | |
460 } | 448 } |
461 | 449 |
462 TEST_F(MemoryCacheTest, DecodeCacheOrder_Basic) | 450 TEST_F(MemoryCacheTest, DecodeCacheOrder_Basic) |
463 { | 451 { |
464 ResourcePtr<FakeDecodedResource> cachedImageLowPriority = | 452 ResourcePtr<FakeDecodedResource> cachedImageLowPriority = |
465 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); | 453 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); |
466 ResourcePtr<FakeDecodedResource> cachedImageHighPriority = | 454 ResourcePtr<FakeDecodedResource> cachedImageHighPriority = |
467 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); | 455 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); |
468 TestDecodeCacheOrder(cachedImageLowPriority, cachedImageHighPriority); | 456 TestDecodeCacheOrder(cachedImageLowPriority, cachedImageHighPriority); |
469 } | 457 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 | 561 |
574 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re
sourcesForURL(url); | 562 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re
sourcesForURL(url); |
575 EXPECT_EQ(2u, resources.size()); | 563 EXPECT_EQ(2u, resources.size()); |
576 | 564 |
577 memoryCache()->evictResources(); | 565 memoryCache()->evictResources(); |
578 EXPECT_FALSE(memoryCache()->contains(resource1.get())); | 566 EXPECT_FALSE(memoryCache()->contains(resource1.get())); |
579 EXPECT_FALSE(memoryCache()->contains(resource3.get())); | 567 EXPECT_FALSE(memoryCache()->contains(resource3.get())); |
580 } | 568 } |
581 | 569 |
582 } // namespace | 570 } // namespace |
OLD | NEW |