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

Side by Side Diff: chrome/browser/net/predictor_unittest.cc

Issue 1989363007: Move predictor dns unit tests to browser tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: eroman@ review Created 4 years, 7 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 | « chrome/browser/net/predictor_browsertest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/net/predictor.h" 5 #include "chrome/browser/net/predictor.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <time.h>
9 8
10 #include <algorithm> 9 #include <algorithm>
11 #include <memory> 10 #include <memory>
12 #include <sstream> 11 #include <sstream>
13 #include <string> 12 #include <string>
14 13
15 #include "base/location.h" 14 #include "base/location.h"
16 #include "base/macros.h" 15 #include "base/macros.h"
17 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
18 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
19 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
20 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
21 #include "base/timer/timer.h"
22 #include "base/values.h" 20 #include "base/values.h"
23 #include "build/build_config.h" 21 #include "build/build_config.h"
24 #include "chrome/browser/net/url_info.h" 22 #include "chrome/browser/net/url_info.h"
25 #include "components/network_hints/common/network_hints_common.h"
26 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
27 #include "net/base/address_list.h"
28 #include "net/base/load_flags.h" 24 #include "net/base/load_flags.h"
29 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
30 #include "net/base/winsock_init.h"
31 #include "net/dns/mock_host_resolver.h"
32 #include "net/http/transport_security_state.h" 26 #include "net/http/transport_security_state.h"
33 #include "net/proxy/proxy_config_service_fixed.h" 27 #include "net/proxy/proxy_config_service_fixed.h"
34 #include "net/proxy/proxy_service.h" 28 #include "net/proxy/proxy_service.h"
35 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
37 31
38 using base::Time; 32 using base::Time;
39 using base::TimeDelta; 33 using base::TimeDelta;
40 using content::BrowserThread; 34 using content::BrowserThread;
41 35
42 namespace chrome_browser_net { 36 namespace chrome_browser_net {
43 37
44 class WaitForResolutionHelper;
45
46 class WaitForResolutionHelper {
47 public:
48 WaitForResolutionHelper(Predictor* predictor,
49 const UrlList& hosts,
50 base::RepeatingTimer* timer,
51 int checks_until_quit)
52 : predictor_(predictor),
53 hosts_(hosts),
54 timer_(timer),
55 checks_until_quit_(checks_until_quit) {}
56
57 void CheckIfResolutionsDone() {
58 if (--checks_until_quit_ > 0) {
59 for (UrlList::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
60 if (predictor_->GetResolutionDuration(*i) ==
61 UrlInfo::NullDuration())
62 return; // We don't have resolution for that host.
63 }
64
65 // When all hostnames have been resolved, or we've hit the limit,
66 // exit the loop.
67 timer_->Stop();
68 base::MessageLoop::current()->QuitWhenIdle();
69 delete timer_;
70 delete this;
71 }
72
73 private:
74 Predictor* predictor_;
75 const UrlList hosts_;
76 base::RepeatingTimer* timer_;
77 int checks_until_quit_;
78 };
79
80 class PredictorTest : public testing::Test { 38 class PredictorTest : public testing::Test {
81 public: 39 public:
82 PredictorTest() 40 PredictorTest()
83 : ui_thread_(BrowserThread::UI, &loop_), 41 : ui_thread_(BrowserThread::UI, &loop_),
84 io_thread_(BrowserThread::IO, &loop_), 42 io_thread_(BrowserThread::IO, &loop_) {}
85 host_resolver_(new net::MockCachingHostResolver()) {
86 }
87
88 protected:
89 void SetUp() override {
90 #if defined(OS_WIN)
91 net::EnsureWinsockInit();
92 #endif
93 Predictor::set_max_parallel_resolves(
94 Predictor::kMaxSpeculativeParallelResolves);
95 Predictor::set_max_queueing_delay(
96 Predictor::kMaxSpeculativeResolveQueueDelayMs);
97 // Since we are using a caching HostResolver, the following latencies will
98 // only be incurred by the first request, after which the result will be
99 // cached internally by |host_resolver_|.
100 net::RuleBasedHostResolverProc* rules = host_resolver_->rules();
101 rules->AddRuleWithLatency("www.google.com", "127.0.0.1", 50);
102 rules->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70);
103 rules->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44);
104 rules->AddRuleWithLatency("gmail.com", "127.0.0.1", 63);
105 }
106
107 void WaitForResolution(Predictor* predictor, const UrlList& hosts) {
108 base::RepeatingTimer* timer = new base::RepeatingTimer();
109 // By default allow the loop to run for a minute -- 600 iterations.
110 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100),
111 new WaitForResolutionHelper(predictor, hosts, timer, 600),
112 &WaitForResolutionHelper::CheckIfResolutionsDone);
113 base::MessageLoop::current()->Run();
114 }
115
116 void WaitForResolutionWithLimit(
117 Predictor* predictor, const UrlList& hosts, int limit) {
118 base::RepeatingTimer* timer = new base::RepeatingTimer();
119 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100),
120 new WaitForResolutionHelper(predictor, hosts, timer, limit),
121 &WaitForResolutionHelper::CheckIfResolutionsDone);
122 base::MessageLoop::current()->Run();
123 }
124 43
125 private: 44 private:
126 // IMPORTANT: do not move this below |host_resolver_|; the host resolver
127 // must not outlive the message loop, otherwise bad things can happen
128 // (like posting to a deleted message loop).
129 base::MessageLoopForUI loop_; 45 base::MessageLoopForUI loop_;
130 content::TestBrowserThread ui_thread_; 46 content::TestBrowserThread ui_thread_;
131 content::TestBrowserThread io_thread_; 47 content::TestBrowserThread io_thread_;
132
133 protected:
134 std::unique_ptr<net::MockCachingHostResolver> host_resolver_;
135 }; 48 };
136 49
137 //------------------------------------------------------------------------------ 50 //------------------------------------------------------------------------------
138
139 TEST_F(PredictorTest, StartupShutdownTest) {
140 Predictor testing_master(true, true);
141 testing_master.Shutdown();
142 }
143
144
145 TEST_F(PredictorTest, ShutdownWhenResolutionIsPendingTest) {
146 std::unique_ptr<net::HostResolver> host_resolver(
147 new net::HangingHostResolver());
148
149 Predictor testing_master(true, true);
150 testing_master.SetHostResolver(host_resolver.get());
151
152 GURL localhost("http://localhost:80");
153 UrlList names;
154 names.push_back(localhost);
155
156 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED);
157
158 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
159 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
160 base::TimeDelta::FromMilliseconds(500));
161 base::MessageLoop::current()->Run();
162
163 EXPECT_FALSE(testing_master.WasFound(localhost));
164
165 testing_master.Shutdown();
166
167 // Clean up after ourselves.
168 base::MessageLoop::current()->RunUntilIdle();
169 }
170
171 TEST_F(PredictorTest, SingleLookupTest) {
172 Predictor testing_master(true, true);
173 testing_master.SetHostResolver(host_resolver_.get());
174
175 GURL goog("http://www.google.com:80");
176
177 UrlList names;
178 names.push_back(goog);
179
180 // Try to flood the predictor with many concurrent requests.
181 for (int i = 0; i < 10; i++)
182 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED);
183
184 WaitForResolution(&testing_master, names);
185
186 EXPECT_TRUE(testing_master.WasFound(goog));
187
188 base::MessageLoop::current()->RunUntilIdle();
189
190 EXPECT_GT(testing_master.peak_pending_lookups(), names.size() / 2);
191 EXPECT_LE(testing_master.peak_pending_lookups(), names.size());
192 EXPECT_LE(testing_master.peak_pending_lookups(),
193 testing_master.max_concurrent_dns_lookups());
194
195 testing_master.Shutdown();
196 }
197
198 TEST_F(PredictorTest, ConcurrentLookupTest) {
199 host_resolver_->rules()->AddSimulatedFailure("*.notfound");
200
201 Predictor testing_master(true, true);
202 testing_master.SetHostResolver(host_resolver_.get());
203
204 GURL goog("http://www.google.com:80"),
205 goog2("http://gmail.google.com.com:80"),
206 goog3("http://mail.google.com:80"),
207 goog4("http://gmail.com:80");
208 GURL bad1("http://bad1.notfound:80"),
209 bad2("http://bad2.notfound:80");
210
211 UrlList names;
212 names.push_back(goog);
213 names.push_back(goog3);
214 names.push_back(bad1);
215 names.push_back(goog2);
216 names.push_back(bad2);
217 names.push_back(goog4);
218 names.push_back(goog);
219
220 // Try to flood the predictor with many concurrent requests.
221 for (int i = 0; i < 10; i++)
222 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED);
223
224 WaitForResolution(&testing_master, names);
225
226 EXPECT_TRUE(testing_master.WasFound(goog));
227 EXPECT_TRUE(testing_master.WasFound(goog3));
228 EXPECT_TRUE(testing_master.WasFound(goog2));
229 EXPECT_TRUE(testing_master.WasFound(goog4));
230 EXPECT_FALSE(testing_master.WasFound(bad1));
231 EXPECT_FALSE(testing_master.WasFound(bad2));
232
233 base::MessageLoop::current()->RunUntilIdle();
234
235 EXPECT_FALSE(testing_master.WasFound(bad1));
236 EXPECT_FALSE(testing_master.WasFound(bad2));
237
238 EXPECT_LE(testing_master.peak_pending_lookups(), names.size());
239 EXPECT_LE(testing_master.peak_pending_lookups(),
240 testing_master.max_concurrent_dns_lookups());
241
242 testing_master.Shutdown();
243 }
244
245 TEST_F(PredictorTest, MassiveConcurrentLookupTest) {
246 host_resolver_->rules()->AddSimulatedFailure("*.notfound");
247
248 Predictor testing_master(true, true);
249 testing_master.SetHostResolver(host_resolver_.get());
250
251 UrlList names;
252 for (int i = 0; i < 100; i++)
253 names.push_back(GURL(
254 "http://host" + base::IntToString(i) + ".notfound:80"));
255
256 // Try to flood the predictor with many concurrent requests.
257 for (int i = 0; i < 10; i++)
258 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED);
259
260 WaitForResolution(&testing_master, names);
261
262 base::MessageLoop::current()->RunUntilIdle();
263
264 EXPECT_LE(testing_master.peak_pending_lookups(), names.size());
265 EXPECT_LE(testing_master.peak_pending_lookups(),
266 testing_master.max_concurrent_dns_lookups());
267
268 testing_master.Shutdown();
269 }
270
271 //------------------------------------------------------------------------------
272 // Functions to help synthesize and test serializations of subresource referrer 51 // Functions to help synthesize and test serializations of subresource referrer
273 // lists. 52 // lists.
274 53
275 // Return a motivation_list if we can find one for the given motivating_host (or 54 // Return a motivation_list if we can find one for the given motivating_host (or
276 // NULL if a match is not found). 55 // NULL if a match is not found).
277 static const base::ListValue* FindSerializationMotivation( 56 static const base::ListValue* FindSerializationMotivation(
278 const GURL& motivation, 57 const GURL& motivation,
279 const base::ListValue* referral_list) { 58 const base::ListValue* referral_list) {
280 CHECK_LT(0u, referral_list->GetSize()); // Room for version. 59 CHECK_LT(0u, referral_list->GetSize()); // Room for version.
281 int format_version = -1; 60 int format_version = -1;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 std::string url_spec; 137 std::string url_spec;
359 EXPECT_TRUE(subresource_list->GetString(i++, &url_spec)); 138 EXPECT_TRUE(subresource_list->GetString(i++, &url_spec));
360 EXPECT_TRUE(subresource_list->GetDouble(i++, use_rate)); 139 EXPECT_TRUE(subresource_list->GetDouble(i++, use_rate));
361 if (subresource == GURL(url_spec)) { 140 if (subresource == GURL(url_spec)) {
362 return true; 141 return true;
363 } 142 }
364 } 143 }
365 return false; 144 return false;
366 } 145 }
367 146
368 //------------------------------------------------------------------------------ 147 TEST_F(PredictorTest, StartupShutdownTest) {
148 Predictor testing_master(true, true);
149 testing_master.Shutdown();
150 }
369 151
370 // Make sure nil referral lists really have no entries, and no latency listed. 152 // Make sure nil referral lists really have no entries, and no latency listed.
371 TEST_F(PredictorTest, ReferrerSerializationNilTest) { 153 TEST_F(PredictorTest, ReferrerSerializationNilTest) {
372 Predictor predictor(true, true); 154 Predictor predictor(true, true);
373 predictor.SetHostResolver(host_resolver_.get());
374 155
375 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList()); 156 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList());
376 predictor.SerializeReferrers(referral_list.get()); 157 predictor.SerializeReferrers(referral_list.get());
377 EXPECT_EQ(1U, referral_list->GetSize()); 158 EXPECT_EQ(1U, referral_list->GetSize());
378 EXPECT_FALSE(GetDataFromSerialization( 159 EXPECT_FALSE(GetDataFromSerialization(
379 GURL("http://a.com:79"), GURL("http://b.com:78"), 160 GURL("http://a.com:79"), GURL("http://b.com:78"),
380 *referral_list.get(), NULL)); 161 *referral_list.get(), NULL));
381 162
382 predictor.Shutdown(); 163 predictor.Shutdown();
383 } 164 }
384 165
385 // Make sure that when a serialization list includes a value, that it can be 166 // Make sure that when a serialization list includes a value, that it can be
386 // deserialized into the database, and can be extracted back out via 167 // deserialized into the database, and can be extracted back out via
387 // serialization without being changed. 168 // serialization without being changed.
388 TEST_F(PredictorTest, ReferrerSerializationSingleReferrerTest) { 169 TEST_F(PredictorTest, ReferrerSerializationSingleReferrerTest) {
389 Predictor predictor(true, true); 170 Predictor predictor(true, true);
390 predictor.SetHostResolver(host_resolver_.get());
391 const GURL motivation_url("http://www.google.com:91"); 171 const GURL motivation_url("http://www.google.com:91");
392 const GURL subresource_url("http://icons.google.com:90"); 172 const GURL subresource_url("http://icons.google.com:90");
393 const double kUseRate = 23.4; 173 const double kUseRate = 23.4;
394 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList()); 174 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList());
395 175
396 AddToSerializedList(motivation_url, subresource_url, 176 AddToSerializedList(motivation_url, subresource_url,
397 kUseRate, referral_list.get()); 177 kUseRate, referral_list.get());
398 178
399 predictor.DeserializeReferrers(*referral_list.get()); 179 predictor.DeserializeReferrers(*referral_list.get());
400 180
401 base::ListValue recovered_referral_list; 181 base::ListValue recovered_referral_list;
402 predictor.SerializeReferrers(&recovered_referral_list); 182 predictor.SerializeReferrers(&recovered_referral_list);
403 EXPECT_EQ(2U, recovered_referral_list.GetSize()); 183 EXPECT_EQ(2U, recovered_referral_list.GetSize());
404 double rate; 184 double rate;
405 EXPECT_TRUE(GetDataFromSerialization( 185 EXPECT_TRUE(GetDataFromSerialization(
406 motivation_url, subresource_url, recovered_referral_list, &rate)); 186 motivation_url, subresource_url, recovered_referral_list, &rate));
407 EXPECT_EQ(rate, kUseRate); 187 EXPECT_EQ(rate, kUseRate);
408 188
409 predictor.Shutdown(); 189 predictor.Shutdown();
410 } 190 }
411 191
412 // Check that GetHtmlReferrerLists() doesn't crash when given duplicated 192 // Check that GetHtmlReferrerLists() doesn't crash when given duplicated
413 // domains for referring URL, and that it sorts the results in the 193 // domains for referring URL, and that it sorts the results in the
414 // correct order. 194 // correct order.
415 TEST_F(PredictorTest, GetHtmlReferrerLists) { 195 TEST_F(PredictorTest, GetHtmlReferrerLists) {
416 Predictor predictor(true, true); 196 Predictor predictor(true, true);
417 predictor.SetHostResolver(host_resolver_.get());
418 const double kUseRate = 23.4; 197 const double kUseRate = 23.4;
419 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList()); 198 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList());
420 199
421 AddToSerializedList( 200 AddToSerializedList(
422 GURL("http://d.google.com/x1"), 201 GURL("http://d.google.com/x1"),
423 GURL("http://foo.com/"), 202 GURL("http://foo.com/"),
424 kUseRate, referral_list.get()); 203 kUseRate, referral_list.get());
425 204
426 // Duplicated hostname (d.google.com). This should not cause any crashes 205 // Duplicated hostname (d.google.com). This should not cause any crashes
427 // (i.e. crbug.com/116345) 206 // (i.e. crbug.com/116345)
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 if ((a) < 0.) \ 271 if ((a) < 0.) \
493 espilon_ratio = 1 / espilon_ratio; \ 272 espilon_ratio = 1 / espilon_ratio; \
494 EXPECT_LT(a, espilon_ratio * (b)); \ 273 EXPECT_LT(a, espilon_ratio * (b)); \
495 EXPECT_GT((a) * espilon_ratio, b); \ 274 EXPECT_GT((a) * espilon_ratio, b); \
496 } while (0) 275 } while (0)
497 276
498 277
499 // Make sure the Trim() functionality works as expected. 278 // Make sure the Trim() functionality works as expected.
500 TEST_F(PredictorTest, ReferrerSerializationTrimTest) { 279 TEST_F(PredictorTest, ReferrerSerializationTrimTest) {
501 Predictor predictor(true, true); 280 Predictor predictor(true, true);
502 predictor.SetHostResolver(host_resolver_.get());
503 GURL motivation_url("http://www.google.com:110"); 281 GURL motivation_url("http://www.google.com:110");
504 282
505 GURL icon_subresource_url("http://icons.google.com:111"); 283 GURL icon_subresource_url("http://icons.google.com:111");
506 const double kRateIcon = 16.0 * Predictor::kDiscardableExpectedValue; 284 const double kRateIcon = 16.0 * Predictor::kDiscardableExpectedValue;
507 GURL img_subresource_url("http://img.google.com:118"); 285 GURL img_subresource_url("http://img.google.com:118");
508 const double kRateImg = 8.0 * Predictor::kDiscardableExpectedValue; 286 const double kRateImg = 8.0 * Predictor::kDiscardableExpectedValue;
509 287
510 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList()); 288 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList());
511 AddToSerializedList( 289 AddToSerializedList(
512 motivation_url, icon_subresource_url, kRateIcon, referral_list.get()); 290 motivation_url, icon_subresource_url, kRateIcon, referral_list.get());
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 Predictor::CanonicalizeUrl(https)); 459 Predictor::CanonicalizeUrl(https));
682 460
683 // Https works fine. 461 // Https works fine.
684 GURL long_https("https://host:999/path?query=value"); 462 GURL long_https("https://host:999/path?query=value");
685 EXPECT_EQ(Predictor::CanonicalizeUrl(long_https), 463 EXPECT_EQ(Predictor::CanonicalizeUrl(long_https),
686 long_https.GetWithEmptyPath()); 464 long_https.GetWithEmptyPath());
687 } 465 }
688 466
689 TEST_F(PredictorTest, DiscardPredictorResults) { 467 TEST_F(PredictorTest, DiscardPredictorResults) {
690 SimplePredictor predictor(true, true); 468 SimplePredictor predictor(true, true);
691 predictor.SetHostResolver(host_resolver_.get());
692 base::ListValue referral_list; 469 base::ListValue referral_list;
693 predictor.SerializeReferrers(&referral_list); 470 predictor.SerializeReferrers(&referral_list);
694 EXPECT_EQ(1U, referral_list.GetSize()); 471 EXPECT_EQ(1U, referral_list.GetSize());
695 472
696 GURL host_1("http://test_1"); 473 GURL host_1("http://test_1");
697 GURL host_2("http://test_2"); 474 GURL host_2("http://test_2");
698 predictor.LearnFromNavigation(host_1, host_2); 475 predictor.LearnFromNavigation(host_1, host_2);
699 476
700 predictor.SerializeReferrers(&referral_list); 477 predictor.SerializeReferrers(&referral_list);
701 EXPECT_EQ(2U, referral_list.GetSize()); 478 EXPECT_EQ(2U, referral_list.GetSize());
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 testing_master.Resolve(goog, UrlInfo::OMNIBOX_MOTIVATED); 619 testing_master.Resolve(goog, UrlInfo::OMNIBOX_MOTIVATED);
843 620
844 // Proxy may not be in use (the PAC script has not yet been evaluated), so the 621 // Proxy may not be in use (the PAC script has not yet been evaluated), so the
845 // name has been registered for pre-resolve. 622 // name has been registered for pre-resolve.
846 EXPECT_FALSE(testing_master.work_queue_.IsEmpty()); 623 EXPECT_FALSE(testing_master.work_queue_.IsEmpty());
847 624
848 testing_master.Shutdown(); 625 testing_master.Shutdown();
849 } 626 }
850 627
851 } // namespace chrome_browser_net 628 } // namespace chrome_browser_net
OLDNEW
« no previous file with comments | « chrome/browser/net/predictor_browsertest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698