| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/memory_pressure_listener.h" | 5 #include "base/memory/memory_pressure_listener.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/test/simple_test_clock.h" | 8 #include "base/test/simple_test_clock.h" |
| 9 #include "net/base/net_log.h" | 9 #include "net/base/net_log.h" |
| 10 #include "net/base/sdch_manager.h" | 10 #include "net/base/sdch_manager.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 | 113 |
| 114 bool IsHandledURL(const GURL& url) const override { | 114 bool IsHandledURL(const GURL& url) const override { |
| 115 return url.SchemeIs("http"); | 115 return url.SchemeIs("http"); |
| 116 } | 116 } |
| 117 | 117 |
| 118 bool IsSafeRedirectTarget(const GURL& location) const override { | 118 bool IsSafeRedirectTarget(const GURL& location) const override { |
| 119 return false; | 119 return false; |
| 120 } | 120 } |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 // File testing infrastructure summary: |
| 124 // * NewSdchDictionary(): Creates a dictionary of a specific size. |
| 125 // * URLRequestErrorCountingJob: A URLRequestJob that returns an error |
| 126 // and counts the number of outstanding (started but not finished) |
| 127 // jobs, and calls a global callback when that number transitions to zero. |
| 128 // * MockURLRequestJobFactory: Factory to create the above jobs. Tracks |
| 129 // the number of jobs created. |
| 130 // * SdchOwnerTest: Interfaces |
| 131 // * Access manager, owner, and net log |
| 132 // * Return the number of jobs created in a time interval |
| 133 // * Return dictionary present in the manager |
| 134 // * Notify SdchOwner of an incoming dictionary (& wait until jobs clear) |
| 135 // * Attempt to add a dictionary and test for success. |
| 136 // Test patterns: |
| 137 // * Let the owner know about a Get-Dictionary header and test for |
| 138 // appropriate jobs being created. |
| 139 // * Let the owner know that a dictionary was successfully fetched |
| 140 // and test for appropriate outcome. |
| 141 // * Either of the above, having previously added dictionaries to create |
| 142 // a particular initial state. |
| 123 class SdchOwnerTest : public testing::Test { | 143 class SdchOwnerTest : public testing::Test { |
| 124 public: | 144 public: |
| 125 static const size_t kMaxSizeForTesting = 1000 * 50; | 145 static const size_t kMaxSizeForTesting = 1000 * 50; |
| 126 static const size_t kMinFetchSpaceForTesting = 500; | 146 static const size_t kMinFetchSpaceForTesting = 500; |
| 127 | 147 |
| 128 SdchOwnerTest() | 148 SdchOwnerTest() |
| 129 : last_jobs_created_(error_jobs_created), | 149 : last_jobs_created_(error_jobs_created), |
| 130 dictionary_creation_index_(0), | 150 dictionary_creation_index_(0), |
| 131 sdch_owner_(&sdch_manager_, &url_request_context_) { | 151 sdch_owner_(&sdch_manager_, &url_request_context_) { |
| 132 // Any jobs created on this context will immediately error, | 152 // Any jobs created on this context will immediately error, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 base::Closure quit_closure(run_loop.QuitClosure()); | 185 base::Closure quit_closure(run_loop.QuitClosure()); |
| 166 empty_url_request_jobs_callback = &quit_closure; | 186 empty_url_request_jobs_callback = &quit_closure; |
| 167 run_loop.Run(); | 187 run_loop.Run(); |
| 168 empty_url_request_jobs_callback = NULL; | 188 empty_url_request_jobs_callback = NULL; |
| 169 } | 189 } |
| 170 | 190 |
| 171 // Create a unique (by hash) dictionary of the given size, | 191 // Create a unique (by hash) dictionary of the given size, |
| 172 // associate it with a unique URL, add it to the manager through | 192 // associate it with a unique URL, add it to the manager through |
| 173 // SdchOwner::OnDictionaryFetched(), and return whether that | 193 // SdchOwner::OnDictionaryFetched(), and return whether that |
| 174 // addition was successful or not. | 194 // addition was successful or not. |
| 175 bool CreateAndAddDictionary(size_t size, std::string* server_hash_p) { | 195 bool CreateAndAddDictionary(size_t size, |
| 196 std::string* server_hash_p, |
| 197 base::Time last_used_time) { |
| 176 GURL dictionary_url( | 198 GURL dictionary_url( |
| 177 base::StringPrintf("%s/d%d", generic_url, dictionary_creation_index_)); | 199 base::StringPrintf("%s/d%d", generic_url, dictionary_creation_index_)); |
| 178 std::string dictionary_text(NewSdchDictionary(size - 4)); | 200 std::string dictionary_text(NewSdchDictionary(size - 4)); |
| 179 dictionary_text += base::StringPrintf("%04d", dictionary_creation_index_); | 201 dictionary_text += base::StringPrintf("%04d", dictionary_creation_index_); |
| 180 ++dictionary_creation_index_; | 202 ++dictionary_creation_index_; |
| 181 std::string client_hash; | 203 std::string client_hash; |
| 182 std::string server_hash; | 204 std::string server_hash; |
| 183 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); | 205 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); |
| 184 | 206 |
| 185 if (DictionaryPresentInManager(server_hash)) | 207 if (DictionaryPresentInManager(server_hash)) |
| 186 return false; | 208 return false; |
| 187 sdch_owner().OnDictionaryFetched(dictionary_text, dictionary_url, net_log_); | 209 sdch_owner().OnDictionaryFetched(last_used_time, 0, dictionary_text, |
| 210 dictionary_url, net_log_); |
| 188 if (server_hash_p) | 211 if (server_hash_p) |
| 189 *server_hash_p = server_hash; | 212 *server_hash_p = server_hash; |
| 190 return DictionaryPresentInManager(server_hash); | 213 return DictionaryPresentInManager(server_hash); |
| 191 } | 214 } |
| 192 | 215 |
| 193 private: | 216 private: |
| 194 int last_jobs_created_; | 217 int last_jobs_created_; |
| 195 BoundNetLog net_log_; | 218 BoundNetLog net_log_; |
| 196 int dictionary_creation_index_; | 219 int dictionary_creation_index_; |
| 197 | 220 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 213 | 236 |
| 214 // Fetch generated when empty. | 237 // Fetch generated when empty. |
| 215 GURL dict_url1(std::string(generic_url) + "/d1"); | 238 GURL dict_url1(std::string(generic_url) + "/d1"); |
| 216 EXPECT_EQ(0, JobsRecentlyCreated()); | 239 EXPECT_EQ(0, JobsRecentlyCreated()); |
| 217 SignalGetDictionaryAndClearJobs(request_url, dict_url1); | 240 SignalGetDictionaryAndClearJobs(request_url, dict_url1); |
| 218 EXPECT_EQ(1, JobsRecentlyCreated()); | 241 EXPECT_EQ(1, JobsRecentlyCreated()); |
| 219 | 242 |
| 220 // Fetch generated when half full. | 243 // Fetch generated when half full. |
| 221 GURL dict_url2(std::string(generic_url) + "/d2"); | 244 GURL dict_url2(std::string(generic_url) + "/d2"); |
| 222 std::string dictionary1(NewSdchDictionary(kMaxSizeForTesting / 2)); | 245 std::string dictionary1(NewSdchDictionary(kMaxSizeForTesting / 2)); |
| 223 sdch_owner().OnDictionaryFetched(dictionary1, dict_url1, bound_net_log()); | 246 sdch_owner().OnDictionaryFetched(base::Time::Now(), 1, dictionary1, dict_url1, |
| 247 bound_net_log()); |
| 224 EXPECT_EQ(0, JobsRecentlyCreated()); | 248 EXPECT_EQ(0, JobsRecentlyCreated()); |
| 225 SignalGetDictionaryAndClearJobs(request_url, dict_url2); | 249 SignalGetDictionaryAndClearJobs(request_url, dict_url2); |
| 226 EXPECT_EQ(1, JobsRecentlyCreated()); | 250 EXPECT_EQ(1, JobsRecentlyCreated()); |
| 227 | 251 |
| 228 // Fetch not generated when close to completely full. | 252 // Fetch not generated when close to completely full. |
| 229 GURL dict_url3(std::string(generic_url) + "/d3"); | 253 GURL dict_url3(std::string(generic_url) + "/d3"); |
| 230 std::string dictionary2(NewSdchDictionary( | 254 std::string dictionary2(NewSdchDictionary( |
| 231 (kMaxSizeForTesting / 2 - kMinFetchSpaceForTesting / 2))); | 255 (kMaxSizeForTesting / 2 - kMinFetchSpaceForTesting / 2))); |
| 232 sdch_owner().OnDictionaryFetched(dictionary2, dict_url2, bound_net_log()); | 256 sdch_owner().OnDictionaryFetched(base::Time::Now(), 1, dictionary2, dict_url2, |
| 257 bound_net_log()); |
| 233 EXPECT_EQ(0, JobsRecentlyCreated()); | 258 EXPECT_EQ(0, JobsRecentlyCreated()); |
| 234 SignalGetDictionaryAndClearJobs(request_url, dict_url3); | 259 SignalGetDictionaryAndClearJobs(request_url, dict_url3); |
| 235 EXPECT_EQ(0, JobsRecentlyCreated()); | 260 EXPECT_EQ(0, JobsRecentlyCreated()); |
| 236 } | 261 } |
| 237 | 262 |
| 238 // Make sure attempts to add dictionaries do what they should. | 263 // Make sure attempts to add dictionaries do what they should. |
| 239 TEST_F(SdchOwnerTest, OnDictionaryFetched_Fetching) { | 264 TEST_F(SdchOwnerTest, OnDictionaryFetched_Fetching) { |
| 240 GURL request_url(std::string(generic_url) + "/r1"); | 265 GURL request_url(std::string(generic_url) + "/r1"); |
| 241 std::string client_hash; | 266 std::string client_hash; |
| 242 std::string server_hash; | 267 std::string server_hash; |
| 243 | 268 |
| 269 // In the past, but still fresh for an unused dictionary. |
| 270 base::Time dictionary_last_used_time(base::Time::Now() - |
| 271 base::TimeDelta::FromMinutes(30)); |
| 272 |
| 244 // Add successful when empty. | 273 // Add successful when empty. |
| 245 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr)); | 274 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr, |
| 275 dictionary_last_used_time)); |
| 246 EXPECT_EQ(0, JobsRecentlyCreated()); | 276 EXPECT_EQ(0, JobsRecentlyCreated()); |
| 247 | 277 |
| 248 // Add successful when half full. | 278 // Add successful when half full. |
| 249 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr)); | 279 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr, |
| 280 dictionary_last_used_time)); |
| 250 EXPECT_EQ(0, JobsRecentlyCreated()); | 281 EXPECT_EQ(0, JobsRecentlyCreated()); |
| 251 | 282 |
| 252 // Add unsuccessful when full. | 283 // Add unsuccessful when full. |
| 253 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr)); | 284 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting / 2, nullptr, |
| 285 dictionary_last_used_time)); |
| 254 EXPECT_EQ(0, JobsRecentlyCreated()); | 286 EXPECT_EQ(0, JobsRecentlyCreated()); |
| 255 } | 287 } |
| 256 | 288 |
| 257 // Confirm auto-eviction happens if space is needed. | 289 // Confirm auto-eviction happens if space is needed. |
| 258 TEST_F(SdchOwnerTest, ConfirmAutoEviction) { | 290 TEST_F(SdchOwnerTest, ConfirmAutoEviction) { |
| 259 std::string server_hash_d1; | 291 std::string server_hash_d1; |
| 260 std::string server_hash_d2; | 292 std::string server_hash_d2; |
| 261 std::string server_hash_d3; | 293 std::string server_hash_d3; |
| 262 | 294 |
| 263 // Add two dictionaries, one recent, one more than a day in the past. | 295 // Add two dictionaries, one recent, one more than a day in the past. |
| 264 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1)); | 296 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); |
| 297 base::Time stale(base::Time::Now() - base::TimeDelta::FromHours(25)); |
| 265 | 298 |
| 266 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 299 EXPECT_TRUE( |
| 267 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 300 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1, fresh)); |
| 268 sdch_owner().SetClockForTesting(clock.Pass()); | 301 EXPECT_TRUE( |
| 269 | 302 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d2, stale)); |
| 270 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d2)); | |
| 271 | 303 |
| 272 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 304 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 273 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 305 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
| 274 | 306 |
| 275 // The addition of a new dictionary should succeed, evicting the old one. | 307 EXPECT_TRUE( |
| 276 clock.reset(new base::SimpleTestClock); | 308 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d3, fresh)); |
| 277 clock->SetNow(base::Time::Now()); | |
| 278 sdch_owner().SetClockForTesting(clock.Pass()); | |
| 279 | |
| 280 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d3)); | |
| 281 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 309 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 282 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 310 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
| 283 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 311 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
| 284 } | 312 } |
| 285 | 313 |
| 286 // Confirm auto-eviction happens if space is needed, with a more complicated | 314 // Confirm auto-eviction happens if space is needed, with a more complicated |
| 287 // situation | 315 // situation |
| 288 TEST_F(SdchOwnerTest, ConfirmAutoEviction_2) { | 316 TEST_F(SdchOwnerTest, ConfirmAutoEviction_2) { |
| 289 std::string server_hash_d1; | 317 std::string server_hash_d1; |
| 290 std::string server_hash_d2; | 318 std::string server_hash_d2; |
| 291 std::string server_hash_d3; | 319 std::string server_hash_d3; |
| 292 | 320 |
| 293 // Add dictionaries, one recent, two more than a day in the past that | 321 // Add dictionaries, one recent, two more than a day in the past that |
| 294 // between them add up to the space needed. | 322 // between them add up to the space needed. |
| 295 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1)); | 323 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); |
| 324 base::Time stale(base::Time::Now() - base::TimeDelta::FromHours(25)); |
| 325 EXPECT_TRUE( |
| 326 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d1, fresh)); |
| 296 | 327 |
| 297 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 328 EXPECT_TRUE( |
| 298 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 329 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2, stale)); |
| 299 sdch_owner().SetClockForTesting(clock.Pass()); | 330 EXPECT_TRUE( |
| 300 | 331 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3, stale)); |
| 301 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2)); | |
| 302 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3)); | |
| 303 | 332 |
| 304 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 333 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 305 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 334 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
| 306 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 335 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
| 307 | 336 |
| 308 // The addition of a new dictionary should succeed, evicting the old one. | |
| 309 clock.reset(new base::SimpleTestClock); | |
| 310 clock->SetNow(base::Time::Now()); | |
| 311 sdch_owner().SetClockForTesting(clock.Pass()); | |
| 312 | |
| 313 std::string server_hash_d4; | 337 std::string server_hash_d4; |
| 314 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4)); | 338 EXPECT_TRUE( |
| 339 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4, fresh)); |
| 315 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 340 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 316 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 341 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
| 317 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d3)); | 342 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d3)); |
| 318 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); | 343 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); |
| 319 } | 344 } |
| 320 | 345 |
| 321 // Confirm if only one dictionary needs to be evicted it's the oldest. | 346 // Confirm if only one dictionary needs to be evicted it's the oldest. |
| 322 TEST_F(SdchOwnerTest, ConfirmAutoEviction_Oldest) { | 347 TEST_F(SdchOwnerTest, ConfirmAutoEviction_Oldest) { |
| 323 std::string server_hash_d1; | 348 std::string server_hash_d1; |
| 324 std::string server_hash_d2; | 349 std::string server_hash_d2; |
| 325 std::string server_hash_d3; | 350 std::string server_hash_d3; |
| 326 | 351 |
| 327 // Add dictionaries, one recent, one two days in the past, and one | 352 // Add dictionaries, one recent, one two days in the past, and one |
| 328 // four days in the past. | 353 // four days in the past. |
| 329 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1)); | 354 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); |
| 355 base::Time stale_newer(base::Time::Now() - base::TimeDelta::FromHours(47)); |
| 356 base::Time stale_older(base::Time::Now() - base::TimeDelta::FromHours(71)); |
| 330 | 357 |
| 331 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 358 EXPECT_TRUE( |
| 332 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 359 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1, fresh)); |
| 333 sdch_owner().SetClockForTesting(clock.Pass()); | |
| 334 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2)); | |
| 335 | 360 |
| 336 clock.reset(new base::SimpleTestClock); | 361 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2, |
| 337 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(4)); | 362 stale_newer)); |
| 338 sdch_owner().SetClockForTesting(clock.Pass()); | 363 |
| 339 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3)); | 364 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3, |
| 365 stale_older)); |
| 340 | 366 |
| 341 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 367 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 342 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 368 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
| 343 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 369 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
| 344 | 370 |
| 345 // The addition of a new dictionary should succeed, evicting only the | 371 // The addition of a new dictionary should succeed, evicting only the |
| 346 // oldest one. | 372 // oldest one. |
| 347 clock.reset(new base::SimpleTestClock); | |
| 348 clock->SetNow(base::Time::Now()); | |
| 349 sdch_owner().SetClockForTesting(clock.Pass()); | |
| 350 | 373 |
| 351 std::string server_hash_d4; | 374 std::string server_hash_d4; |
| 352 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4)); | 375 EXPECT_TRUE( |
| 376 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4, fresh)); |
| 353 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 377 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 354 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 378 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
| 355 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d3)); | 379 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d3)); |
| 356 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); | 380 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); |
| 357 } | 381 } |
| 358 | 382 |
| 359 // Confirm using a dictionary changes eviction behavior properly. | 383 // Confirm using a dictionary changes eviction behavior properly. |
| 360 TEST_F(SdchOwnerTest, UseChangesEviction) { | 384 TEST_F(SdchOwnerTest, UseChangesEviction) { |
| 361 std::string server_hash_d1; | 385 std::string server_hash_d1; |
| 362 std::string server_hash_d2; | 386 std::string server_hash_d2; |
| 363 std::string server_hash_d3; | 387 std::string server_hash_d3; |
| 364 | 388 |
| 365 // Add dictionaries, one recent, one two days in the past, and one | 389 // Add dictionaries, one recent, one two days in the past, and one |
| 366 // four days in the past. | 390 // four days in the past. |
| 367 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1)); | 391 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); |
| 392 base::Time stale_newer(base::Time::Now() - base::TimeDelta::FromHours(47)); |
| 393 base::Time stale_older(base::Time::Now() - base::TimeDelta::FromHours(71)); |
| 368 | 394 |
| 369 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 395 EXPECT_TRUE( |
| 370 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 396 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1, fresh)); |
| 371 sdch_owner().SetClockForTesting(clock.Pass()); | |
| 372 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2)); | |
| 373 | 397 |
| 374 clock.reset(new base::SimpleTestClock); | 398 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2, |
| 375 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(4)); | 399 stale_newer)); |
| 376 sdch_owner().SetClockForTesting(clock.Pass()); | 400 |
| 377 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3)); | 401 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3, |
| 402 stale_older)); |
| 378 | 403 |
| 379 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 404 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 380 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 405 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
| 381 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 406 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
| 382 | 407 |
| 383 clock.reset(new base::SimpleTestClock); | |
| 384 clock->SetNow(base::Time::Now()); | |
| 385 sdch_owner().SetClockForTesting(clock.Pass()); | |
| 386 | |
| 387 // Use the oldest dictionary. | 408 // Use the oldest dictionary. |
| 388 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d3); | 409 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d3); |
| 389 | 410 |
| 390 // The addition of a new dictionary should succeed, evicting only the | 411 // The addition of a new dictionary should succeed, evicting only the |
| 391 // newer stale one. | 412 // newer stale one. |
| 392 std::string server_hash_d4; | 413 std::string server_hash_d4; |
| 393 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4)); | 414 EXPECT_TRUE( |
| 415 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4, fresh)); |
| 394 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 416 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 395 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 417 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
| 396 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 418 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
| 397 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); | 419 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d4)); |
| 398 } | 420 } |
| 399 | 421 |
| 400 // Confirm using a dictionary can prevent the addition of a new dictionary. | 422 // Confirm using a dictionary can prevent the addition of a new dictionary. |
| 401 TEST_F(SdchOwnerTest, UsePreventsAddition) { | 423 TEST_F(SdchOwnerTest, UsePreventsAddition) { |
| 402 std::string server_hash_d1; | 424 std::string server_hash_d1; |
| 403 std::string server_hash_d2; | 425 std::string server_hash_d2; |
| 404 std::string server_hash_d3; | 426 std::string server_hash_d3; |
| 405 | 427 |
| 406 // Add dictionaries, one recent, one two days in the past, and one | 428 // Add dictionaries, one recent, one two days in the past, and one |
| 407 // four days in the past. | 429 // four days in the past. |
| 408 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1)); | 430 base::Time fresh(base::Time::Now() - base::TimeDelta::FromMinutes(30)); |
| 431 base::Time stale_newer(base::Time::Now() - base::TimeDelta::FromHours(47)); |
| 432 base::Time stale_older(base::Time::Now() - base::TimeDelta::FromHours(71)); |
| 409 | 433 |
| 410 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | 434 EXPECT_TRUE( |
| 411 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(2)); | 435 CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d1, fresh)); |
| 412 sdch_owner().SetClockForTesting(clock.Pass()); | |
| 413 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2)); | |
| 414 | 436 |
| 415 clock.reset(new base::SimpleTestClock); | 437 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d2, |
| 416 clock->SetNow(base::Time::Now() - base::TimeDelta::FromDays(4)); | 438 stale_newer)); |
| 417 sdch_owner().SetClockForTesting(clock.Pass()); | 439 |
| 418 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3)); | 440 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 4, &server_hash_d3, |
| 441 stale_older)); |
| 419 | 442 |
| 420 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 443 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 421 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 444 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
| 422 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 445 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
| 423 | 446 |
| 424 clock.reset(new base::SimpleTestClock); | |
| 425 clock->SetNow(base::Time::Now()); | |
| 426 sdch_owner().SetClockForTesting(clock.Pass()); | |
| 427 | |
| 428 // Use the older dictionaries. | 447 // Use the older dictionaries. |
| 429 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d2); | 448 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d2); |
| 430 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d3); | 449 sdch_owner().OnDictionaryUsed(&sdch_manager(), server_hash_d3); |
| 431 | 450 |
| 432 // The addition of a new dictionary should fail, not evicting anything. | 451 // The addition of a new dictionary should fail, not evicting anything. |
| 433 std::string server_hash_d4; | 452 std::string server_hash_d4; |
| 434 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4)); | 453 EXPECT_FALSE( |
| 454 CreateAndAddDictionary(kMaxSizeForTesting / 2, &server_hash_d4, fresh)); |
| 435 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 455 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 436 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); | 456 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d2)); |
| 437 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); | 457 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d3)); |
| 438 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d4)); | 458 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d4)); |
| 439 } | 459 } |
| 440 | 460 |
| 441 // Confirm clear gets all the space back. | 461 // Confirm clear gets all the space back. |
| 442 TEST_F(SdchOwnerTest, ClearReturnsSpace) { | 462 TEST_F(SdchOwnerTest, ClearReturnsSpace) { |
| 443 std::string server_hash_d1; | 463 std::string server_hash_d1; |
| 444 std::string server_hash_d2; | 464 std::string server_hash_d2; |
| 445 | 465 |
| 446 // Take up all the space. | 466 // Take up all the space. |
| 447 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d1)); | 467 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d1, |
| 468 base::Time::Now())); |
| 448 | 469 |
| 449 // Addition should fail. | 470 // Addition should fail. |
| 450 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d2)); | 471 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d2, |
| 472 base::Time::Now())); |
| 451 | 473 |
| 452 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 474 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 453 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 475 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
| 454 | 476 |
| 455 sdch_manager().ClearData(); | 477 sdch_manager().ClearData(); |
| 456 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d1)); | 478 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d1)); |
| 457 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 479 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
| 458 | 480 |
| 459 // Addition should now succeed. | 481 // Addition should now succeed. |
| 460 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, nullptr)); | 482 EXPECT_TRUE( |
| 483 CreateAndAddDictionary(kMaxSizeForTesting, nullptr, base::Time::Now())); |
| 461 } | 484 } |
| 462 | 485 |
| 463 // Confirm memory pressure gets all the space back. | 486 // Confirm memory pressure gets all the space back. |
| 464 TEST_F(SdchOwnerTest, MemoryPressureReturnsSpace) { | 487 TEST_F(SdchOwnerTest, MemoryPressureReturnsSpace) { |
| 465 std::string server_hash_d1; | 488 std::string server_hash_d1; |
| 466 std::string server_hash_d2; | 489 std::string server_hash_d2; |
| 467 | 490 |
| 468 // Take up all the space. | 491 // Take up all the space. |
| 469 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d1)); | 492 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d1, |
| 493 base::Time::Now())); |
| 470 | 494 |
| 471 // Addition should fail. | 495 // Addition should fail. |
| 472 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d2)); | 496 EXPECT_FALSE(CreateAndAddDictionary(kMaxSizeForTesting, &server_hash_d2, |
| 497 base::Time::Now())); |
| 473 | 498 |
| 474 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); | 499 EXPECT_TRUE(DictionaryPresentInManager(server_hash_d1)); |
| 475 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 500 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
| 476 | 501 |
| 477 base::MemoryPressureListener::NotifyMemoryPressure( | 502 base::MemoryPressureListener::NotifyMemoryPressure( |
| 478 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | 503 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 479 // The notification may have (implementation note: does :-}) use a PostTask, | 504 // The notification may have (implementation note: does :-}) use a PostTask, |
| 480 // so we drain the local message queue. This should be safe (i.e. not have | 505 // so we drain the local message queue. This should be safe (i.e. not have |
| 481 // an inifinite number of messages) in a unit test. | 506 // an inifinite number of messages) in a unit test. |
| 482 base::RunLoop().RunUntilIdle(); | 507 base::RunLoop().RunUntilIdle(); |
| 483 | 508 |
| 484 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d1)); | 509 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d1)); |
| 485 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); | 510 EXPECT_FALSE(DictionaryPresentInManager(server_hash_d2)); |
| 486 | 511 |
| 487 // Addition should now succeed. | 512 // Addition should now succeed. |
| 488 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting, nullptr)); | 513 EXPECT_TRUE( |
| 514 CreateAndAddDictionary(kMaxSizeForTesting, nullptr, base::Time::Now())); |
| 489 } | 515 } |
| 490 | 516 |
| 491 } // namespace net | 517 } // namespace net |
| OLD | NEW |