| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <time.h> | 5 #include <time.h> |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 private: | 53 private: |
| 54 DnsMaster* master_; | 54 DnsMaster* master_; |
| 55 const NameList hosts_; | 55 const NameList hosts_; |
| 56 HelperTimer* timer_; | 56 HelperTimer* timer_; |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 class DnsMasterTest : public testing::Test { | 59 class DnsMasterTest : public testing::Test { |
| 60 public: | 60 public: |
| 61 DnsMasterTest() | 61 DnsMasterTest() |
| 62 : mapper_(new net::RuleBasedHostMapper()), | 62 : mapper_(new net::RuleBasedHostMapper()), |
| 63 default_max_queueing_delay_(TimeDelta::FromMilliseconds( |
| 64 DnsPrefetcherInit::kMaxQueueingDelayMs)), |
| 63 scoped_mapper_(mapper_.get()) { | 65 scoped_mapper_(mapper_.get()) { |
| 64 } | 66 } |
| 65 | 67 |
| 66 protected: | 68 protected: |
| 67 virtual void SetUp() { | 69 virtual void SetUp() { |
| 68 #if defined(OS_WIN) | 70 #if defined(OS_WIN) |
| 69 net::EnsureWinsockInit(); | 71 net::EnsureWinsockInit(); |
| 70 #endif | 72 #endif |
| 71 mapper_->AddRuleWithLatency("www.google.com", "127.0.0.1", 50); | 73 mapper_->AddRuleWithLatency("www.google.com", "127.0.0.1", 50); |
| 72 mapper_->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70); | 74 mapper_->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70); |
| 73 mapper_->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44); | 75 mapper_->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44); |
| 74 mapper_->AddRuleWithLatency("gmail.com", "127.0.0.1", 63); | 76 mapper_->AddRuleWithLatency("gmail.com", "127.0.0.1", 63); |
| 75 } | 77 } |
| 76 | 78 |
| 77 void WaitForResolution(DnsMaster* master, const NameList& hosts) { | 79 void WaitForResolution(DnsMaster* master, const NameList& hosts) { |
| 78 HelperTimer* timer = new HelperTimer(); | 80 HelperTimer* timer = new HelperTimer(); |
| 79 timer->Start(TimeDelta::FromMilliseconds(100), | 81 timer->Start(TimeDelta::FromMilliseconds(100), |
| 80 new WaitForResolutionHelper(master, hosts, timer), | 82 new WaitForResolutionHelper(master, hosts, timer), |
| 81 &WaitForResolutionHelper::Run); | 83 &WaitForResolutionHelper::Run); |
| 82 MessageLoop::current()->Run(); | 84 MessageLoop::current()->Run(); |
| 83 } | 85 } |
| 84 | 86 |
| 85 scoped_refptr<net::RuleBasedHostMapper> mapper_; | 87 scoped_refptr<net::RuleBasedHostMapper> mapper_; |
| 86 | 88 |
| 89 // Shorthand to access TimeDelta of DnsPrefetcherInit::kMaxQueueingDelayMs. |
| 90 // (It would be a static constant... except style rules preclude that :-/ ). |
| 91 const TimeDelta default_max_queueing_delay_; |
| 92 |
| 87 private: | 93 private: |
| 88 MessageLoop loop; | 94 MessageLoop loop; |
| 89 net::ScopedHostMapper scoped_mapper_; | 95 net::ScopedHostMapper scoped_mapper_; |
| 90 }; | 96 }; |
| 91 | 97 |
| 92 //------------------------------------------------------------------------------ | 98 //------------------------------------------------------------------------------ |
| 93 // Provide a function to create unique (nonexistant) domains at *every* call. | 99 // Provide a function to create unique (nonexistant) domains at *every* call. |
| 94 //------------------------------------------------------------------------------ | 100 //------------------------------------------------------------------------------ |
| 95 static std::string GetNonexistantDomain() { | 101 static std::string GetNonexistantDomain() { |
| 96 static std::string postfix = ".google.com"; | 102 static std::string postfix = ".google.com"; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 if (all_lookups >= 10) | 162 if (all_lookups >= 10) |
| 157 if (lookups_with_improvement * 100 > all_lookups * 75) | 163 if (lookups_with_improvement * 100 > all_lookups * 75) |
| 158 // Okay, we see the improvement for more than 75% of all lookups. | 164 // Okay, we see the improvement for more than 75% of all lookups. |
| 159 return; | 165 return; |
| 160 } | 166 } |
| 161 FAIL() << "No substantial improvement in lookup time."; | 167 FAIL() << "No substantial improvement in lookup time."; |
| 162 } | 168 } |
| 163 | 169 |
| 164 TEST_F(DnsMasterTest, StartupShutdownTest) { | 170 TEST_F(DnsMasterTest, StartupShutdownTest) { |
| 165 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, | 171 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, |
| 166 MessageLoop::current(), DnsPrefetcherInit::kMaxConcurrentLookups); | 172 MessageLoop::current(), default_max_queueing_delay_, |
| 173 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 167 testing_master->Shutdown(); | 174 testing_master->Shutdown(); |
| 168 } | 175 } |
| 169 | 176 |
| 170 TEST_F(DnsMasterTest, BenefitLookupTest) { | 177 TEST_F(DnsMasterTest, BenefitLookupTest) { |
| 171 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, | 178 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, |
| 172 MessageLoop::current(), DnsPrefetcherInit::kMaxConcurrentLookups); | 179 MessageLoop::current(), default_max_queueing_delay_, |
| 180 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 173 | 181 |
| 174 std::string goog("www.google.com"), | 182 std::string goog("www.google.com"), |
| 175 goog2("gmail.google.com.com"), | 183 goog2("gmail.google.com.com"), |
| 176 goog3("mail.google.com"), | 184 goog3("mail.google.com"), |
| 177 goog4("gmail.com"); | 185 goog4("gmail.com"); |
| 178 DnsHostInfo goog_info, goog2_info, goog3_info, goog4_info; | 186 DnsHostInfo goog_info, goog2_info, goog3_info, goog4_info; |
| 179 | 187 |
| 180 // Simulate getting similar names from a network observer | 188 // Simulate getting similar names from a network observer |
| 181 goog_info.SetHostname(goog); | 189 goog_info.SetHostname(goog); |
| 182 goog2_info.SetHostname(goog2); | 190 goog2_info.SetHostname(goog2); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 EXPECT_FALSE(testing_master->AccruePrefetchBenefits(GURL(), &goog4_info)); | 233 EXPECT_FALSE(testing_master->AccruePrefetchBenefits(GURL(), &goog4_info)); |
| 226 | 234 |
| 227 testing_master->Shutdown(); | 235 testing_master->Shutdown(); |
| 228 } | 236 } |
| 229 | 237 |
| 230 TEST_F(DnsMasterTest, ShutdownWhenResolutionIsPendingTest) { | 238 TEST_F(DnsMasterTest, ShutdownWhenResolutionIsPendingTest) { |
| 231 scoped_refptr<net::WaitingHostMapper> mapper = new net::WaitingHostMapper(); | 239 scoped_refptr<net::WaitingHostMapper> mapper = new net::WaitingHostMapper(); |
| 232 net::ScopedHostMapper scoped_mapper(mapper.get()); | 240 net::ScopedHostMapper scoped_mapper(mapper.get()); |
| 233 | 241 |
| 234 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, | 242 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, |
| 235 MessageLoop::current(), DnsPrefetcherInit::kMaxConcurrentLookups); | 243 MessageLoop::current(), default_max_queueing_delay_, |
| 244 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 236 | 245 |
| 237 std::string localhost("127.0.0.1"); | 246 std::string localhost("127.0.0.1"); |
| 238 NameList names; | 247 NameList names; |
| 239 names.insert(names.end(), localhost); | 248 names.insert(names.end(), localhost); |
| 240 | 249 |
| 241 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); | 250 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); |
| 242 | 251 |
| 243 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 252 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 244 new MessageLoop::QuitTask(), 500); | 253 new MessageLoop::QuitTask(), 500); |
| 245 MessageLoop::current()->Run(); | 254 MessageLoop::current()->Run(); |
| 246 | 255 |
| 247 EXPECT_FALSE(testing_master->WasFound(localhost)); | 256 EXPECT_FALSE(testing_master->WasFound(localhost)); |
| 248 | 257 |
| 249 testing_master->Shutdown(); | 258 testing_master->Shutdown(); |
| 250 | 259 |
| 251 // Clean up after ourselves. | 260 // Clean up after ourselves. |
| 252 mapper->Signal(); | 261 mapper->Signal(); |
| 253 MessageLoop::current()->RunAllPending(); | 262 MessageLoop::current()->RunAllPending(); |
| 254 } | 263 } |
| 255 | 264 |
| 256 TEST_F(DnsMasterTest, SingleLookupTest) { | 265 TEST_F(DnsMasterTest, SingleLookupTest) { |
| 257 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, | 266 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, |
| 258 MessageLoop::current(), DnsPrefetcherInit::kMaxConcurrentLookups); | 267 MessageLoop::current(), default_max_queueing_delay_, |
| 268 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 259 | 269 |
| 260 std::string goog("www.google.com"); | 270 std::string goog("www.google.com"); |
| 261 | 271 |
| 262 NameList names; | 272 NameList names; |
| 263 names.insert(names.end(), goog); | 273 names.insert(names.end(), goog); |
| 264 | 274 |
| 265 // Try to flood the master with many concurrent requests. | 275 // Try to flood the master with many concurrent requests. |
| 266 for (int i = 0; i < 10; i++) | 276 for (int i = 0; i < 10; i++) |
| 267 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); | 277 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); |
| 268 | 278 |
| 269 WaitForResolution(testing_master, names); | 279 WaitForResolution(testing_master, names); |
| 270 | 280 |
| 271 EXPECT_TRUE(testing_master->WasFound(goog)); | 281 EXPECT_TRUE(testing_master->WasFound(goog)); |
| 272 | 282 |
| 273 MessageLoop::current()->RunAllPending(); | 283 MessageLoop::current()->RunAllPending(); |
| 274 | 284 |
| 275 EXPECT_GT(testing_master->peak_pending_lookups(), names.size() / 2); | 285 EXPECT_GT(testing_master->peak_pending_lookups(), names.size() / 2); |
| 276 EXPECT_LE(testing_master->peak_pending_lookups(), names.size()); | 286 EXPECT_LE(testing_master->peak_pending_lookups(), names.size()); |
| 277 EXPECT_LE(testing_master->peak_pending_lookups(), | 287 EXPECT_LE(testing_master->peak_pending_lookups(), |
| 278 DnsPrefetcherInit::kMaxConcurrentLookups); | 288 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 279 | 289 |
| 280 testing_master->Shutdown(); | 290 testing_master->Shutdown(); |
| 281 } | 291 } |
| 282 | 292 |
| 283 TEST_F(DnsMasterTest, ConcurrentLookupTest) { | 293 TEST_F(DnsMasterTest, ConcurrentLookupTest) { |
| 284 mapper_->AddSimulatedFailure("*.notfound"); | 294 mapper_->AddSimulatedFailure("*.notfound"); |
| 285 | 295 |
| 286 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, | 296 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, |
| 287 MessageLoop::current(), DnsPrefetcherInit::kMaxConcurrentLookups); | 297 MessageLoop::current(), default_max_queueing_delay_, |
| 298 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 288 | 299 |
| 289 std::string goog("www.google.com"), | 300 std::string goog("www.google.com"), |
| 290 goog2("gmail.google.com.com"), | 301 goog2("gmail.google.com.com"), |
| 291 goog3("mail.google.com"), | 302 goog3("mail.google.com"), |
| 292 goog4("gmail.com"); | 303 goog4("gmail.com"); |
| 293 std::string bad1("bad1.notfound"), | 304 std::string bad1("bad1.notfound"), |
| 294 bad2("bad2.notfound"); | 305 bad2("bad2.notfound"); |
| 295 | 306 |
| 296 NameList names; | 307 NameList names; |
| 297 names.insert(names.end(), goog); | 308 names.insert(names.end(), goog); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 EXPECT_LE(testing_master->peak_pending_lookups(), | 342 EXPECT_LE(testing_master->peak_pending_lookups(), |
| 332 DnsPrefetcherInit::kMaxConcurrentLookups); | 343 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 333 | 344 |
| 334 testing_master->Shutdown(); | 345 testing_master->Shutdown(); |
| 335 } | 346 } |
| 336 | 347 |
| 337 TEST_F(DnsMasterTest, DISABLED_MassiveConcurrentLookupTest) { | 348 TEST_F(DnsMasterTest, DISABLED_MassiveConcurrentLookupTest) { |
| 338 mapper_->AddSimulatedFailure("*.notfound"); | 349 mapper_->AddSimulatedFailure("*.notfound"); |
| 339 | 350 |
| 340 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, | 351 scoped_refptr<DnsMaster> testing_master = new DnsMaster(new net::HostResolver, |
| 341 MessageLoop::current(), DnsPrefetcherInit::kMaxConcurrentLookups); | 352 MessageLoop::current(), default_max_queueing_delay_, |
| 353 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 342 | 354 |
| 343 NameList names; | 355 NameList names; |
| 344 for (int i = 0; i < 100; i++) | 356 for (int i = 0; i < 100; i++) |
| 345 names.push_back("host" + IntToString(i) + ".notfound"); | 357 names.push_back("host" + IntToString(i) + ".notfound"); |
| 346 | 358 |
| 347 // Try to flood the master with many concurrent requests. | 359 // Try to flood the master with many concurrent requests. |
| 348 for (int i = 0; i < 10; i++) | 360 for (int i = 0; i < 10; i++) |
| 349 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); | 361 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); |
| 350 | 362 |
| 351 WaitForResolution(testing_master, names); | 363 WaitForResolution(testing_master, names); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 ++i; // Skip latency value. | 447 ++i; // Skip latency value. |
| 436 } | 448 } |
| 437 return kLatencyNotFound; | 449 return kLatencyNotFound; |
| 438 } | 450 } |
| 439 | 451 |
| 440 //------------------------------------------------------------------------------ | 452 //------------------------------------------------------------------------------ |
| 441 | 453 |
| 442 // Make sure nil referral lists really have no entries, and no latency listed. | 454 // Make sure nil referral lists really have no entries, and no latency listed. |
| 443 TEST_F(DnsMasterTest, ReferrerSerializationNilTest) { | 455 TEST_F(DnsMasterTest, ReferrerSerializationNilTest) { |
| 444 scoped_refptr<DnsMaster> master = new DnsMaster(new net::HostResolver, | 456 scoped_refptr<DnsMaster> master = new DnsMaster(new net::HostResolver, |
| 445 MessageLoop::current(), DnsPrefetcherInit::kMaxConcurrentLookups); | 457 MessageLoop::current(), default_max_queueing_delay_, |
| 458 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 446 ListValue referral_list; | 459 ListValue referral_list; |
| 447 master->SerializeReferrers(&referral_list); | 460 master->SerializeReferrers(&referral_list); |
| 448 EXPECT_EQ(0U, referral_list.GetSize()); | 461 EXPECT_EQ(0U, referral_list.GetSize()); |
| 449 EXPECT_EQ(kLatencyNotFound, GetLatencyFromSerialization("a.com", "b.com", | 462 EXPECT_EQ(kLatencyNotFound, GetLatencyFromSerialization("a.com", "b.com", |
| 450 referral_list)); | 463 referral_list)); |
| 451 | 464 |
| 452 master->Shutdown(); | 465 master->Shutdown(); |
| 453 } | 466 } |
| 454 | 467 |
| 455 // Make sure that when a serialization list includes a value, that it can be | 468 // Make sure that when a serialization list includes a value, that it can be |
| 456 // deserialized into the database, and can be extracted back out via | 469 // deserialized into the database, and can be extracted back out via |
| 457 // serialization without being changed. | 470 // serialization without being changed. |
| 458 TEST_F(DnsMasterTest, ReferrerSerializationSingleReferrerTest) { | 471 TEST_F(DnsMasterTest, ReferrerSerializationSingleReferrerTest) { |
| 459 scoped_refptr<DnsMaster> master = new DnsMaster(new net::HostResolver, | 472 scoped_refptr<DnsMaster> master = new DnsMaster(new net::HostResolver, |
| 460 MessageLoop::current(), DnsPrefetcherInit::kMaxConcurrentLookups); | 473 MessageLoop::current(), default_max_queueing_delay_, |
| 474 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 461 std::string motivation_hostname = "www.google.com"; | 475 std::string motivation_hostname = "www.google.com"; |
| 462 std::string subresource_hostname = "icons.google.com"; | 476 std::string subresource_hostname = "icons.google.com"; |
| 463 const int kLatency = 3; | 477 const int kLatency = 3; |
| 464 ListValue referral_list; | 478 ListValue referral_list; |
| 465 | 479 |
| 466 AddToSerializedList(motivation_hostname, subresource_hostname, kLatency, | 480 AddToSerializedList(motivation_hostname, subresource_hostname, kLatency, |
| 467 &referral_list); | 481 &referral_list); |
| 468 | 482 |
| 469 master->DeserializeReferrers(referral_list); | 483 master->DeserializeReferrers(referral_list); |
| 470 | 484 |
| 471 ListValue recovered_referral_list; | 485 ListValue recovered_referral_list; |
| 472 master->SerializeReferrers(&recovered_referral_list); | 486 master->SerializeReferrers(&recovered_referral_list); |
| 473 EXPECT_EQ(1U, recovered_referral_list.GetSize()); | 487 EXPECT_EQ(1U, recovered_referral_list.GetSize()); |
| 474 EXPECT_EQ(kLatency, GetLatencyFromSerialization(motivation_hostname, | 488 EXPECT_EQ(kLatency, GetLatencyFromSerialization(motivation_hostname, |
| 475 subresource_hostname, | 489 subresource_hostname, |
| 476 recovered_referral_list)); | 490 recovered_referral_list)); |
| 477 | 491 |
| 478 master->Shutdown(); | 492 master->Shutdown(); |
| 479 } | 493 } |
| 480 | 494 |
| 481 // Make sure the Trim() functionality works as expected. | 495 // Make sure the Trim() functionality works as expected. |
| 482 TEST_F(DnsMasterTest, ReferrerSerializationTrimTest) { | 496 TEST_F(DnsMasterTest, ReferrerSerializationTrimTest) { |
| 483 scoped_refptr<DnsMaster> master = new DnsMaster(new net::HostResolver, | 497 scoped_refptr<DnsMaster> master = new DnsMaster(new net::HostResolver, |
| 484 MessageLoop::current(), DnsPrefetcherInit::kMaxConcurrentLookups); | 498 MessageLoop::current(), default_max_queueing_delay_, |
| 499 DnsPrefetcherInit::kMaxConcurrentLookups); |
| 485 std::string motivation_hostname = "www.google.com"; | 500 std::string motivation_hostname = "www.google.com"; |
| 486 std::string icon_subresource_hostname = "icons.google.com"; | 501 std::string icon_subresource_hostname = "icons.google.com"; |
| 487 std::string img_subresource_hostname = "img.google.com"; | 502 std::string img_subresource_hostname = "img.google.com"; |
| 488 ListValue referral_list; | 503 ListValue referral_list; |
| 489 | 504 |
| 490 AddToSerializedList(motivation_hostname, icon_subresource_hostname, 10, | 505 AddToSerializedList(motivation_hostname, icon_subresource_hostname, 10, |
| 491 &referral_list); | 506 &referral_list); |
| 492 AddToSerializedList(motivation_hostname, img_subresource_hostname, 3, | 507 AddToSerializedList(motivation_hostname, img_subresource_hostname, 3, |
| 493 &referral_list); | 508 &referral_list); |
| 494 | 509 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 EXPECT_EQ(queue.Pop(), "startup"); | 623 EXPECT_EQ(queue.Pop(), "startup"); |
| 609 EXPECT_EQ(queue.Pop(), "omni"); | 624 EXPECT_EQ(queue.Pop(), "omni"); |
| 610 | 625 |
| 611 EXPECT_TRUE(queue.IsEmpty()); | 626 EXPECT_TRUE(queue.IsEmpty()); |
| 612 } | 627 } |
| 613 | 628 |
| 614 | 629 |
| 615 | 630 |
| 616 | 631 |
| 617 } // namespace chrome_browser_net | 632 } // namespace chrome_browser_net |
| OLD | NEW |