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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 cachedDeadResource->finish(); | 239 cachedDeadResource->finish(); |
240 MockImageResourceClient client(cachedLiveResource); | 240 MockResourceClient client(cachedLiveResource); |
241 cachedLiveResource->appendData(data, 4u); | 241 cachedLiveResource->appendData(data, 4u); |
242 cachedLiveResource->finish(); | 242 cachedLiveResource->finish(); |
243 | 243 |
244 Platform::current()->currentThread()->getWebTaskRunner()->postTask(BLINK_FRO
M_HERE, bind(&runTask1, PassRefPtrWillBeRawPtr<Resource>(cachedLiveResource), Pa
ssRefPtrWillBeRawPtr<Resource>(cachedDeadResource))); | 244 Platform::current()->currentThread()->getWebTaskRunner()->postTask(BLINK_FRO
M_HERE, bind(&runTask1, PassRefPtrWillBeRawPtr<Resource>(cachedLiveResource), Pa
ssRefPtrWillBeRawPtr<Resource>(cachedDeadResource))); |
245 Platform::current()->currentThread()->getWebTaskRunner()->postTask(BLINK_FRO
M_HERE, bind(&runTask2, cachedLiveResource->encodedSize() + cachedLiveResource->
overheadSize())); | 245 Platform::current()->currentThread()->getWebTaskRunner()->postTask(BLINK_FRO
M_HERE, bind(&runTask2, cachedLiveResource->encodedSize() + cachedLiveResource->
overheadSize())); |
246 testing::runPendingTasks(); | 246 testing::runPendingTasks(); |
247 } | 247 } |
248 | 248 |
249 // Verified that when ordering a prune in a runLoop task, the prune | 249 // Verified that when ordering a prune in a runLoop task, the prune |
250 // is deferred to the end of the task. | 250 // is deferred to the end of the task. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource.get(), cachedLive
Resource.get()); | 287 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource.get(), cachedLive
Resource.get()); |
288 memoryCache()->evictResources(); | 288 memoryCache()->evictResources(); |
289 } | 289 } |
290 } | 290 } |
291 | 291 |
292 // Verifies that cached resources are evicted immediately after release when | 292 // Verifies that cached resources are evicted immediately after release when |
293 // the total dead resource size is more than double the dead resource capacity. | 293 // the total dead resource size is more than double the dead resource capacity. |
294 static void TestClientRemoval(Resource* resource1, Resource* resource2) | 294 static void TestClientRemoval(Resource* resource1, Resource* resource2) |
295 { | 295 { |
296 const char data[6] = "abcde"; | 296 const char data[6] = "abcde"; |
297 MockImageResourceClient client1(resource1); | 297 MockResourceClient client1(resource1); |
298 resource1->appendData(data, 4u); | 298 resource1->appendData(data, 4u); |
299 MockImageResourceClient client2(resource2); | 299 MockResourceClient client2(resource2); |
300 resource2->appendData(data, 4u); | 300 resource2->appendData(data, 4u); |
301 | 301 |
302 const unsigned minDeadCapacity = 0; | 302 const unsigned minDeadCapacity = 0; |
303 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; | 303 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; |
304 const unsigned totalCapacity = maxDeadCapacity; | 304 const unsigned totalCapacity = maxDeadCapacity; |
305 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 305 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
306 memoryCache()->add(resource1); | 306 memoryCache()->add(resource1); |
307 memoryCache()->add(resource2); | 307 memoryCache()->add(resource2); |
308 // Call prune. There is nothing to prune, but this will initialize | 308 // Call prune. There is nothing to prune, but this will initialize |
309 // the prune timestamp, allowing future prunes to be deferred. | 309 // the prune timestamp, allowing future prunes to be deferred. |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 | 420 |
421 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re
sourcesForURL(url); | 421 WillBeHeapVector<RawPtrWillBeMember<Resource>> resources = memoryCache()->re
sourcesForURL(url); |
422 EXPECT_EQ(2u, resources.size()); | 422 EXPECT_EQ(2u, resources.size()); |
423 | 423 |
424 memoryCache()->evictResources(); | 424 memoryCache()->evictResources(); |
425 EXPECT_FALSE(memoryCache()->contains(resource1.get())); | 425 EXPECT_FALSE(memoryCache()->contains(resource1.get())); |
426 EXPECT_FALSE(memoryCache()->contains(resource3.get())); | 426 EXPECT_FALSE(memoryCache()->contains(resource3.get())); |
427 } | 427 } |
428 | 428 |
429 } // namespace blink | 429 } // namespace blink |
OLD | NEW |