| 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 12 matching lines...) Expand all Loading... |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "core/fetch/MemoryCache.h" | 31 #include "core/fetch/MemoryCache.h" |
| 32 | 32 |
| 33 #include "core/fetch/MockImageResourceClient.h" | 33 #include "core/fetch/MockResourceClients.h" |
| 34 #include "core/fetch/RawResource.h" | 34 #include "core/fetch/RawResource.h" |
| 35 #include "platform/network/ResourceRequest.h" | 35 #include "platform/network/ResourceRequest.h" |
| 36 #include "platform/testing/UnitTestHelpers.h" | 36 #include "platform/testing/UnitTestHelpers.h" |
| 37 #include "public/platform/Platform.h" | 37 #include "public/platform/Platform.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 39 #include "wtf/OwnPtr.h" | 39 #include "wtf/OwnPtr.h" |
| 40 | 40 |
| 41 namespace blink { | 41 namespace blink { |
| 42 | 42 |
| 43 class MemoryCacheTest : public ::testing::Test { | 43 class MemoryCacheTest : public ::testing::Test { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 RefPtrWillBeRawPtr<FakeResource> cachedResource = | 126 RefPtrWillBeRawPtr<FakeResource> cachedResource = |
| 127 FakeResource::create(ResourceRequest("http://test/resource"), Resource::
Raw); | 127 FakeResource::create(ResourceRequest("http://test/resource"), Resource::
Raw); |
| 128 cachedResource->fakeEncodedSize(resourceSize1); | 128 cachedResource->fakeEncodedSize(resourceSize1); |
| 129 | 129 |
| 130 ASSERT_EQ(0u, memoryCache()->deadSize()); | 130 ASSERT_EQ(0u, memoryCache()->deadSize()); |
| 131 ASSERT_EQ(0u, memoryCache()->liveSize()); | 131 ASSERT_EQ(0u, memoryCache()->liveSize()); |
| 132 memoryCache()->add(cachedResource.get()); | 132 memoryCache()->add(cachedResource.get()); |
| 133 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); | 133 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); |
| 134 ASSERT_EQ(0u, memoryCache()->liveSize()); | 134 ASSERT_EQ(0u, memoryCache()->liveSize()); |
| 135 | 135 |
| 136 MockImageResourceClient client(cachedResource); | 136 MockResourceClient client(cachedResource); |
| 137 ASSERT_EQ(0u, memoryCache()->deadSize()); | 137 ASSERT_EQ(0u, memoryCache()->deadSize()); |
| 138 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); | 138 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); |
| 139 | 139 |
| 140 cachedResource->fakeEncodedSize(resourceSize2); | 140 cachedResource->fakeEncodedSize(resourceSize2); |
| 141 ASSERT_EQ(0u, memoryCache()->deadSize()); | 141 ASSERT_EQ(0u, memoryCache()->deadSize()); |
| 142 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); | 142 ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize()); |
| 143 } | 143 } |
| 144 | 144 |
| 145 // Verifies that dead resources that exceed dead resource capacity are evicted | 145 // Verifies that dead resources that exceed dead resource capacity are evicted |
| 146 // from cache when pruning. | 146 // from cache when pruning. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 | 229 |
| 230 static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource, Re
source* cachedLiveResource) | 230 static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource, Re
source* cachedLiveResource) |
| 231 { | 231 { |
| 232 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 232 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
| 233 const unsigned totalCapacity = 1; | 233 const unsigned totalCapacity = 1; |
| 234 const unsigned minDeadCapacity = 0; | 234 const unsigned minDeadCapacity = 0; |
| 235 const unsigned maxDeadCapacity = 0; | 235 const unsigned maxDeadCapacity = 0; |
| 236 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 236 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
| 237 const char data[6] = "abcde"; | 237 const char data[6] = "abcde"; |
| 238 cachedDeadResource->appendData(data, 3u); | 238 cachedDeadResource->appendData(data, 3u); |
| 239 MockImageResourceClient client(cachedLiveResource); | 239 MockResourceClient client(cachedLiveResource); |
| 240 cachedLiveResource->appendData(data, 4u); | 240 cachedLiveResource->appendData(data, 4u); |
| 241 | 241 |
| 242 Platform::current()->currentThread()->getWebTaskRunner()->postTask(BLINK_FRO
M_HERE, bind(&runTask1, PassRefPtrWillBeRawPtr<Resource>(cachedLiveResource), Pa
ssRefPtrWillBeRawPtr<Resource>(cachedDeadResource))); | 242 Platform::current()->currentThread()->getWebTaskRunner()->postTask(BLINK_FRO
M_HERE, bind(&runTask1, PassRefPtrWillBeRawPtr<Resource>(cachedLiveResource), Pa
ssRefPtrWillBeRawPtr<Resource>(cachedDeadResource))); |
| 243 Platform::current()->currentThread()->getWebTaskRunner()->postTask(BLINK_FRO
M_HERE, bind(&runTask2, cachedLiveResource->encodedSize() + cachedLiveResource->
overheadSize())); | 243 Platform::current()->currentThread()->getWebTaskRunner()->postTask(BLINK_FRO
M_HERE, bind(&runTask2, cachedLiveResource->encodedSize() + cachedLiveResource->
overheadSize())); |
| 244 testing::runPendingTasks(); | 244 testing::runPendingTasks(); |
| 245 } | 245 } |
| 246 | 246 |
| 247 // Verified that when ordering a prune in a runLoop task, the prune | 247 // Verified that when ordering a prune in a runLoop task, the prune |
| 248 // is deferred to the end of the task. | 248 // is deferred to the end of the task. |
| 249 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask_Basic) | 249 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask_Basic) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource.get(), cachedLive
Resource.get()); | 285 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource.get(), cachedLive
Resource.get()); |
| 286 memoryCache()->evictResources(); | 286 memoryCache()->evictResources(); |
| 287 } | 287 } |
| 288 } | 288 } |
| 289 | 289 |
| 290 // Verifies that cached resources are evicted immediately after release when | 290 // Verifies that cached resources are evicted immediately after release when |
| 291 // the total dead resource size is more than double the dead resource capacity. | 291 // the total dead resource size is more than double the dead resource capacity. |
| 292 static void TestClientRemoval(Resource* resource1, Resource* resource2) | 292 static void TestClientRemoval(Resource* resource1, Resource* resource2) |
| 293 { | 293 { |
| 294 const char data[6] = "abcde"; | 294 const char data[6] = "abcde"; |
| 295 MockImageResourceClient client1(resource1); | 295 MockResourceClient client1(resource1); |
| 296 resource1->appendData(data, 4u); | 296 resource1->appendData(data, 4u); |
| 297 MockImageResourceClient client2(resource2); | 297 MockResourceClient client2(resource2); |
| 298 resource2->appendData(data, 4u); | 298 resource2->appendData(data, 4u); |
| 299 | 299 |
| 300 const unsigned minDeadCapacity = 0; | 300 const unsigned minDeadCapacity = 0; |
| 301 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; | 301 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; |
| 302 const unsigned totalCapacity = maxDeadCapacity; | 302 const unsigned totalCapacity = maxDeadCapacity; |
| 303 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 303 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
| 304 memoryCache()->add(resource1); | 304 memoryCache()->add(resource1); |
| 305 memoryCache()->add(resource2); | 305 memoryCache()->add(resource2); |
| 306 // Call prune. There is nothing to prune, but this will initialize | 306 // Call prune. There is nothing to prune, but this will initialize |
| 307 // the prune timestamp, allowing future prunes to be deferred. | 307 // the prune timestamp, allowing future prunes to be deferred. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 | 418 |
| 419 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re
sourcesForURL(url); | 419 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re
sourcesForURL(url); |
| 420 EXPECT_EQ(2u, resources.size()); | 420 EXPECT_EQ(2u, resources.size()); |
| 421 | 421 |
| 422 memoryCache()->evictResources(); | 422 memoryCache()->evictResources(); |
| 423 EXPECT_FALSE(memoryCache()->contains(resource1.get())); | 423 EXPECT_FALSE(memoryCache()->contains(resource1.get())); |
| 424 EXPECT_FALSE(memoryCache()->contains(resource3.get())); | 424 EXPECT_FALSE(memoryCache()->contains(resource3.get())); |
| 425 } | 425 } |
| 426 | 426 |
| 427 } // namespace blink | 427 } // namespace blink |
| OLD | NEW |