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 10 matching lines...) Expand all Loading... |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
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 "platform/loader/fetch/MemoryCache.h" |
32 | 32 |
33 #include "core/fetch/MockResourceClient.h" | 33 #include "platform/loader/fetch/MockResourceClient.h" |
34 #include "core/fetch/RawResource.h" | 34 #include "platform/loader/fetch/RawResource.h" |
35 #include "platform/network/ResourceRequest.h" | 35 #include "platform/network/ResourceRequest.h" |
| 36 #include "platform/testing/TestingPlatformSupport.h" |
36 #include "platform/testing/UnitTestHelpers.h" | 37 #include "platform/testing/UnitTestHelpers.h" |
37 #include "public/platform/Platform.h" | 38 #include "public/platform/Platform.h" |
38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
39 | 40 |
40 namespace blink { | 41 namespace blink { |
41 | 42 |
42 class MemoryCacheTest : public ::testing::Test { | 43 class MemoryCacheTest : public ::testing::Test { |
43 public: | 44 public: |
44 class FakeDecodedResource final : public Resource { | 45 class FakeDecodedResource final : public Resource { |
45 public: | 46 public: |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 resource2->finish(); | 174 resource2->finish(); |
174 | 175 |
175 Platform::current()->currentThread()->getWebTaskRunner()->postTask( | 176 Platform::current()->currentThread()->getWebTaskRunner()->postTask( |
176 BLINK_FROM_HERE, WTF::bind(&runTask1, wrapPersistent(resource1), | 177 BLINK_FROM_HERE, WTF::bind(&runTask1, wrapPersistent(resource1), |
177 wrapPersistent(resource2))); | 178 wrapPersistent(resource2))); |
178 Platform::current()->currentThread()->getWebTaskRunner()->postTask( | 179 Platform::current()->currentThread()->getWebTaskRunner()->postTask( |
179 BLINK_FROM_HERE, | 180 BLINK_FROM_HERE, |
180 WTF::bind(&runTask2, | 181 WTF::bind(&runTask2, |
181 resource1->encodedSize() + resource1->overheadSize() + | 182 resource1->encodedSize() + resource1->overheadSize() + |
182 resource2->encodedSize() + resource2->overheadSize())); | 183 resource2->encodedSize() + resource2->overheadSize())); |
183 testing::runPendingTasks(); | |
184 } | 184 } |
185 | 185 |
186 // Verified that when ordering a prune in a runLoop task, the prune | 186 // Verified that when ordering a prune in a runLoop task, the prune |
187 // is deferred to the end of the task. | 187 // is deferred to the end of the task. |
188 TEST_F(MemoryCacheTest, ResourcePruningAtEndOfTask_Basic) { | 188 TEST_F(MemoryCacheTest, ResourcePruningAtEndOfTask_Basic) { |
| 189 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> |
| 190 platform; |
189 Resource* resource1 = FakeDecodedResource::create( | 191 Resource* resource1 = FakeDecodedResource::create( |
190 ResourceRequest("http://test/resource1"), Resource::Raw); | 192 ResourceRequest("http://test/resource1"), Resource::Raw); |
191 Resource* resource2 = FakeDecodedResource::create( | 193 Resource* resource2 = FakeDecodedResource::create( |
192 ResourceRequest("http://test/resource2"), Resource::Raw); | 194 ResourceRequest("http://test/resource2"), Resource::Raw); |
193 testResourcePruningAtEndOfTask(resource1, resource2); | 195 testResourcePruningAtEndOfTask(resource1, resource2); |
| 196 platform->runUntilIdle(); |
194 } | 197 } |
195 | 198 |
196 TEST_F(MemoryCacheTest, ResourcePruningAtEndOfTask_MultipleResourceMaps) { | 199 TEST_F(MemoryCacheTest, ResourcePruningAtEndOfTask_MultipleResourceMaps) { |
| 200 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> |
| 201 platform; |
197 { | 202 { |
198 Resource* resource1 = FakeDecodedResource::create( | 203 Resource* resource1 = FakeDecodedResource::create( |
199 ResourceRequest("http://test/resource1"), Resource::Raw); | 204 ResourceRequest("http://test/resource1"), Resource::Raw); |
200 Resource* resource2 = FakeDecodedResource::create( | 205 Resource* resource2 = FakeDecodedResource::create( |
201 ResourceRequest("http://test/resource2"), Resource::Raw); | 206 ResourceRequest("http://test/resource2"), Resource::Raw); |
202 resource1->setCacheIdentifier("foo"); | 207 resource1->setCacheIdentifier("foo"); |
203 testResourcePruningAtEndOfTask(resource1, resource2); | 208 testResourcePruningAtEndOfTask(resource1, resource2); |
| 209 platform->runUntilIdle(); |
204 memoryCache()->evictResources(); | 210 memoryCache()->evictResources(); |
205 } | 211 } |
206 { | 212 { |
207 Resource* resource1 = FakeDecodedResource::create( | 213 Resource* resource1 = FakeDecodedResource::create( |
208 ResourceRequest("http://test/resource1"), Resource::Raw); | 214 ResourceRequest("http://test/resource1"), Resource::Raw); |
209 Resource* resource2 = FakeDecodedResource::create( | 215 Resource* resource2 = FakeDecodedResource::create( |
210 ResourceRequest("http://test/resource2"), Resource::Raw); | 216 ResourceRequest("http://test/resource2"), Resource::Raw); |
211 resource1->setCacheIdentifier("foo"); | 217 resource1->setCacheIdentifier("foo"); |
212 resource2->setCacheIdentifier("bar"); | 218 resource2->setCacheIdentifier("bar"); |
213 testResourcePruningAtEndOfTask(resource1, resource2); | 219 testResourcePruningAtEndOfTask(resource1, resource2); |
| 220 platform->runUntilIdle(); |
214 memoryCache()->evictResources(); | 221 memoryCache()->evictResources(); |
215 } | 222 } |
216 } | 223 } |
217 | 224 |
218 // Verifies that | 225 // Verifies that |
219 // - Resources are not pruned synchronously when ResourceClient is removed. | 226 // - Resources are not pruned synchronously when ResourceClient is removed. |
220 // - size() is updated appropriately when Resources are added to MemoryCache | 227 // - size() is updated appropriately when Resources are added to MemoryCache |
221 // and garbage collected. | 228 // and garbage collected. |
222 static void testClientRemoval(Resource* resource1, Resource* resource2) { | 229 static void testClientRemoval(Resource* resource1, Resource* resource2) { |
223 const char data[6] = "abcde"; | 230 const char data[6] = "abcde"; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 FakeResource* resource2 = | 396 FakeResource* resource2 = |
390 FakeResource::create(ResourceRequest(url2), Resource::Raw); | 397 FakeResource::create(ResourceRequest(url2), Resource::Raw); |
391 memoryCache()->add(resource2); | 398 memoryCache()->add(resource2); |
392 EXPECT_TRUE(memoryCache()->contains(resource2)); | 399 EXPECT_TRUE(memoryCache()->contains(resource2)); |
393 | 400 |
394 memoryCache()->removeURLFromCache(url2); | 401 memoryCache()->removeURLFromCache(url2); |
395 EXPECT_FALSE(memoryCache()->contains(resource2)); | 402 EXPECT_FALSE(memoryCache()->contains(resource2)); |
396 } | 403 } |
397 | 404 |
398 } // namespace blink | 405 } // namespace blink |
OLD | NEW |