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

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: Hopefully fixed 469120 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
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> 8 #include <time.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 23 matching lines...) Expand all
34 #include "net/proxy/proxy_service.h" 34 #include "net/proxy/proxy_service.h"
35 #include "testing/gmock/include/gmock/gmock.h" 35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
37 37
38 using base::Time; 38 using base::Time;
39 using base::TimeDelta; 39 using base::TimeDelta;
40 using content::BrowserThread; 40 using content::BrowserThread;
41 41
42 namespace chrome_browser_net { 42 namespace chrome_browser_net {
43 43
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 { 44 class PredictorTest : public testing::Test {
81 public: 45 public:
82 PredictorTest() 46 PredictorTest()
83 : ui_thread_(BrowserThread::UI, &loop_), 47 : ui_thread_(BrowserThread::UI, &loop_),
84 io_thread_(BrowserThread::IO, &loop_), 48 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 49
125 private: 50 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_; 51 base::MessageLoopForUI loop_;
130 content::TestBrowserThread ui_thread_; 52 content::TestBrowserThread ui_thread_;
131 content::TestBrowserThread io_thread_; 53 content::TestBrowserThread io_thread_;
132
133 protected:
134 std::unique_ptr<net::MockCachingHostResolver> host_resolver_;
135 }; 54 };
136 55
137 //------------------------------------------------------------------------------ 56 //------------------------------------------------------------------------------
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 57 // Functions to help synthesize and test serializations of subresource referrer
273 // lists. 58 // lists.
274 59
275 // Return a motivation_list if we can find one for the given motivating_host (or 60 // Return a motivation_list if we can find one for the given motivating_host (or
276 // NULL if a match is not found). 61 // NULL if a match is not found).
277 static const base::ListValue* FindSerializationMotivation( 62 static const base::ListValue* FindSerializationMotivation(
278 const GURL& motivation, 63 const GURL& motivation,
279 const base::ListValue* referral_list) { 64 const base::ListValue* referral_list) {
280 CHECK_LT(0u, referral_list->GetSize()); // Room for version. 65 CHECK_LT(0u, referral_list->GetSize()); // Room for version.
281 int format_version = -1; 66 int format_version = -1;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 EXPECT_TRUE(subresource_list->GetDouble(i++, use_rate)); 145 EXPECT_TRUE(subresource_list->GetDouble(i++, use_rate));
361 if (subresource == GURL(url_spec)) { 146 if (subresource == GURL(url_spec)) {
362 return true; 147 return true;
363 } 148 }
364 } 149 }
365 return false; 150 return false;
366 } 151 }
367 152
368 //------------------------------------------------------------------------------ 153 //------------------------------------------------------------------------------
369 154
155 TEST_F(PredictorTest, StartupShutdownTest) {
156 Predictor testing_master(true, true);
157 testing_master.Shutdown();
158 }
159
370 // Make sure nil referral lists really have no entries, and no latency listed. 160 // Make sure nil referral lists really have no entries, and no latency listed.
371 TEST_F(PredictorTest, ReferrerSerializationNilTest) { 161 TEST_F(PredictorTest, ReferrerSerializationNilTest) {
372 Predictor predictor(true, true); 162 Predictor predictor(true, true);
373 predictor.SetHostResolver(host_resolver_.get());
374 163
375 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList()); 164 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList());
376 predictor.SerializeReferrers(referral_list.get()); 165 predictor.SerializeReferrers(referral_list.get());
377 EXPECT_EQ(1U, referral_list->GetSize()); 166 EXPECT_EQ(1U, referral_list->GetSize());
378 EXPECT_FALSE(GetDataFromSerialization( 167 EXPECT_FALSE(GetDataFromSerialization(
379 GURL("http://a.com:79"), GURL("http://b.com:78"), 168 GURL("http://a.com:79"), GURL("http://b.com:78"),
380 *referral_list.get(), NULL)); 169 *referral_list.get(), NULL));
381 170
382 predictor.Shutdown(); 171 predictor.Shutdown();
383 } 172 }
384 173
385 // Make sure that when a serialization list includes a value, that it can be 174 // 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 175 // deserialized into the database, and can be extracted back out via
387 // serialization without being changed. 176 // serialization without being changed.
388 TEST_F(PredictorTest, ReferrerSerializationSingleReferrerTest) { 177 TEST_F(PredictorTest, ReferrerSerializationSingleReferrerTest) {
389 Predictor predictor(true, true); 178 Predictor predictor(true, true);
390 predictor.SetHostResolver(host_resolver_.get());
391 const GURL motivation_url("http://www.google.com:91"); 179 const GURL motivation_url("http://www.google.com:91");
392 const GURL subresource_url("http://icons.google.com:90"); 180 const GURL subresource_url("http://icons.google.com:90");
393 const double kUseRate = 23.4; 181 const double kUseRate = 23.4;
394 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList()); 182 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList());
395 183
396 AddToSerializedList(motivation_url, subresource_url, 184 AddToSerializedList(motivation_url, subresource_url,
397 kUseRate, referral_list.get()); 185 kUseRate, referral_list.get());
398 186
399 predictor.DeserializeReferrers(*referral_list.get()); 187 predictor.DeserializeReferrers(*referral_list.get());
400 188
401 base::ListValue recovered_referral_list; 189 base::ListValue recovered_referral_list;
402 predictor.SerializeReferrers(&recovered_referral_list); 190 predictor.SerializeReferrers(&recovered_referral_list);
403 EXPECT_EQ(2U, recovered_referral_list.GetSize()); 191 EXPECT_EQ(2U, recovered_referral_list.GetSize());
404 double rate; 192 double rate;
405 EXPECT_TRUE(GetDataFromSerialization( 193 EXPECT_TRUE(GetDataFromSerialization(
406 motivation_url, subresource_url, recovered_referral_list, &rate)); 194 motivation_url, subresource_url, recovered_referral_list, &rate));
407 EXPECT_EQ(rate, kUseRate); 195 EXPECT_EQ(rate, kUseRate);
408 196
409 predictor.Shutdown(); 197 predictor.Shutdown();
410 } 198 }
411 199
412 // Check that GetHtmlReferrerLists() doesn't crash when given duplicated 200 // Check that GetHtmlReferrerLists() doesn't crash when given duplicated
413 // domains for referring URL, and that it sorts the results in the 201 // domains for referring URL, and that it sorts the results in the
414 // correct order. 202 // correct order.
415 TEST_F(PredictorTest, GetHtmlReferrerLists) { 203 TEST_F(PredictorTest, GetHtmlReferrerLists) {
416 Predictor predictor(true, true); 204 Predictor predictor(true, true);
417 predictor.SetHostResolver(host_resolver_.get());
418 const double kUseRate = 23.4; 205 const double kUseRate = 23.4;
419 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList()); 206 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList());
420 207
421 AddToSerializedList( 208 AddToSerializedList(
422 GURL("http://d.google.com/x1"), 209 GURL("http://d.google.com/x1"),
423 GURL("http://foo.com/"), 210 GURL("http://foo.com/"),
424 kUseRate, referral_list.get()); 211 kUseRate, referral_list.get());
425 212
426 // Duplicated hostname (d.google.com). This should not cause any crashes 213 // Duplicated hostname (d.google.com). This should not cause any crashes
427 // (i.e. crbug.com/116345) 214 // (i.e. crbug.com/116345)
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 if ((a) < 0.) \ 279 if ((a) < 0.) \
493 espilon_ratio = 1 / espilon_ratio; \ 280 espilon_ratio = 1 / espilon_ratio; \
494 EXPECT_LT(a, espilon_ratio * (b)); \ 281 EXPECT_LT(a, espilon_ratio * (b)); \
495 EXPECT_GT((a) * espilon_ratio, b); \ 282 EXPECT_GT((a) * espilon_ratio, b); \
496 } while (0) 283 } while (0)
497 284
498 285
499 // Make sure the Trim() functionality works as expected. 286 // Make sure the Trim() functionality works as expected.
500 TEST_F(PredictorTest, ReferrerSerializationTrimTest) { 287 TEST_F(PredictorTest, ReferrerSerializationTrimTest) {
501 Predictor predictor(true, true); 288 Predictor predictor(true, true);
502 predictor.SetHostResolver(host_resolver_.get());
503 GURL motivation_url("http://www.google.com:110"); 289 GURL motivation_url("http://www.google.com:110");
504 290
505 GURL icon_subresource_url("http://icons.google.com:111"); 291 GURL icon_subresource_url("http://icons.google.com:111");
506 const double kRateIcon = 16.0 * Predictor::kDiscardableExpectedValue; 292 const double kRateIcon = 16.0 * Predictor::kDiscardableExpectedValue;
507 GURL img_subresource_url("http://img.google.com:118"); 293 GURL img_subresource_url("http://img.google.com:118");
508 const double kRateImg = 8.0 * Predictor::kDiscardableExpectedValue; 294 const double kRateImg = 8.0 * Predictor::kDiscardableExpectedValue;
509 295
510 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList()); 296 std::unique_ptr<base::ListValue> referral_list(NewEmptySerializationList());
511 AddToSerializedList( 297 AddToSerializedList(
512 motivation_url, icon_subresource_url, kRateIcon, referral_list.get()); 298 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)); 467 Predictor::CanonicalizeUrl(https));
682 468
683 // Https works fine. 469 // Https works fine.
684 GURL long_https("https://host:999/path?query=value"); 470 GURL long_https("https://host:999/path?query=value");
685 EXPECT_EQ(Predictor::CanonicalizeUrl(long_https), 471 EXPECT_EQ(Predictor::CanonicalizeUrl(long_https),
686 long_https.GetWithEmptyPath()); 472 long_https.GetWithEmptyPath());
687 } 473 }
688 474
689 TEST_F(PredictorTest, DiscardPredictorResults) { 475 TEST_F(PredictorTest, DiscardPredictorResults) {
690 SimplePredictor predictor(true, true); 476 SimplePredictor predictor(true, true);
691 predictor.SetHostResolver(host_resolver_.get());
692 base::ListValue referral_list; 477 base::ListValue referral_list;
693 predictor.SerializeReferrers(&referral_list); 478 predictor.SerializeReferrers(&referral_list);
694 EXPECT_EQ(1U, referral_list.GetSize()); 479 EXPECT_EQ(1U, referral_list.GetSize());
695 480
696 GURL host_1("http://test_1"); 481 GURL host_1("http://test_1");
697 GURL host_2("http://test_2"); 482 GURL host_2("http://test_2");
698 predictor.LearnFromNavigation(host_1, host_2); 483 predictor.LearnFromNavigation(host_1, host_2);
699 484
700 predictor.SerializeReferrers(&referral_list); 485 predictor.SerializeReferrers(&referral_list);
701 EXPECT_EQ(2U, referral_list.GetSize()); 486 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); 627 testing_master.Resolve(goog, UrlInfo::OMNIBOX_MOTIVATED);
843 628
844 // Proxy may not be in use (the PAC script has not yet been evaluated), so the 629 // Proxy may not be in use (the PAC script has not yet been evaluated), so the
845 // name has been registered for pre-resolve. 630 // name has been registered for pre-resolve.
846 EXPECT_FALSE(testing_master.work_queue_.IsEmpty()); 631 EXPECT_FALSE(testing_master.work_queue_.IsEmpty());
847 632
848 testing_master.Shutdown(); 633 testing_master.Shutdown();
849 } 634 }
850 635
851 } // namespace chrome_browser_net 636 } // namespace chrome_browser_net
OLDNEW
« chrome/browser/net/predictor_browsertest.cc ('K') | « chrome/browser/net/predictor_browsertest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698