| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 class MemoryCacheTest : public ::testing::Test { | 45 class MemoryCacheTest : public ::testing::Test { |
| 46 public: | 46 public: |
| 47 class FakeDecodedResource : public blink::Resource { | 47 class FakeDecodedResource : public blink::Resource { |
| 48 public: | 48 public: |
| 49 FakeDecodedResource(const ResourceRequest& request, Type type) | 49 FakeDecodedResource(const ResourceRequest& request, Type type) |
| 50 : Resource(request, type) | 50 : Resource(request, type) |
| 51 { | 51 { |
| 52 } | 52 } |
| 53 | 53 |
| 54 virtual void appendData(const char* data, int len) | 54 virtual void appendData(const char* data, unsigned len) |
| 55 { | 55 { |
| 56 Resource::appendData(data, len); | 56 Resource::appendData(data, len); |
| 57 setDecodedSize(this->size()); | 57 setDecodedSize(this->size()); |
| 58 } | 58 } |
| 59 | 59 |
| 60 protected: | 60 protected: |
| 61 virtual void destroyDecodedDataIfPossible() OVERRIDE | 61 virtual void destroyDecodedDataIfPossible() OVERRIDE |
| 62 { | 62 { |
| 63 setDecodedSize(0); | 63 setDecodedSize(0); |
| 64 } | 64 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 143 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
| 144 memoryCache()->setMaxPruneDeferralDelay(0); | 144 memoryCache()->setMaxPruneDeferralDelay(0); |
| 145 const unsigned totalCapacity = 1000000; | 145 const unsigned totalCapacity = 1000000; |
| 146 const unsigned minDeadCapacity = 0; | 146 const unsigned minDeadCapacity = 0; |
| 147 const unsigned maxDeadCapacity = 0; | 147 const unsigned maxDeadCapacity = 0; |
| 148 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 148 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
| 149 | 149 |
| 150 Resource* cachedResource = | 150 Resource* cachedResource = |
| 151 new Resource(ResourceRequest("http://test/resource"), Resource::Raw); | 151 new Resource(ResourceRequest("http://test/resource"), Resource::Raw); |
| 152 const char data[5] = "abcd"; | 152 const char data[5] = "abcd"; |
| 153 cachedResource->appendData(data, 3); | 153 cachedResource->appendData(data, 3u); |
| 154 // The resource size has to be nonzero for this test to be meaningful, but | 154 // The resource size has to be nonzero for this test to be meaningful, but |
| 155 // we do not rely on it having any particular value. | 155 // we do not rely on it having any particular value. |
| 156 ASSERT_GT(cachedResource->size(), 0u); | 156 ASSERT_GT(cachedResource->size(), 0u); |
| 157 | 157 |
| 158 ASSERT_EQ(0u, memoryCache()->deadSize()); | 158 ASSERT_EQ(0u, memoryCache()->deadSize()); |
| 159 ASSERT_EQ(0u, memoryCache()->liveSize()); | 159 ASSERT_EQ(0u, memoryCache()->liveSize()); |
| 160 | 160 |
| 161 memoryCache()->add(cachedResource); | 161 memoryCache()->add(cachedResource); |
| 162 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); | 162 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); |
| 163 ASSERT_EQ(0u, memoryCache()->liveSize()); | 163 ASSERT_EQ(0u, memoryCache()->liveSize()); |
| 164 | 164 |
| 165 memoryCache()->prune(); | 165 memoryCache()->prune(); |
| 166 ASSERT_EQ(0u, memoryCache()->deadSize()); | 166 ASSERT_EQ(0u, memoryCache()->deadSize()); |
| 167 ASSERT_EQ(0u, memoryCache()->liveSize()); | 167 ASSERT_EQ(0u, memoryCache()->liveSize()); |
| 168 } | 168 } |
| 169 | 169 |
| 170 // Verified that when ordering a prune in a runLoop task, the prune | 170 // Verified that when ordering a prune in a runLoop task, the prune |
| 171 // is deferred to the end of the task. | 171 // is deferred to the end of the task. |
| 172 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask) | 172 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask) |
| 173 { | 173 { |
| 174 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 174 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
| 175 const unsigned totalCapacity = 1; | 175 const unsigned totalCapacity = 1; |
| 176 const unsigned minDeadCapacity = 0; | 176 const unsigned minDeadCapacity = 0; |
| 177 const unsigned maxDeadCapacity = 0; | 177 const unsigned maxDeadCapacity = 0; |
| 178 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 178 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
| 179 const char data[6] = "abcde"; | 179 const char data[6] = "abcde"; |
| 180 Resource* cachedDeadResource = | 180 Resource* cachedDeadResource = |
| 181 new Resource(ResourceRequest("hhtp://foo"), Resource::Raw); | 181 new Resource(ResourceRequest("hhtp://foo"), Resource::Raw); |
| 182 cachedDeadResource->appendData(data, 3); | 182 cachedDeadResource->appendData(data, 3u); |
| 183 ResourcePtr<Resource> cachedLiveResource = | 183 ResourcePtr<Resource> cachedLiveResource = |
| 184 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); | 184 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); |
| 185 MockImageResourceClient client; | 185 MockImageResourceClient client; |
| 186 cachedLiveResource->addClient(&client); | 186 cachedLiveResource->addClient(&client); |
| 187 cachedLiveResource->appendData(data, 4); | 187 cachedLiveResource->appendData(data, 4u); |
| 188 | 188 |
| 189 class Task1 : public blink::WebThread::Task { | 189 class Task1 : public blink::WebThread::Task { |
| 190 public: | 190 public: |
| 191 Task1(const ResourcePtr<Resource>& live, Resource* dead) | 191 Task1(const ResourcePtr<Resource>& live, Resource* dead) |
| 192 : m_live(live) | 192 : m_live(live) |
| 193 , m_dead(dead) | 193 , m_dead(dead) |
| 194 { } | 194 { } |
| 195 | 195 |
| 196 virtual void run() OVERRIDE | 196 virtual void run() OVERRIDE |
| 197 { | 197 { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 247 |
| 248 // Verifies that cached resources are evicted immediately after release when | 248 // Verifies that cached resources are evicted immediately after release when |
| 249 // the total dead resource size is more than double the dead resource capacity. | 249 // the total dead resource size is more than double the dead resource capacity. |
| 250 TEST_F(MemoryCacheTest, ClientRemoval) | 250 TEST_F(MemoryCacheTest, ClientRemoval) |
| 251 { | 251 { |
| 252 const char data[6] = "abcde"; | 252 const char data[6] = "abcde"; |
| 253 ResourcePtr<Resource> resource1 = | 253 ResourcePtr<Resource> resource1 = |
| 254 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); | 254 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); |
| 255 MockImageResourceClient client1; | 255 MockImageResourceClient client1; |
| 256 resource1->addClient(&client1); | 256 resource1->addClient(&client1); |
| 257 resource1->appendData(data, 4); | 257 resource1->appendData(data, 4u); |
| 258 ResourcePtr<Resource> resource2 = | 258 ResourcePtr<Resource> resource2 = |
| 259 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); | 259 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); |
| 260 MockImageResourceClient client2; | 260 MockImageResourceClient client2; |
| 261 resource2->addClient(&client2); | 261 resource2->addClient(&client2); |
| 262 resource2->appendData(data, 4); | 262 resource2->appendData(data, 4u); |
| 263 | 263 |
| 264 const unsigned minDeadCapacity = 0; | 264 const unsigned minDeadCapacity = 0; |
| 265 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; | 265 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; |
| 266 const unsigned totalCapacity = maxDeadCapacity; | 266 const unsigned totalCapacity = maxDeadCapacity; |
| 267 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 267 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
| 268 memoryCache()->add(resource1.get()); | 268 memoryCache()->add(resource1.get()); |
| 269 memoryCache()->add(resource2.get()); | 269 memoryCache()->add(resource2.get()); |
| 270 // Call prune. There is nothing to prune, but this will initialize | 270 // Call prune. There is nothing to prune, but this will initialize |
| 271 // the prune timestamp, allowing future prunes to be deferred. | 271 // the prune timestamp, allowing future prunes to be deferred. |
| 272 memoryCache()->prune(); | 272 memoryCache()->prune(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); | 306 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); |
| 307 ResourcePtr<FakeDecodedResource> cachedImageHighPriority = | 307 ResourcePtr<FakeDecodedResource> cachedImageHighPriority = |
| 308 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); | 308 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); |
| 309 | 309 |
| 310 MockImageResourceClient clientLowPriority; | 310 MockImageResourceClient clientLowPriority; |
| 311 MockImageResourceClient clientHighPriority; | 311 MockImageResourceClient clientHighPriority; |
| 312 cachedImageLowPriority->addClient(&clientLowPriority); | 312 cachedImageLowPriority->addClient(&clientLowPriority); |
| 313 cachedImageHighPriority->addClient(&clientHighPriority); | 313 cachedImageHighPriority->addClient(&clientHighPriority); |
| 314 | 314 |
| 315 const char data[5] = "abcd"; | 315 const char data[5] = "abcd"; |
| 316 cachedImageLowPriority->appendData(data, 1); | 316 cachedImageLowPriority->appendData(data, 1u); |
| 317 cachedImageHighPriority->appendData(data, 4); | 317 cachedImageHighPriority->appendData(data, 4u); |
| 318 const unsigned lowPrioritySize = cachedImageLowPriority->size(); | 318 const unsigned lowPrioritySize = cachedImageLowPriority->size(); |
| 319 const unsigned highPrioritySize = cachedImageHighPriority->size(); | 319 const unsigned highPrioritySize = cachedImageHighPriority->size(); |
| 320 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi
ze(); | 320 const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSi
ze(); |
| 321 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded
Size(); | 321 const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decoded
Size(); |
| 322 const unsigned totalSize = lowPrioritySize + highPrioritySize; | 322 const unsigned totalSize = lowPrioritySize + highPrioritySize; |
| 323 | 323 |
| 324 // Verify that the sizes are different to ensure that we can test eviction o
rder. | 324 // Verify that the sizes are different to ensure that we can test eviction o
rder. |
| 325 ASSERT_GT(lowPrioritySize, 0u); | 325 ASSERT_GT(lowPrioritySize, 0u); |
| 326 ASSERT_NE(lowPrioritySize, highPrioritySize); | 326 ASSERT_NE(lowPrioritySize, highPrioritySize); |
| 327 ASSERT_GT(lowPriorityMockDecodeSize, 0u); | 327 ASSERT_GT(lowPriorityMockDecodeSize, 0u); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 EXPECT_TRUE(memoryCache()->contains(resource3.get())); | 398 EXPECT_TRUE(memoryCache()->contains(resource3.get())); |
| 399 EXPECT_FALSE(memoryCache()->contains(resource2.get())); | 399 EXPECT_FALSE(memoryCache()->contains(resource2.get())); |
| 400 | 400 |
| 401 memoryCache()->replace(resource1.get(), resource2.get()); | 401 memoryCache()->replace(resource1.get(), resource2.get()); |
| 402 EXPECT_TRUE(memoryCache()->contains(resource1.get())); | 402 EXPECT_TRUE(memoryCache()->contains(resource1.get())); |
| 403 EXPECT_FALSE(memoryCache()->contains(resource2.get())); | 403 EXPECT_FALSE(memoryCache()->contains(resource2.get())); |
| 404 EXPECT_FALSE(memoryCache()->contains(resource3.get())); | 404 EXPECT_FALSE(memoryCache()->contains(resource3.get())); |
| 405 } | 405 } |
| 406 | 406 |
| 407 } // namespace | 407 } // namespace |
| OLD | NEW |