Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: Source/core/fetch/MemoryCacheTest.cpp

Issue 179943002: MemoryCache: make sure that Resources are evicted only when they can be deleted (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebase Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/fetch/MemoryCache.cpp ('k') | Source/core/fetch/Resource.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 // from cache when pruning. 146 // from cache when pruning.
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 ResourcePtr<Resource> cachedResource = 156 Resource* cachedResource =
157 new Resource(ResourceRequest(""), Resource::Raw); 157 new Resource(ResourceRequest(""), 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.get()); 167 memoryCache()->add(cachedResource);
168 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize()); 168 ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize());
169 ASSERT_EQ(0u, memoryCache()->liveSize()); 169 ASSERT_EQ(0u, memoryCache()->liveSize());
170 170
171 memoryCache()->prune(); 171 memoryCache()->prune();
172 ASSERT_EQ(0u, memoryCache()->deadSize()); 172 ASSERT_EQ(0u, memoryCache()->deadSize());
173 ASSERT_EQ(0u, memoryCache()->liveSize()); 173 ASSERT_EQ(0u, memoryCache()->liveSize());
174 } 174 }
175 175
176 // Verified that when ordering a prune in a runLoop task, the prune 176 // Verified that when ordering a prune in a runLoop task, the prune
177 // is deferred to the end of the task. 177 // is deferred to the end of the task.
178 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask) 178 TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask)
179 { 179 {
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 ResourcePtr<Resource> cachedDeadResource = 186 Resource* cachedDeadResource =
187 new Resource(ResourceRequest("http://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(""), Resource::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, const ResourcePtr<Resource>& de ad) 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 { }
201 201
202 virtual void run() OVERRIDE 202 virtual void run() OVERRIDE
203 { 203 {
204 // The resource size has to be nonzero for this test to be meaningfu l, but 204 // The resource size has to be nonzero for this test to be meaningfu l, but
205 // we do not rely on it having any particular value. 205 // we do not rely on it having any particular value.
206 ASSERT_GT(m_live->size(), 0u); 206 ASSERT_GT(m_live->size(), 0u);
207 ASSERT_GT(m_dead->size(), 0u); 207 ASSERT_GT(m_dead->size(), 0u);
208 208
209 ASSERT_EQ(0u, memoryCache()->deadSize()); 209 ASSERT_EQ(0u, memoryCache()->deadSize());
210 ASSERT_EQ(0u, memoryCache()->liveSize()); 210 ASSERT_EQ(0u, memoryCache()->liveSize());
211 211
212 memoryCache()->add(m_dead.get()); 212 memoryCache()->add(m_dead);
213 memoryCache()->add(m_live.get()); 213 memoryCache()->add(m_live.get());
214 memoryCache()->insertInLiveDecodedResourcesList(m_live.get()); 214 memoryCache()->insertInLiveDecodedResourcesList(m_live.get());
215 ASSERT_EQ(m_dead->size(), memoryCache()->deadSize()); 215 ASSERT_EQ(m_dead->size(), memoryCache()->deadSize());
216 ASSERT_EQ(m_live->size(), memoryCache()->liveSize()); 216 ASSERT_EQ(m_live->size(), memoryCache()->liveSize());
217 ASSERT_GT(m_live->decodedSize(), 0u); 217 ASSERT_GT(m_live->decodedSize(), 0u);
218 218
219 memoryCache()->prune(); // Dead resources are pruned immediately 219 memoryCache()->prune(); // Dead resources are pruned immediately
220 ASSERT_EQ(m_dead->size(), memoryCache()->deadSize()); 220 ASSERT_EQ(m_dead->size(), memoryCache()->deadSize());
221 ASSERT_EQ(m_live->size(), memoryCache()->liveSize()); 221 ASSERT_EQ(m_live->size(), memoryCache()->liveSize());
222 ASSERT_GT(m_live->decodedSize(), 0u); 222 ASSERT_GT(m_live->decodedSize(), 0u);
223 } 223 }
224 224
225 private: 225 private:
226 ResourcePtr<Resource> m_live, m_dead; 226 ResourcePtr<Resource> m_live;
227 Resource* m_dead;
227 }; 228 };
228 229
229 class Task2 : public blink::WebThread::Task { 230 class Task2 : public blink::WebThread::Task {
230 public: 231 public:
231 Task2(unsigned liveSizeWithoutDecode) 232 Task2(unsigned liveSizeWithoutDecode)
232 : m_liveSizeWithoutDecode(liveSizeWithoutDecode) { } 233 : m_liveSizeWithoutDecode(liveSizeWithoutDecode) { }
233 234
234 virtual void run() OVERRIDE 235 virtual void run() OVERRIDE
235 { 236 {
236 // Next task: now, the live resource was evicted. 237 // Next task: now, the live resource was evicted.
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 EXPECT_TRUE(memoryCache()->contains(resource3.get())); 401 EXPECT_TRUE(memoryCache()->contains(resource3.get()));
401 EXPECT_FALSE(memoryCache()->contains(resource2.get())); 402 EXPECT_FALSE(memoryCache()->contains(resource2.get()));
402 403
403 memoryCache()->replace(resource1.get(), resource2.get()); 404 memoryCache()->replace(resource1.get(), resource2.get());
404 EXPECT_TRUE(memoryCache()->contains(resource1.get())); 405 EXPECT_TRUE(memoryCache()->contains(resource1.get()));
405 EXPECT_FALSE(memoryCache()->contains(resource2.get())); 406 EXPECT_FALSE(memoryCache()->contains(resource2.get()));
406 EXPECT_FALSE(memoryCache()->contains(resource3.get())); 407 EXPECT_FALSE(memoryCache()->contains(resource3.get()));
407 } 408 }
408 409
409 } // namespace 410 } // namespace
OLDNEW
« no previous file with comments | « Source/core/fetch/MemoryCache.cpp ('k') | Source/core/fetch/Resource.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698