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 |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/timer.h" | 14 #include "base/timer.h" |
| 15 #include "chrome/browser/chrome_thread.h" |
15 #include "chrome/browser/net/dns_global.h" | 16 #include "chrome/browser/net/dns_global.h" |
16 #include "chrome/browser/net/dns_host_info.h" | 17 #include "chrome/browser/net/dns_host_info.h" |
17 #include "chrome/common/net/dns.h" | 18 #include "chrome/common/net/dns.h" |
18 #include "net/base/address_list.h" | 19 #include "net/base/address_list.h" |
19 #include "net/base/mock_host_resolver.h" | 20 #include "net/base/mock_host_resolver.h" |
20 #include "net/base/winsock_init.h" | 21 #include "net/base/winsock_init.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 using base::Time; | 24 using base::Time; |
24 using base::TimeDelta; | 25 using base::TimeDelta; |
(...skipping 27 matching lines...) Expand all Loading... |
52 | 53 |
53 private: | 54 private: |
54 DnsMaster* master_; | 55 DnsMaster* master_; |
55 const NameList hosts_; | 56 const NameList hosts_; |
56 HelperTimer* timer_; | 57 HelperTimer* timer_; |
57 }; | 58 }; |
58 | 59 |
59 class DnsMasterTest : public testing::Test { | 60 class DnsMasterTest : public testing::Test { |
60 public: | 61 public: |
61 DnsMasterTest() | 62 DnsMasterTest() |
62 : host_resolver_(new net::MockCachingHostResolver()), | 63 : io_thread_(ChromeThread::IO, &loop_), |
| 64 host_resolver_(new net::MockCachingHostResolver()), |
63 default_max_queueing_delay_(TimeDelta::FromMilliseconds( | 65 default_max_queueing_delay_(TimeDelta::FromMilliseconds( |
64 DnsPrefetcherInit::kMaxQueueingDelayMs)) { | 66 DnsPrefetcherInit::kMaxQueueingDelayMs)) { |
65 } | 67 } |
66 | 68 |
67 protected: | 69 protected: |
68 virtual void SetUp() { | 70 virtual void SetUp() { |
69 #if defined(OS_WIN) | 71 #if defined(OS_WIN) |
70 net::EnsureWinsockInit(); | 72 net::EnsureWinsockInit(); |
71 #endif | 73 #endif |
72 // Since we are using a caching HostResolver, the following latencies will | 74 // Since we are using a caching HostResolver, the following latencies will |
(...skipping 11 matching lines...) Expand all Loading... |
84 timer->Start(TimeDelta::FromMilliseconds(100), | 86 timer->Start(TimeDelta::FromMilliseconds(100), |
85 new WaitForResolutionHelper(master, hosts, timer), | 87 new WaitForResolutionHelper(master, hosts, timer), |
86 &WaitForResolutionHelper::Run); | 88 &WaitForResolutionHelper::Run); |
87 MessageLoop::current()->Run(); | 89 MessageLoop::current()->Run(); |
88 } | 90 } |
89 | 91 |
90 private: | 92 private: |
91 // IMPORTANT: do not move this below |host_resolver_|; the host resolver | 93 // IMPORTANT: do not move this below |host_resolver_|; the host resolver |
92 // must not outlive the message loop, otherwise bad things can happen | 94 // must not outlive the message loop, otherwise bad things can happen |
93 // (like posting to a deleted message loop). | 95 // (like posting to a deleted message loop). |
94 MessageLoop loop; | 96 MessageLoop loop_; |
| 97 ChromeThread io_thread_; |
95 | 98 |
96 protected: | 99 protected: |
97 scoped_refptr<net::MockCachingHostResolver> host_resolver_; | 100 scoped_refptr<net::MockCachingHostResolver> host_resolver_; |
98 | 101 |
99 // Shorthand to access TimeDelta of DnsPrefetcherInit::kMaxQueueingDelayMs. | 102 // Shorthand to access TimeDelta of DnsPrefetcherInit::kMaxQueueingDelayMs. |
100 // (It would be a static constant... except style rules preclude that :-/ ). | 103 // (It would be a static constant... except style rules preclude that :-/ ). |
101 const TimeDelta default_max_queueing_delay_; | 104 const TimeDelta default_max_queueing_delay_; |
102 }; | 105 }; |
103 | 106 |
104 //------------------------------------------------------------------------------ | 107 //------------------------------------------------------------------------------ |
105 | 108 |
106 TEST_F(DnsMasterTest, StartupShutdownTest) { | 109 TEST_F(DnsMasterTest, StartupShutdownTest) { |
107 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, | 110 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, |
108 MessageLoop::current(), default_max_queueing_delay_, | 111 default_max_queueing_delay_, DnsPrefetcherInit::kMaxConcurrentLookups); |
109 DnsPrefetcherInit::kMaxConcurrentLookups); | |
110 testing_master->Shutdown(); | 112 testing_master->Shutdown(); |
111 } | 113 } |
112 | 114 |
113 TEST_F(DnsMasterTest, BenefitLookupTest) { | 115 TEST_F(DnsMasterTest, BenefitLookupTest) { |
114 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, | 116 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, |
115 MessageLoop::current(), default_max_queueing_delay_, | 117 default_max_queueing_delay_, DnsPrefetcherInit::kMaxConcurrentLookups); |
116 DnsPrefetcherInit::kMaxConcurrentLookups); | |
117 | 118 |
118 std::string goog("www.google.com"), | 119 std::string goog("www.google.com"), |
119 goog2("gmail.google.com.com"), | 120 goog2("gmail.google.com.com"), |
120 goog3("mail.google.com"), | 121 goog3("mail.google.com"), |
121 goog4("gmail.com"); | 122 goog4("gmail.com"); |
122 DnsHostInfo goog_info, goog2_info, goog3_info, goog4_info; | 123 DnsHostInfo goog_info, goog2_info, goog3_info, goog4_info; |
123 | 124 |
124 // Simulate getting similar names from a network observer | 125 // Simulate getting similar names from a network observer |
125 goog_info.SetHostname(goog); | 126 goog_info.SetHostname(goog); |
126 goog2_info.SetHostname(goog2); | 127 goog2_info.SetHostname(goog2); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 171 |
171 testing_master->Shutdown(); | 172 testing_master->Shutdown(); |
172 } | 173 } |
173 | 174 |
174 TEST_F(DnsMasterTest, ShutdownWhenResolutionIsPendingTest) { | 175 TEST_F(DnsMasterTest, ShutdownWhenResolutionIsPendingTest) { |
175 scoped_refptr<net::WaitingHostResolverProc> resolver_proc = | 176 scoped_refptr<net::WaitingHostResolverProc> resolver_proc = |
176 new net::WaitingHostResolverProc(NULL); | 177 new net::WaitingHostResolverProc(NULL); |
177 host_resolver_->Reset(resolver_proc); | 178 host_resolver_->Reset(resolver_proc); |
178 | 179 |
179 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, | 180 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, |
180 MessageLoop::current(), default_max_queueing_delay_, | 181 default_max_queueing_delay_, DnsPrefetcherInit::kMaxConcurrentLookups); |
181 DnsPrefetcherInit::kMaxConcurrentLookups); | |
182 | 182 |
183 std::string localhost("127.0.0.1"); | 183 std::string localhost("127.0.0.1"); |
184 NameList names; | 184 NameList names; |
185 names.insert(names.end(), localhost); | 185 names.insert(names.end(), localhost); |
186 | 186 |
187 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); | 187 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); |
188 | 188 |
189 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 189 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
190 new MessageLoop::QuitTask(), 500); | 190 new MessageLoop::QuitTask(), 500); |
191 MessageLoop::current()->Run(); | 191 MessageLoop::current()->Run(); |
192 | 192 |
193 EXPECT_FALSE(testing_master->WasFound(localhost)); | 193 EXPECT_FALSE(testing_master->WasFound(localhost)); |
194 | 194 |
195 testing_master->Shutdown(); | 195 testing_master->Shutdown(); |
196 | 196 |
197 // Clean up after ourselves. | 197 // Clean up after ourselves. |
198 resolver_proc->Signal(); | 198 resolver_proc->Signal(); |
199 MessageLoop::current()->RunAllPending(); | 199 MessageLoop::current()->RunAllPending(); |
200 } | 200 } |
201 | 201 |
202 TEST_F(DnsMasterTest, SingleLookupTest) { | 202 TEST_F(DnsMasterTest, SingleLookupTest) { |
203 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, | 203 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, |
204 MessageLoop::current(), default_max_queueing_delay_, | 204 default_max_queueing_delay_, DnsPrefetcherInit::kMaxConcurrentLookups); |
205 DnsPrefetcherInit::kMaxConcurrentLookups); | |
206 | 205 |
207 std::string goog("www.google.com"); | 206 std::string goog("www.google.com"); |
208 | 207 |
209 NameList names; | 208 NameList names; |
210 names.insert(names.end(), goog); | 209 names.insert(names.end(), goog); |
211 | 210 |
212 // Try to flood the master with many concurrent requests. | 211 // Try to flood the master with many concurrent requests. |
213 for (int i = 0; i < 10; i++) | 212 for (int i = 0; i < 10; i++) |
214 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); | 213 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); |
215 | 214 |
216 WaitForResolution(testing_master, names); | 215 WaitForResolution(testing_master, names); |
217 | 216 |
218 EXPECT_TRUE(testing_master->WasFound(goog)); | 217 EXPECT_TRUE(testing_master->WasFound(goog)); |
219 | 218 |
220 MessageLoop::current()->RunAllPending(); | 219 MessageLoop::current()->RunAllPending(); |
221 | 220 |
222 EXPECT_GT(testing_master->peak_pending_lookups(), names.size() / 2); | 221 EXPECT_GT(testing_master->peak_pending_lookups(), names.size() / 2); |
223 EXPECT_LE(testing_master->peak_pending_lookups(), names.size()); | 222 EXPECT_LE(testing_master->peak_pending_lookups(), names.size()); |
224 EXPECT_LE(testing_master->peak_pending_lookups(), | 223 EXPECT_LE(testing_master->peak_pending_lookups(), |
225 testing_master->max_concurrent_lookups()); | 224 testing_master->max_concurrent_lookups()); |
226 | 225 |
227 testing_master->Shutdown(); | 226 testing_master->Shutdown(); |
228 } | 227 } |
229 | 228 |
230 TEST_F(DnsMasterTest, ConcurrentLookupTest) { | 229 TEST_F(DnsMasterTest, ConcurrentLookupTest) { |
231 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); | 230 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); |
232 | 231 |
233 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, | 232 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, |
234 MessageLoop::current(), default_max_queueing_delay_, | 233 default_max_queueing_delay_, DnsPrefetcherInit::kMaxConcurrentLookups); |
235 DnsPrefetcherInit::kMaxConcurrentLookups); | |
236 | 234 |
237 std::string goog("www.google.com"), | 235 std::string goog("www.google.com"), |
238 goog2("gmail.google.com.com"), | 236 goog2("gmail.google.com.com"), |
239 goog3("mail.google.com"), | 237 goog3("mail.google.com"), |
240 goog4("gmail.com"); | 238 goog4("gmail.com"); |
241 std::string bad1("bad1.notfound"), | 239 std::string bad1("bad1.notfound"), |
242 bad2("bad2.notfound"); | 240 bad2("bad2.notfound"); |
243 | 241 |
244 NameList names; | 242 NameList names; |
245 names.insert(names.end(), goog); | 243 names.insert(names.end(), goog); |
(...skipping 27 matching lines...) Expand all Loading... |
273 EXPECT_LE(testing_master->peak_pending_lookups(), | 271 EXPECT_LE(testing_master->peak_pending_lookups(), |
274 testing_master->max_concurrent_lookups()); | 272 testing_master->max_concurrent_lookups()); |
275 | 273 |
276 testing_master->Shutdown(); | 274 testing_master->Shutdown(); |
277 } | 275 } |
278 | 276 |
279 TEST_F(DnsMasterTest, MassiveConcurrentLookupTest) { | 277 TEST_F(DnsMasterTest, MassiveConcurrentLookupTest) { |
280 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); | 278 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); |
281 | 279 |
282 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, | 280 scoped_refptr<DnsMaster> testing_master = new DnsMaster(host_resolver_, |
283 MessageLoop::current(), default_max_queueing_delay_, | 281 default_max_queueing_delay_, DnsPrefetcherInit::kMaxConcurrentLookups); |
284 DnsPrefetcherInit::kMaxConcurrentLookups); | |
285 | 282 |
286 NameList names; | 283 NameList names; |
287 for (int i = 0; i < 100; i++) | 284 for (int i = 0; i < 100; i++) |
288 names.push_back("host" + IntToString(i) + ".notfound"); | 285 names.push_back("host" + IntToString(i) + ".notfound"); |
289 | 286 |
290 // Try to flood the master with many concurrent requests. | 287 // Try to flood the master with many concurrent requests. |
291 for (int i = 0; i < 10; i++) | 288 for (int i = 0; i < 10; i++) |
292 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); | 289 testing_master->ResolveList(names, DnsHostInfo::PAGE_SCAN_MOTIVATED); |
293 | 290 |
294 WaitForResolution(testing_master, names); | 291 WaitForResolution(testing_master, names); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 ++i; // Skip latency value. | 375 ++i; // Skip latency value. |
379 } | 376 } |
380 return kLatencyNotFound; | 377 return kLatencyNotFound; |
381 } | 378 } |
382 | 379 |
383 //------------------------------------------------------------------------------ | 380 //------------------------------------------------------------------------------ |
384 | 381 |
385 // Make sure nil referral lists really have no entries, and no latency listed. | 382 // Make sure nil referral lists really have no entries, and no latency listed. |
386 TEST_F(DnsMasterTest, ReferrerSerializationNilTest) { | 383 TEST_F(DnsMasterTest, ReferrerSerializationNilTest) { |
387 scoped_refptr<DnsMaster> master = new DnsMaster(host_resolver_, | 384 scoped_refptr<DnsMaster> master = new DnsMaster(host_resolver_, |
388 MessageLoop::current(), default_max_queueing_delay_, | 385 default_max_queueing_delay_, DnsPrefetcherInit::kMaxConcurrentLookups); |
389 DnsPrefetcherInit::kMaxConcurrentLookups); | |
390 ListValue referral_list; | 386 ListValue referral_list; |
391 master->SerializeReferrers(&referral_list); | 387 master->SerializeReferrers(&referral_list); |
392 EXPECT_EQ(0U, referral_list.GetSize()); | 388 EXPECT_EQ(0U, referral_list.GetSize()); |
393 EXPECT_EQ(kLatencyNotFound, GetLatencyFromSerialization("a.com", "b.com", | 389 EXPECT_EQ(kLatencyNotFound, GetLatencyFromSerialization("a.com", "b.com", |
394 referral_list)); | 390 referral_list)); |
395 | 391 |
396 master->Shutdown(); | 392 master->Shutdown(); |
397 } | 393 } |
398 | 394 |
399 // Make sure that when a serialization list includes a value, that it can be | 395 // Make sure that when a serialization list includes a value, that it can be |
400 // deserialized into the database, and can be extracted back out via | 396 // deserialized into the database, and can be extracted back out via |
401 // serialization without being changed. | 397 // serialization without being changed. |
402 TEST_F(DnsMasterTest, ReferrerSerializationSingleReferrerTest) { | 398 TEST_F(DnsMasterTest, ReferrerSerializationSingleReferrerTest) { |
403 scoped_refptr<DnsMaster> master = new DnsMaster(host_resolver_, | 399 scoped_refptr<DnsMaster> master = new DnsMaster(host_resolver_, |
404 MessageLoop::current(), default_max_queueing_delay_, | 400 default_max_queueing_delay_, DnsPrefetcherInit::kMaxConcurrentLookups); |
405 DnsPrefetcherInit::kMaxConcurrentLookups); | |
406 std::string motivation_hostname = "www.google.com"; | 401 std::string motivation_hostname = "www.google.com"; |
407 std::string subresource_hostname = "icons.google.com"; | 402 std::string subresource_hostname = "icons.google.com"; |
408 const int kLatency = 3; | 403 const int kLatency = 3; |
409 ListValue referral_list; | 404 ListValue referral_list; |
410 | 405 |
411 AddToSerializedList(motivation_hostname, subresource_hostname, kLatency, | 406 AddToSerializedList(motivation_hostname, subresource_hostname, kLatency, |
412 &referral_list); | 407 &referral_list); |
413 | 408 |
414 master->DeserializeReferrers(referral_list); | 409 master->DeserializeReferrers(referral_list); |
415 | 410 |
416 ListValue recovered_referral_list; | 411 ListValue recovered_referral_list; |
417 master->SerializeReferrers(&recovered_referral_list); | 412 master->SerializeReferrers(&recovered_referral_list); |
418 EXPECT_EQ(1U, recovered_referral_list.GetSize()); | 413 EXPECT_EQ(1U, recovered_referral_list.GetSize()); |
419 EXPECT_EQ(kLatency, GetLatencyFromSerialization(motivation_hostname, | 414 EXPECT_EQ(kLatency, GetLatencyFromSerialization(motivation_hostname, |
420 subresource_hostname, | 415 subresource_hostname, |
421 recovered_referral_list)); | 416 recovered_referral_list)); |
422 | 417 |
423 master->Shutdown(); | 418 master->Shutdown(); |
424 } | 419 } |
425 | 420 |
426 // Make sure the Trim() functionality works as expected. | 421 // Make sure the Trim() functionality works as expected. |
427 TEST_F(DnsMasterTest, ReferrerSerializationTrimTest) { | 422 TEST_F(DnsMasterTest, ReferrerSerializationTrimTest) { |
428 scoped_refptr<DnsMaster> master = new DnsMaster(host_resolver_, | 423 scoped_refptr<DnsMaster> master = new DnsMaster(host_resolver_, |
429 MessageLoop::current(), default_max_queueing_delay_, | 424 default_max_queueing_delay_, DnsPrefetcherInit::kMaxConcurrentLookups); |
430 DnsPrefetcherInit::kMaxConcurrentLookups); | |
431 std::string motivation_hostname = "www.google.com"; | 425 std::string motivation_hostname = "www.google.com"; |
432 std::string icon_subresource_hostname = "icons.google.com"; | 426 std::string icon_subresource_hostname = "icons.google.com"; |
433 std::string img_subresource_hostname = "img.google.com"; | 427 std::string img_subresource_hostname = "img.google.com"; |
434 ListValue referral_list; | 428 ListValue referral_list; |
435 | 429 |
436 AddToSerializedList(motivation_hostname, icon_subresource_hostname, 10, | 430 AddToSerializedList(motivation_hostname, icon_subresource_hostname, 10, |
437 &referral_list); | 431 &referral_list); |
438 AddToSerializedList(motivation_hostname, img_subresource_hostname, 3, | 432 AddToSerializedList(motivation_hostname, img_subresource_hostname, 3, |
439 &referral_list); | 433 &referral_list); |
440 | 434 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 EXPECT_EQ(queue.Pop(), "scan"); | 543 EXPECT_EQ(queue.Pop(), "scan"); |
550 EXPECT_EQ(queue.Pop(), "unit"); | 544 EXPECT_EQ(queue.Pop(), "unit"); |
551 EXPECT_EQ(queue.Pop(), "lmax"); | 545 EXPECT_EQ(queue.Pop(), "lmax"); |
552 EXPECT_EQ(queue.Pop(), "omni"); | 546 EXPECT_EQ(queue.Pop(), "omni"); |
553 EXPECT_EQ(queue.Pop(), "startup"); | 547 EXPECT_EQ(queue.Pop(), "startup"); |
554 EXPECT_EQ(queue.Pop(), "omni"); | 548 EXPECT_EQ(queue.Pop(), "omni"); |
555 | 549 |
556 EXPECT_TRUE(queue.IsEmpty()); | 550 EXPECT_TRUE(queue.IsEmpty()); |
557 } | 551 } |
558 | 552 |
559 | |
560 | |
561 | |
562 } // namespace chrome_browser_net | 553 } // namespace chrome_browser_net |
OLD | NEW |