| 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 FakeResource* cachedResource = | 126 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); | 132 memoryCache()->add(cachedResource); |
| 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 MockResourceClient client(cachedResource); | 136 Persistent<MockResourceClient> client = new MockResourceClient(cachedResourc
e); |
| 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 MockResourceClient client(cachedLiveResource); | 240 Persistent<MockResourceClient> client = new MockResourceClient(cachedLiveRes
ource); |
| 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, cachedLiveResource, cachedDeadResource)); | 244 Platform::current()->currentThread()->getWebTaskRunner()->postTask(BLINK_FRO
M_HERE, bind(&runTask1, cachedLiveResource, 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, cachedLiveResour
ce); | 287 TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResour
ce); |
| 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 MockResourceClient client1(resource1); | 297 Persistent<MockResourceClient> client1 = new MockResourceClient(resource1); |
| 298 resource1->appendData(data, 4u); | 298 resource1->appendData(data, 4u); |
| 299 MockResourceClient client2(resource2); | 299 Persistent<MockResourceClient> client2 = new MockResourceClient(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. |
| 310 memoryCache()->prune(); | 310 memoryCache()->prune(); |
| 311 ASSERT_GT(resource1->decodedSize(), 0u); | 311 ASSERT_GT(resource1->decodedSize(), 0u); |
| 312 ASSERT_GT(resource2->decodedSize(), 0u); | 312 ASSERT_GT(resource2->decodedSize(), 0u); |
| 313 ASSERT_EQ(memoryCache()->deadSize(), 0u); | 313 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
| 314 ASSERT_EQ(memoryCache()->liveSize(), resource1->size() + resource2->size()); | 314 ASSERT_EQ(memoryCache()->liveSize(), resource1->size() + resource2->size()); |
| 315 | 315 |
| 316 // Removing the client from resource1 should result in all resources | 316 // Removing the client from resource1 should result in all resources |
| 317 // remaining in cache since the prune is deferred. | 317 // remaining in cache since the prune is deferred. |
| 318 client1.removeAsClient(); | 318 client1->removeAsClient(); |
| 319 ASSERT_GT(resource1->decodedSize(), 0u); | 319 ASSERT_GT(resource1->decodedSize(), 0u); |
| 320 ASSERT_GT(resource2->decodedSize(), 0u); | 320 ASSERT_GT(resource2->decodedSize(), 0u); |
| 321 ASSERT_EQ(memoryCache()->deadSize(), resource1->size()); | 321 ASSERT_EQ(memoryCache()->deadSize(), resource1->size()); |
| 322 ASSERT_EQ(memoryCache()->liveSize(), resource2->size()); | 322 ASSERT_EQ(memoryCache()->liveSize(), resource2->size()); |
| 323 ASSERT_TRUE(memoryCache()->contains(resource1)); | 323 ASSERT_TRUE(memoryCache()->contains(resource1)); |
| 324 ASSERT_TRUE(memoryCache()->contains(resource2)); | 324 ASSERT_TRUE(memoryCache()->contains(resource2)); |
| 325 | 325 |
| 326 // Removing the client from resource2 should result in immediate | 326 // Removing the client from resource2 should result in immediate |
| 327 // eviction of resource2 because we are over the prune deferral limit. | 327 // eviction of resource2 because we are over the prune deferral limit. |
| 328 client2.removeAsClient(); | 328 client2->removeAsClient(); |
| 329 ASSERT_GT(resource1->decodedSize(), 0u); | 329 ASSERT_GT(resource1->decodedSize(), 0u); |
| 330 ASSERT_GT(resource2->decodedSize(), 0u); | 330 ASSERT_GT(resource2->decodedSize(), 0u); |
| 331 ASSERT_EQ(memoryCache()->deadSize(), resource1->size()); | 331 ASSERT_EQ(memoryCache()->deadSize(), resource1->size()); |
| 332 ASSERT_EQ(memoryCache()->liveSize(), 0u); | 332 ASSERT_EQ(memoryCache()->liveSize(), 0u); |
| 333 ASSERT_TRUE(memoryCache()->contains(resource1)); | 333 ASSERT_TRUE(memoryCache()->contains(resource1)); |
| 334 ASSERT_FALSE(memoryCache()->contains(resource2)); | 334 ASSERT_FALSE(memoryCache()->contains(resource2)); |
| 335 } | 335 } |
| 336 | 336 |
| 337 TEST_F(MemoryCacheTest, ClientRemoval_Basic) | 337 TEST_F(MemoryCacheTest, ClientRemoval_Basic) |
| 338 { | 338 { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 420 |
| 421 HeapVector<Member<Resource>> resources = memoryCache()->resourcesForURL(url)
; | 421 HeapVector<Member<Resource>> resources = memoryCache()->resourcesForURL(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)); | 425 EXPECT_FALSE(memoryCache()->contains(resource1)); |
| 426 EXPECT_FALSE(memoryCache()->contains(resource3)); | 426 EXPECT_FALSE(memoryCache()->contains(resource3)); |
| 427 } | 427 } |
| 428 | 428 |
| 429 } // namespace blink | 429 } // namespace blink |
| OLD | NEW |