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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 TEST_F(MemoryCacheTest, VeryLargeResourceAccounting) | 116 TEST_F(MemoryCacheTest, VeryLargeResourceAccounting) |
117 { | 117 { |
118 const size_t sizeMax = ~static_cast<size_t>(0); | 118 const size_t sizeMax = ~static_cast<size_t>(0); |
119 const size_t totalCapacity = sizeMax / 4; | 119 const size_t totalCapacity = sizeMax / 4; |
120 const size_t minDeadCapacity = sizeMax / 16; | 120 const size_t minDeadCapacity = sizeMax / 16; |
121 const size_t maxDeadCapacity = sizeMax / 8; | 121 const size_t maxDeadCapacity = sizeMax / 8; |
122 const size_t resourceSize1 = sizeMax / 16; | 122 const size_t resourceSize1 = sizeMax / 16; |
123 const size_t resourceSize2 = sizeMax / 20; | 123 const size_t resourceSize2 = sizeMax / 20; |
124 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 124 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
125 ResourcePtr<FakeResource> cachedResource = | 125 ResourcePtr<FakeResource> cachedResource = |
126 new FakeResource(ResourceRequest(""), Resource::Raw); | 126 new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw)
; |
127 cachedResource->fakeEncodedSize(resourceSize1); | 127 cachedResource->fakeEncodedSize(resourceSize1); |
128 | 128 |
129 ASSERT_EQ(0u, memoryCache()->deadSize()); | 129 ASSERT_EQ(0u, memoryCache()->deadSize()); |
130 ASSERT_EQ(0u, memoryCache()->liveSize()); | 130 ASSERT_EQ(0u, memoryCache()->liveSize()); |
131 memoryCache()->add(cachedResource.get()); | 131 memoryCache()->add(cachedResource.get()); |
132 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); | 132 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); |
133 ASSERT_EQ(0u, memoryCache()->liveSize()); | 133 ASSERT_EQ(0u, memoryCache()->liveSize()); |
134 | 134 |
135 MockImageResourceClient client; | 135 MockImageResourceClient client; |
136 cachedResource->addClient(&client); | 136 cachedResource->addClient(&client); |
(...skipping 10 matching lines...) Expand all Loading... |
147 TEST_F(MemoryCacheTest, DeadResourceEviction) | 147 TEST_F(MemoryCacheTest, DeadResourceEviction) |
148 { | 148 { |
149 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 149 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
150 memoryCache()->setMaxPruneDeferralDelay(0); | 150 memoryCache()->setMaxPruneDeferralDelay(0); |
151 const unsigned totalCapacity = 1000000; | 151 const unsigned totalCapacity = 1000000; |
152 const unsigned minDeadCapacity = 0; | 152 const unsigned minDeadCapacity = 0; |
153 const unsigned maxDeadCapacity = 0; | 153 const unsigned maxDeadCapacity = 0; |
154 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 154 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
155 | 155 |
156 Resource* cachedResource = | 156 Resource* cachedResource = |
157 new Resource(ResourceRequest(""), Resource::Raw); | 157 new Resource(ResourceRequest("http://test/resource"), Resource::Raw); |
158 const char data[5] = "abcd"; | 158 const char data[5] = "abcd"; |
159 cachedResource->appendData(data, 3); | 159 cachedResource->appendData(data, 3); |
160 // The resource size has to be nonzero for this test to be meaningful, but | 160 // The resource size has to be nonzero for this test to be meaningful, but |
161 // we do not rely on it having any particular value. | 161 // we do not rely on it having any particular value. |
162 ASSERT_GT(cachedResource->size(), 0u); | 162 ASSERT_GT(cachedResource->size(), 0u); |
163 | 163 |
164 ASSERT_EQ(0u, memoryCache()->deadSize()); | 164 ASSERT_EQ(0u, memoryCache()->deadSize()); |
165 ASSERT_EQ(0u, memoryCache()->liveSize()); | 165 ASSERT_EQ(0u, memoryCache()->liveSize()); |
166 | 166 |
167 memoryCache()->add(cachedResource); | 167 memoryCache()->add(cachedResource); |
(...skipping 12 matching lines...) Expand all Loading... |
180 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 180 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
181 const unsigned totalCapacity = 1; | 181 const unsigned totalCapacity = 1; |
182 const unsigned minDeadCapacity = 0; | 182 const unsigned minDeadCapacity = 0; |
183 const unsigned maxDeadCapacity = 0; | 183 const unsigned maxDeadCapacity = 0; |
184 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 184 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
185 const char data[6] = "abcde"; | 185 const char data[6] = "abcde"; |
186 Resource* cachedDeadResource = | 186 Resource* cachedDeadResource = |
187 new Resource(ResourceRequest("hhtp://foo"), Resource::Raw); | 187 new Resource(ResourceRequest("hhtp://foo"), Resource::Raw); |
188 cachedDeadResource->appendData(data, 3); | 188 cachedDeadResource->appendData(data, 3); |
189 ResourcePtr<Resource> cachedLiveResource = | 189 ResourcePtr<Resource> cachedLiveResource = |
190 new FakeDecodedResource(ResourceRequest(""), Resource::Raw); | 190 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); |
191 MockImageResourceClient client; | 191 MockImageResourceClient client; |
192 cachedLiveResource->addClient(&client); | 192 cachedLiveResource->addClient(&client); |
193 cachedLiveResource->appendData(data, 4); | 193 cachedLiveResource->appendData(data, 4); |
194 | 194 |
195 class Task1 : public blink::WebThread::Task { | 195 class Task1 : public blink::WebThread::Task { |
196 public: | 196 public: |
197 Task1(const ResourcePtr<Resource>& live, Resource* dead) | 197 Task1(const ResourcePtr<Resource>& live, Resource* dead) |
198 : m_live(live) | 198 : m_live(live) |
199 , m_dead(dead) | 199 , m_dead(dead) |
200 { } | 200 { } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 // the total dead resource size is more than double the dead resource capacity. | 255 // the total dead resource size is more than double the dead resource capacity. |
256 TEST_F(MemoryCacheTest, ClientRemoval) | 256 TEST_F(MemoryCacheTest, ClientRemoval) |
257 { | 257 { |
258 const char data[6] = "abcde"; | 258 const char data[6] = "abcde"; |
259 ResourcePtr<Resource> resource1 = | 259 ResourcePtr<Resource> resource1 = |
260 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); | 260 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); |
261 MockImageResourceClient client1; | 261 MockImageResourceClient client1; |
262 resource1->addClient(&client1); | 262 resource1->addClient(&client1); |
263 resource1->appendData(data, 4); | 263 resource1->appendData(data, 4); |
264 ResourcePtr<Resource> resource2 = | 264 ResourcePtr<Resource> resource2 = |
265 new FakeDecodedResource(ResourceRequest(""), Resource::Raw); | 265 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); |
266 MockImageResourceClient client2; | 266 MockImageResourceClient client2; |
267 resource2->addClient(&client2); | 267 resource2->addClient(&client2); |
268 resource2->appendData(data, 4); | 268 resource2->appendData(data, 4); |
269 | 269 |
270 const unsigned minDeadCapacity = 0; | 270 const unsigned minDeadCapacity = 0; |
271 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; | 271 const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) /
2) - 1; |
272 const unsigned totalCapacity = maxDeadCapacity; | 272 const unsigned totalCapacity = maxDeadCapacity; |
273 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); | 273 memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity
); |
274 memoryCache()->add(resource1.get()); | 274 memoryCache()->add(resource1.get()); |
275 memoryCache()->add(resource2.get()); | 275 memoryCache()->add(resource2.get()); |
(...skipping 28 matching lines...) Expand all Loading... |
304 | 304 |
305 // Verifies that CachedResources are evicted from the decode cache | 305 // Verifies that CachedResources are evicted from the decode cache |
306 // according to their DecodeCachePriority. | 306 // according to their DecodeCachePriority. |
307 TEST_F(MemoryCacheTest, DecodeCacheOrder) | 307 TEST_F(MemoryCacheTest, DecodeCacheOrder) |
308 { | 308 { |
309 memoryCache()->setDelayBeforeLiveDecodedPrune(0); | 309 memoryCache()->setDelayBeforeLiveDecodedPrune(0); |
310 memoryCache()->setMaxPruneDeferralDelay(0); | 310 memoryCache()->setMaxPruneDeferralDelay(0); |
311 ResourcePtr<FakeDecodedResource> cachedImageLowPriority = | 311 ResourcePtr<FakeDecodedResource> cachedImageLowPriority = |
312 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); | 312 new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw
); |
313 ResourcePtr<FakeDecodedResource> cachedImageHighPriority = | 313 ResourcePtr<FakeDecodedResource> cachedImageHighPriority = |
314 new FakeDecodedResource(ResourceRequest(""), Resource::Raw); | 314 new FakeDecodedResource(ResourceRequest("http://test/resource"), Resourc
e::Raw); |
315 | 315 |
316 MockImageResourceClient clientLowPriority; | 316 MockImageResourceClient clientLowPriority; |
317 MockImageResourceClient clientHighPriority; | 317 MockImageResourceClient clientHighPriority; |
318 cachedImageLowPriority->addClient(&clientLowPriority); | 318 cachedImageLowPriority->addClient(&clientLowPriority); |
319 cachedImageHighPriority->addClient(&clientHighPriority); | 319 cachedImageHighPriority->addClient(&clientHighPriority); |
320 | 320 |
321 const char data[5] = "abcd"; | 321 const char data[5] = "abcd"; |
322 cachedImageLowPriority->appendData(data, 1); | 322 cachedImageLowPriority->appendData(data, 1); |
323 cachedImageHighPriority->appendData(data, 4); | 323 cachedImageHighPriority->appendData(data, 4); |
324 const unsigned lowPrioritySize = cachedImageLowPriority->size(); | 324 const unsigned lowPrioritySize = cachedImageLowPriority->size(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 | 363 |
364 // Should prune the HighPriority item. | 364 // Should prune the HighPriority item. |
365 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()
->liveSize() - 10, memoryCache()->liveSize() - 10); | 365 memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()
->liveSize() - 10, memoryCache()->liveSize() - 10); |
366 memoryCache()->prune(); | 366 memoryCache()->prune(); |
367 ASSERT_EQ(memoryCache()->deadSize(), 0u); | 367 ASSERT_EQ(memoryCache()->deadSize(), 0u); |
368 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize -
highPriorityMockDecodeSize); | 368 ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize -
highPriorityMockDecodeSize); |
369 } | 369 } |
370 | 370 |
371 TEST_F(MemoryCacheTest, MultipleReplace) | 371 TEST_F(MemoryCacheTest, MultipleReplace) |
372 { | 372 { |
373 ResourcePtr<FakeResource> resource1 = new FakeResource(ResourceRequest(""),
Resource::Raw); | 373 ResourcePtr<FakeResource> resource1 = new FakeResource(ResourceRequest("http
://test/resource"), Resource::Raw); |
374 memoryCache()->add(resource1.get()); | 374 memoryCache()->add(resource1.get()); |
375 | 375 |
376 ResourcePtr<FakeResource> resource2 = new FakeResource(ResourceRequest(""),
Resource::Raw); | 376 ResourcePtr<FakeResource> resource2 = new FakeResource(ResourceRequest("http
://test/resource"), Resource::Raw); |
377 memoryCache()->replace(resource2.get(), resource1.get()); | 377 memoryCache()->replace(resource2.get(), resource1.get()); |
378 EXPECT_TRUE(memoryCache()->contains(resource2.get())); | 378 EXPECT_TRUE(memoryCache()->contains(resource2.get())); |
379 EXPECT_FALSE(memoryCache()->contains(resource1.get())); | 379 EXPECT_FALSE(memoryCache()->contains(resource1.get())); |
380 | 380 |
381 ResourcePtr<FakeResource> resource3 = new FakeResource(ResourceRequest(""),
Resource::Raw); | 381 ResourcePtr<FakeResource> resource3 = new FakeResource(ResourceRequest("http
://test/resource"), Resource::Raw); |
382 memoryCache()->replace(resource3.get(), resource2.get()); | 382 memoryCache()->replace(resource3.get(), resource2.get()); |
383 EXPECT_TRUE(memoryCache()->contains(resource3.get())); | 383 EXPECT_TRUE(memoryCache()->contains(resource3.get())); |
384 EXPECT_FALSE(memoryCache()->contains(resource2.get())); | 384 EXPECT_FALSE(memoryCache()->contains(resource2.get())); |
385 } | 385 } |
386 | 386 |
387 TEST_F(MemoryCacheTest, RemoveDuringRevalidation) | 387 TEST_F(MemoryCacheTest, RemoveDuringRevalidation) |
388 { | 388 { |
389 ResourcePtr<FakeResource> resource1 = new FakeResource(ResourceRequest(""),
Resource::Raw); | 389 ResourcePtr<FakeResource> resource1 = new FakeResource(ResourceRequest("http
://test/resource"), Resource::Raw); |
390 memoryCache()->add(resource1.get()); | 390 memoryCache()->add(resource1.get()); |
391 | 391 |
392 ResourcePtr<FakeResource> resource2 = new FakeResource(ResourceRequest(""),
Resource::Raw); | 392 ResourcePtr<FakeResource> resource2 = new FakeResource(ResourceRequest("http
://test/resource"), Resource::Raw); |
393 memoryCache()->remove(resource1.get()); | 393 memoryCache()->remove(resource1.get()); |
394 memoryCache()->add(resource2.get()); | 394 memoryCache()->add(resource2.get()); |
395 EXPECT_TRUE(memoryCache()->contains(resource2.get())); | 395 EXPECT_TRUE(memoryCache()->contains(resource2.get())); |
396 EXPECT_FALSE(memoryCache()->contains(resource1.get())); | 396 EXPECT_FALSE(memoryCache()->contains(resource1.get())); |
397 | 397 |
398 ResourcePtr<FakeResource> resource3 = new FakeResource(ResourceRequest(""),
Resource::Raw); | 398 ResourcePtr<FakeResource> resource3 = new FakeResource(ResourceRequest("http
://test/resource"), Resource::Raw); |
399 memoryCache()->remove(resource2.get()); | 399 memoryCache()->remove(resource2.get()); |
400 memoryCache()->add(resource3.get()); | 400 memoryCache()->add(resource3.get()); |
401 EXPECT_TRUE(memoryCache()->contains(resource3.get())); | 401 EXPECT_TRUE(memoryCache()->contains(resource3.get())); |
402 EXPECT_FALSE(memoryCache()->contains(resource2.get())); | 402 EXPECT_FALSE(memoryCache()->contains(resource2.get())); |
403 | 403 |
404 memoryCache()->replace(resource1.get(), resource2.get()); | 404 memoryCache()->replace(resource1.get(), resource2.get()); |
405 EXPECT_TRUE(memoryCache()->contains(resource1.get())); | 405 EXPECT_TRUE(memoryCache()->contains(resource1.get())); |
406 EXPECT_FALSE(memoryCache()->contains(resource2.get())); | 406 EXPECT_FALSE(memoryCache()->contains(resource2.get())); |
407 EXPECT_FALSE(memoryCache()->contains(resource3.get())); | 407 EXPECT_FALSE(memoryCache()->contains(resource3.get())); |
408 } | 408 } |
409 | 409 |
410 } // namespace | 410 } // namespace |
OLD | NEW |