OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
14 #include "base/timer.h" | 14 #include "base/timer.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/net/predictor_api.h" | 16 #include "chrome/browser/net/predictor.h" |
17 #include "chrome/browser/net/url_info.h" | 17 #include "chrome/browser/net/url_info.h" |
18 #include "chrome/common/net/predictor_common.h" | 18 #include "chrome/common/net/predictor_common.h" |
19 #include "content/browser/browser_thread.h" | 19 #include "content/browser/browser_thread.h" |
20 #include "net/base/address_list.h" | 20 #include "net/base/address_list.h" |
21 #include "net/base/mock_host_resolver.h" | 21 #include "net/base/mock_host_resolver.h" |
22 #include "net/base/winsock_init.h" | 22 #include "net/base/winsock_init.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 using base::Time; | 25 using base::Time; |
26 using base::TimeDelta; | 26 using base::TimeDelta; |
(...skipping 28 matching lines...) Expand all Loading... |
55 | 55 |
56 private: | 56 private: |
57 Predictor* predictor_; | 57 Predictor* predictor_; |
58 const UrlList hosts_; | 58 const UrlList hosts_; |
59 HelperTimer* timer_; | 59 HelperTimer* timer_; |
60 }; | 60 }; |
61 | 61 |
62 class PredictorTest : public testing::Test { | 62 class PredictorTest : public testing::Test { |
63 public: | 63 public: |
64 PredictorTest() | 64 PredictorTest() |
65 : io_thread_(BrowserThread::IO, &loop_), | 65 : ui_thread_(BrowserThread::UI, &loop_), |
66 host_resolver_(new net::MockCachingHostResolver()), | 66 io_thread_(BrowserThread::IO, &loop_), |
67 default_max_queueing_delay_(TimeDelta::FromMilliseconds( | 67 host_resolver_(new net::MockCachingHostResolver()) { |
68 PredictorInit::kMaxSpeculativeResolveQueueDelayMs)) { | |
69 } | 68 } |
70 | 69 |
71 protected: | 70 protected: |
72 virtual void SetUp() { | 71 virtual void SetUp() { |
73 #if defined(OS_WIN) | 72 #if defined(OS_WIN) |
74 net::EnsureWinsockInit(); | 73 net::EnsureWinsockInit(); |
75 #endif | 74 #endif |
| 75 Predictor::set_max_parallel_resolves( |
| 76 Predictor::kMaxSpeculativeParallelResolves); |
| 77 Predictor::set_max_queueing_delay( |
| 78 Predictor::kMaxSpeculativeResolveQueueDelayMs); |
76 // Since we are using a caching HostResolver, the following latencies will | 79 // Since we are using a caching HostResolver, the following latencies will |
77 // only be incurred by the first request, after which the result will be | 80 // only be incurred by the first request, after which the result will be |
78 // cached internally by |host_resolver_|. | 81 // cached internally by |host_resolver_|. |
79 net::RuleBasedHostResolverProc* rules = host_resolver_->rules(); | 82 net::RuleBasedHostResolverProc* rules = host_resolver_->rules(); |
80 rules->AddRuleWithLatency("www.google.com", "127.0.0.1", 50); | 83 rules->AddRuleWithLatency("www.google.com", "127.0.0.1", 50); |
81 rules->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70); | 84 rules->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70); |
82 rules->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44); | 85 rules->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44); |
83 rules->AddRuleWithLatency("gmail.com", "127.0.0.1", 63); | 86 rules->AddRuleWithLatency("gmail.com", "127.0.0.1", 63); |
84 } | 87 } |
85 | 88 |
86 void WaitForResolution(Predictor* predictor, const UrlList& hosts) { | 89 void WaitForResolution(Predictor* predictor, const UrlList& hosts) { |
87 HelperTimer* timer = new HelperTimer(); | 90 HelperTimer* timer = new HelperTimer(); |
88 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100), | 91 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100), |
89 new WaitForResolutionHelper(predictor, hosts, timer), | 92 new WaitForResolutionHelper(predictor, hosts, timer), |
90 &WaitForResolutionHelper::Run); | 93 &WaitForResolutionHelper::Run); |
91 MessageLoop::current()->Run(); | 94 MessageLoop::current()->Run(); |
92 } | 95 } |
93 | 96 |
94 private: | 97 private: |
95 // IMPORTANT: do not move this below |host_resolver_|; the host resolver | 98 // IMPORTANT: do not move this below |host_resolver_|; the host resolver |
96 // must not outlive the message loop, otherwise bad things can happen | 99 // must not outlive the message loop, otherwise bad things can happen |
97 // (like posting to a deleted message loop). | 100 // (like posting to a deleted message loop). |
98 MessageLoop loop_; | 101 MessageLoopForUI loop_; |
| 102 BrowserThread ui_thread_; |
99 BrowserThread io_thread_; | 103 BrowserThread io_thread_; |
100 | 104 |
101 protected: | 105 protected: |
102 scoped_ptr<net::MockCachingHostResolver> host_resolver_; | 106 scoped_ptr<net::MockCachingHostResolver> host_resolver_; |
103 | |
104 // Shorthand to access TimeDelta of PredictorInit::kMaxQueueingDelayMs. | |
105 // (It would be a static constant... except style rules preclude that :-/ ). | |
106 const TimeDelta default_max_queueing_delay_; | |
107 }; | 107 }; |
108 | 108 |
109 //------------------------------------------------------------------------------ | 109 //------------------------------------------------------------------------------ |
110 | 110 |
111 TEST_F(PredictorTest, StartupShutdownTest) { | 111 TEST_F(PredictorTest, StartupShutdownTest) { |
112 scoped_refptr<Predictor> testing_master( | 112 Predictor testing_master(true); |
113 new Predictor(host_resolver_.get(), | 113 testing_master.Shutdown(); |
114 default_max_queueing_delay_, | |
115 PredictorInit::kMaxSpeculativeParallelResolves, | |
116 false)); | |
117 testing_master->Shutdown(); | |
118 } | 114 } |
119 | 115 |
120 | 116 |
121 TEST_F(PredictorTest, ShutdownWhenResolutionIsPendingTest) { | 117 TEST_F(PredictorTest, ShutdownWhenResolutionIsPendingTest) { |
122 scoped_refptr<net::WaitingHostResolverProc> resolver_proc( | 118 scoped_refptr<net::WaitingHostResolverProc> resolver_proc( |
123 new net::WaitingHostResolverProc(NULL)); | 119 new net::WaitingHostResolverProc(NULL)); |
124 host_resolver_->Reset(resolver_proc); | 120 host_resolver_->Reset(resolver_proc); |
125 | 121 |
126 scoped_refptr<Predictor> testing_master( | 122 Predictor testing_master(true); |
127 new Predictor(host_resolver_.get(), | 123 testing_master.SetHostResolver(host_resolver_.get()); |
128 default_max_queueing_delay_, | |
129 PredictorInit::kMaxSpeculativeParallelResolves, | |
130 false)); | |
131 | 124 |
132 GURL localhost("http://localhost:80"); | 125 GURL localhost("http://localhost:80"); |
133 UrlList names; | 126 UrlList names; |
134 names.push_back(localhost); | 127 names.push_back(localhost); |
135 | 128 |
136 testing_master->ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); | 129 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); |
137 | 130 |
138 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 131 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
139 new MessageLoop::QuitTask(), 500); | 132 new MessageLoop::QuitTask(), 500); |
140 MessageLoop::current()->Run(); | 133 MessageLoop::current()->Run(); |
141 | 134 |
142 EXPECT_FALSE(testing_master->WasFound(localhost)); | 135 EXPECT_FALSE(testing_master.WasFound(localhost)); |
143 | 136 |
144 testing_master->Shutdown(); | 137 testing_master.Shutdown(); |
145 | 138 |
146 // Clean up after ourselves. | 139 // Clean up after ourselves. |
147 resolver_proc->Signal(); | 140 resolver_proc->Signal(); |
148 MessageLoop::current()->RunAllPending(); | 141 MessageLoop::current()->RunAllPending(); |
149 } | 142 } |
150 | 143 |
151 TEST_F(PredictorTest, SingleLookupTest) { | 144 TEST_F(PredictorTest, SingleLookupTest) { |
152 scoped_refptr<Predictor> testing_master( | 145 Predictor testing_master(true); |
153 new Predictor(host_resolver_.get(), | 146 testing_master.SetHostResolver(host_resolver_.get()); |
154 default_max_queueing_delay_, | |
155 PredictorInit::kMaxSpeculativeParallelResolves, | |
156 false)); | |
157 | 147 |
158 GURL goog("http://www.google.com:80"); | 148 GURL goog("http://www.google.com:80"); |
159 | 149 |
160 UrlList names; | 150 UrlList names; |
161 names.push_back(goog); | 151 names.push_back(goog); |
162 | 152 |
163 // Try to flood the predictor with many concurrent requests. | 153 // Try to flood the predictor with many concurrent requests. |
164 for (int i = 0; i < 10; i++) | 154 for (int i = 0; i < 10; i++) |
165 testing_master->ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); | 155 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); |
166 | 156 |
167 WaitForResolution(testing_master, names); | 157 WaitForResolution(&testing_master, names); |
168 | 158 |
169 EXPECT_TRUE(testing_master->WasFound(goog)); | 159 EXPECT_TRUE(testing_master.WasFound(goog)); |
170 | 160 |
171 MessageLoop::current()->RunAllPending(); | 161 MessageLoop::current()->RunAllPending(); |
172 | 162 |
173 EXPECT_GT(testing_master->peak_pending_lookups(), names.size() / 2); | 163 EXPECT_GT(testing_master.peak_pending_lookups(), names.size() / 2); |
174 EXPECT_LE(testing_master->peak_pending_lookups(), names.size()); | 164 EXPECT_LE(testing_master.peak_pending_lookups(), names.size()); |
175 EXPECT_LE(testing_master->peak_pending_lookups(), | 165 EXPECT_LE(testing_master.peak_pending_lookups(), |
176 testing_master->max_concurrent_dns_lookups()); | 166 testing_master.max_concurrent_dns_lookups()); |
177 | 167 |
178 testing_master->Shutdown(); | 168 testing_master.Shutdown(); |
179 } | 169 } |
180 | 170 |
181 TEST_F(PredictorTest, ConcurrentLookupTest) { | 171 TEST_F(PredictorTest, ConcurrentLookupTest) { |
182 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); | 172 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); |
183 | 173 |
184 scoped_refptr<Predictor> testing_master( | 174 Predictor testing_master(true); |
185 new Predictor(host_resolver_.get(), | 175 testing_master.SetHostResolver(host_resolver_.get()); |
186 default_max_queueing_delay_, | |
187 PredictorInit::kMaxSpeculativeParallelResolves, | |
188 false)); | |
189 | 176 |
190 GURL goog("http://www.google.com:80"), | 177 GURL goog("http://www.google.com:80"), |
191 goog2("http://gmail.google.com.com:80"), | 178 goog2("http://gmail.google.com.com:80"), |
192 goog3("http://mail.google.com:80"), | 179 goog3("http://mail.google.com:80"), |
193 goog4("http://gmail.com:80"); | 180 goog4("http://gmail.com:80"); |
194 GURL bad1("http://bad1.notfound:80"), | 181 GURL bad1("http://bad1.notfound:80"), |
195 bad2("http://bad2.notfound:80"); | 182 bad2("http://bad2.notfound:80"); |
196 | 183 |
197 UrlList names; | 184 UrlList names; |
198 names.push_back(goog); | 185 names.push_back(goog); |
199 names.push_back(goog3); | 186 names.push_back(goog3); |
200 names.push_back(bad1); | 187 names.push_back(bad1); |
201 names.push_back(goog2); | 188 names.push_back(goog2); |
202 names.push_back(bad2); | 189 names.push_back(bad2); |
203 names.push_back(goog4); | 190 names.push_back(goog4); |
204 names.push_back(goog); | 191 names.push_back(goog); |
205 | 192 |
206 // Try to flood the predictor with many concurrent requests. | 193 // Try to flood the predictor with many concurrent requests. |
207 for (int i = 0; i < 10; i++) | 194 for (int i = 0; i < 10; i++) |
208 testing_master->ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); | 195 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); |
209 | 196 |
210 WaitForResolution(testing_master, names); | 197 WaitForResolution(&testing_master, names); |
211 | 198 |
212 EXPECT_TRUE(testing_master->WasFound(goog)); | 199 EXPECT_TRUE(testing_master.WasFound(goog)); |
213 EXPECT_TRUE(testing_master->WasFound(goog3)); | 200 EXPECT_TRUE(testing_master.WasFound(goog3)); |
214 EXPECT_TRUE(testing_master->WasFound(goog2)); | 201 EXPECT_TRUE(testing_master.WasFound(goog2)); |
215 EXPECT_TRUE(testing_master->WasFound(goog4)); | 202 EXPECT_TRUE(testing_master.WasFound(goog4)); |
216 EXPECT_FALSE(testing_master->WasFound(bad1)); | 203 EXPECT_FALSE(testing_master.WasFound(bad1)); |
217 EXPECT_FALSE(testing_master->WasFound(bad2)); | 204 EXPECT_FALSE(testing_master.WasFound(bad2)); |
218 | 205 |
219 MessageLoop::current()->RunAllPending(); | 206 MessageLoop::current()->RunAllPending(); |
220 | 207 |
221 EXPECT_FALSE(testing_master->WasFound(bad1)); | 208 EXPECT_FALSE(testing_master.WasFound(bad1)); |
222 EXPECT_FALSE(testing_master->WasFound(bad2)); | 209 EXPECT_FALSE(testing_master.WasFound(bad2)); |
223 | 210 |
224 EXPECT_LE(testing_master->peak_pending_lookups(), names.size()); | 211 EXPECT_LE(testing_master.peak_pending_lookups(), names.size()); |
225 EXPECT_LE(testing_master->peak_pending_lookups(), | 212 EXPECT_LE(testing_master.peak_pending_lookups(), |
226 testing_master->max_concurrent_dns_lookups()); | 213 testing_master.max_concurrent_dns_lookups()); |
227 | 214 |
228 testing_master->Shutdown(); | 215 testing_master.Shutdown(); |
229 } | 216 } |
230 | 217 |
231 TEST_F(PredictorTest, MassiveConcurrentLookupTest) { | 218 TEST_F(PredictorTest, MassiveConcurrentLookupTest) { |
232 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); | 219 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); |
233 | 220 |
234 scoped_refptr<Predictor> testing_master( | 221 Predictor testing_master(true); |
235 new Predictor(host_resolver_.get(), | 222 testing_master.SetHostResolver(host_resolver_.get()); |
236 default_max_queueing_delay_, | |
237 PredictorInit::kMaxSpeculativeParallelResolves, | |
238 false)); | |
239 | 223 |
240 UrlList names; | 224 UrlList names; |
241 for (int i = 0; i < 100; i++) | 225 for (int i = 0; i < 100; i++) |
242 names.push_back(GURL( | 226 names.push_back(GURL( |
243 "http://host" + base::IntToString(i) + ".notfound:80")); | 227 "http://host" + base::IntToString(i) + ".notfound:80")); |
244 | 228 |
245 // Try to flood the predictor with many concurrent requests. | 229 // Try to flood the predictor with many concurrent requests. |
246 for (int i = 0; i < 10; i++) | 230 for (int i = 0; i < 10; i++) |
247 testing_master->ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); | 231 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); |
248 | 232 |
249 WaitForResolution(testing_master, names); | 233 WaitForResolution(&testing_master, names); |
250 | 234 |
251 MessageLoop::current()->RunAllPending(); | 235 MessageLoop::current()->RunAllPending(); |
252 | 236 |
253 EXPECT_LE(testing_master->peak_pending_lookups(), names.size()); | 237 EXPECT_LE(testing_master.peak_pending_lookups(), names.size()); |
254 EXPECT_LE(testing_master->peak_pending_lookups(), | 238 EXPECT_LE(testing_master.peak_pending_lookups(), |
255 testing_master->max_concurrent_dns_lookups()); | 239 testing_master.max_concurrent_dns_lookups()); |
256 | 240 |
257 testing_master->Shutdown(); | 241 testing_master.Shutdown(); |
258 } | 242 } |
259 | 243 |
260 //------------------------------------------------------------------------------ | 244 //------------------------------------------------------------------------------ |
261 // Functions to help synthesize and test serializations of subresource referrer | 245 // Functions to help synthesize and test serializations of subresource referrer |
262 // lists. | 246 // lists. |
263 | 247 |
264 // Return a motivation_list if we can find one for the given motivating_host (or | 248 // Return a motivation_list if we can find one for the given motivating_host (or |
265 // NULL if a match is not found). | 249 // NULL if a match is not found). |
266 static ListValue* FindSerializationMotivation(const GURL& motivation, | 250 static ListValue* FindSerializationMotivation(const GURL& motivation, |
267 const ListValue& referral_list) { | 251 const ListValue& referral_list) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 return true; | 329 return true; |
346 } | 330 } |
347 } | 331 } |
348 return false; | 332 return false; |
349 } | 333 } |
350 | 334 |
351 //------------------------------------------------------------------------------ | 335 //------------------------------------------------------------------------------ |
352 | 336 |
353 // Make sure nil referral lists really have no entries, and no latency listed. | 337 // Make sure nil referral lists really have no entries, and no latency listed. |
354 TEST_F(PredictorTest, ReferrerSerializationNilTest) { | 338 TEST_F(PredictorTest, ReferrerSerializationNilTest) { |
355 scoped_refptr<Predictor> predictor( | 339 Predictor predictor(true); |
356 new Predictor(host_resolver_.get(), | 340 predictor.SetHostResolver(host_resolver_.get()); |
357 default_max_queueing_delay_, | 341 |
358 PredictorInit::kMaxSpeculativeParallelResolves, | |
359 false)); | |
360 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); | 342 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); |
361 predictor->SerializeReferrers(referral_list.get()); | 343 predictor.SerializeReferrers(referral_list.get()); |
362 EXPECT_EQ(1U, referral_list->GetSize()); | 344 EXPECT_EQ(1U, referral_list->GetSize()); |
363 EXPECT_FALSE(GetDataFromSerialization( | 345 EXPECT_FALSE(GetDataFromSerialization( |
364 GURL("http://a.com:79"), GURL("http://b.com:78"), | 346 GURL("http://a.com:79"), GURL("http://b.com:78"), |
365 *referral_list.get(), NULL)); | 347 *referral_list.get(), NULL)); |
366 | 348 |
367 predictor->Shutdown(); | 349 predictor.Shutdown(); |
368 } | 350 } |
369 | 351 |
370 // Make sure that when a serialization list includes a value, that it can be | 352 // Make sure that when a serialization list includes a value, that it can be |
371 // deserialized into the database, and can be extracted back out via | 353 // deserialized into the database, and can be extracted back out via |
372 // serialization without being changed. | 354 // serialization without being changed. |
373 TEST_F(PredictorTest, ReferrerSerializationSingleReferrerTest) { | 355 TEST_F(PredictorTest, ReferrerSerializationSingleReferrerTest) { |
374 scoped_refptr<Predictor> predictor( | 356 Predictor predictor(true); |
375 new Predictor(host_resolver_.get(), | 357 predictor.SetHostResolver(host_resolver_.get()); |
376 default_max_queueing_delay_, | |
377 PredictorInit::kMaxSpeculativeParallelResolves, | |
378 false)); | |
379 const GURL motivation_url("http://www.google.com:91"); | 358 const GURL motivation_url("http://www.google.com:91"); |
380 const GURL subresource_url("http://icons.google.com:90"); | 359 const GURL subresource_url("http://icons.google.com:90"); |
381 const double kUseRate = 23.4; | 360 const double kUseRate = 23.4; |
382 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); | 361 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); |
383 | 362 |
384 AddToSerializedList(motivation_url, subresource_url, | 363 AddToSerializedList(motivation_url, subresource_url, |
385 kUseRate, referral_list.get()); | 364 kUseRate, referral_list.get()); |
386 | 365 |
387 predictor->DeserializeReferrers(*referral_list.get()); | 366 predictor.DeserializeReferrers(*referral_list.get()); |
388 | 367 |
389 ListValue recovered_referral_list; | 368 ListValue recovered_referral_list; |
390 predictor->SerializeReferrers(&recovered_referral_list); | 369 predictor.SerializeReferrers(&recovered_referral_list); |
391 EXPECT_EQ(2U, recovered_referral_list.GetSize()); | 370 EXPECT_EQ(2U, recovered_referral_list.GetSize()); |
392 double rate; | 371 double rate; |
393 EXPECT_TRUE(GetDataFromSerialization( | 372 EXPECT_TRUE(GetDataFromSerialization( |
394 motivation_url, subresource_url, recovered_referral_list, &rate)); | 373 motivation_url, subresource_url, recovered_referral_list, &rate)); |
395 EXPECT_EQ(rate, kUseRate); | 374 EXPECT_EQ(rate, kUseRate); |
396 | 375 |
397 predictor->Shutdown(); | 376 predictor.Shutdown(); |
398 } | 377 } |
399 | 378 |
400 // Verify that two floats are within 1% of each other in value. | 379 // Verify that two floats are within 1% of each other in value. |
401 #define EXPECT_SIMILAR(a, b) do { \ | 380 #define EXPECT_SIMILAR(a, b) do { \ |
402 double espilon_ratio = 1.01; \ | 381 double espilon_ratio = 1.01; \ |
403 if ((a) < 0.) \ | 382 if ((a) < 0.) \ |
404 espilon_ratio = 1 / espilon_ratio; \ | 383 espilon_ratio = 1 / espilon_ratio; \ |
405 EXPECT_LT(a, espilon_ratio * (b)); \ | 384 EXPECT_LT(a, espilon_ratio * (b)); \ |
406 EXPECT_GT((a) * espilon_ratio, b); \ | 385 EXPECT_GT((a) * espilon_ratio, b); \ |
407 } while (0) | 386 } while (0) |
408 | 387 |
409 | 388 |
410 // Make sure the Trim() functionality works as expected. | 389 // Make sure the Trim() functionality works as expected. |
411 TEST_F(PredictorTest, ReferrerSerializationTrimTest) { | 390 TEST_F(PredictorTest, ReferrerSerializationTrimTest) { |
412 scoped_refptr<Predictor> predictor( | 391 Predictor predictor(true); |
413 new Predictor(host_resolver_.get(), | 392 predictor.SetHostResolver(host_resolver_.get()); |
414 default_max_queueing_delay_, | |
415 PredictorInit::kMaxSpeculativeParallelResolves, | |
416 false)); | |
417 GURL motivation_url("http://www.google.com:110"); | 393 GURL motivation_url("http://www.google.com:110"); |
418 | 394 |
419 GURL icon_subresource_url("http://icons.google.com:111"); | 395 GURL icon_subresource_url("http://icons.google.com:111"); |
420 const double kRateIcon = 16.0 * Predictor::kDiscardableExpectedValue; | 396 const double kRateIcon = 16.0 * Predictor::kDiscardableExpectedValue; |
421 GURL img_subresource_url("http://img.google.com:118"); | 397 GURL img_subresource_url("http://img.google.com:118"); |
422 const double kRateImg = 8.0 * Predictor::kDiscardableExpectedValue; | 398 const double kRateImg = 8.0 * Predictor::kDiscardableExpectedValue; |
423 | 399 |
424 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); | 400 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); |
425 AddToSerializedList( | 401 AddToSerializedList( |
426 motivation_url, icon_subresource_url, kRateIcon, referral_list.get()); | 402 motivation_url, icon_subresource_url, kRateIcon, referral_list.get()); |
427 AddToSerializedList( | 403 AddToSerializedList( |
428 motivation_url, img_subresource_url, kRateImg, referral_list.get()); | 404 motivation_url, img_subresource_url, kRateImg, referral_list.get()); |
429 | 405 |
430 predictor->DeserializeReferrers(*referral_list.get()); | 406 predictor.DeserializeReferrers(*referral_list.get()); |
431 | 407 |
432 ListValue recovered_referral_list; | 408 ListValue recovered_referral_list; |
433 predictor->SerializeReferrers(&recovered_referral_list); | 409 predictor.SerializeReferrers(&recovered_referral_list); |
434 EXPECT_EQ(2U, recovered_referral_list.GetSize()); | 410 EXPECT_EQ(2U, recovered_referral_list.GetSize()); |
435 double rate; | 411 double rate; |
436 EXPECT_TRUE(GetDataFromSerialization( | 412 EXPECT_TRUE(GetDataFromSerialization( |
437 motivation_url, icon_subresource_url, recovered_referral_list, | 413 motivation_url, icon_subresource_url, recovered_referral_list, |
438 &rate)); | 414 &rate)); |
439 EXPECT_SIMILAR(rate, kRateIcon); | 415 EXPECT_SIMILAR(rate, kRateIcon); |
440 | 416 |
441 EXPECT_TRUE(GetDataFromSerialization( | 417 EXPECT_TRUE(GetDataFromSerialization( |
442 motivation_url, img_subresource_url, recovered_referral_list, &rate)); | 418 motivation_url, img_subresource_url, recovered_referral_list, &rate)); |
443 EXPECT_SIMILAR(rate, kRateImg); | 419 EXPECT_SIMILAR(rate, kRateImg); |
444 | 420 |
445 // Each time we Trim 24 times, the user_rate figures should reduce by a factor | 421 // Each time we Trim 24 times, the user_rate figures should reduce by a factor |
446 // of two, until they are small, and then a trim will delete the whole entry. | 422 // of two, until they are small, and then a trim will delete the whole entry. |
447 for (int i = 0; i < 24; ++i) | 423 for (int i = 0; i < 24; ++i) |
448 predictor->TrimReferrersNow(); | 424 predictor.TrimReferrersNow(); |
449 predictor->SerializeReferrers(&recovered_referral_list); | 425 predictor.SerializeReferrers(&recovered_referral_list); |
450 EXPECT_EQ(2U, recovered_referral_list.GetSize()); | 426 EXPECT_EQ(2U, recovered_referral_list.GetSize()); |
451 EXPECT_TRUE(GetDataFromSerialization( | 427 EXPECT_TRUE(GetDataFromSerialization( |
452 motivation_url, icon_subresource_url, recovered_referral_list, &rate)); | 428 motivation_url, icon_subresource_url, recovered_referral_list, &rate)); |
453 EXPECT_SIMILAR(rate, kRateIcon / 2); | 429 EXPECT_SIMILAR(rate, kRateIcon / 2); |
454 | 430 |
455 EXPECT_TRUE(GetDataFromSerialization( | 431 EXPECT_TRUE(GetDataFromSerialization( |
456 motivation_url, img_subresource_url, recovered_referral_list, &rate)); | 432 motivation_url, img_subresource_url, recovered_referral_list, &rate)); |
457 EXPECT_SIMILAR(rate, kRateImg / 2); | 433 EXPECT_SIMILAR(rate, kRateImg / 2); |
458 | 434 |
459 for (int i = 0; i < 24; ++i) | 435 for (int i = 0; i < 24; ++i) |
460 predictor->TrimReferrersNow(); | 436 predictor.TrimReferrersNow(); |
461 predictor->SerializeReferrers(&recovered_referral_list); | 437 predictor.SerializeReferrers(&recovered_referral_list); |
462 EXPECT_EQ(2U, recovered_referral_list.GetSize()); | 438 EXPECT_EQ(2U, recovered_referral_list.GetSize()); |
463 EXPECT_TRUE(GetDataFromSerialization( | 439 EXPECT_TRUE(GetDataFromSerialization( |
464 motivation_url, icon_subresource_url, recovered_referral_list, &rate)); | 440 motivation_url, icon_subresource_url, recovered_referral_list, &rate)); |
465 EXPECT_SIMILAR(rate, kRateIcon / 4); | 441 EXPECT_SIMILAR(rate, kRateIcon / 4); |
466 EXPECT_TRUE(GetDataFromSerialization( | 442 EXPECT_TRUE(GetDataFromSerialization( |
467 motivation_url, img_subresource_url, recovered_referral_list, &rate)); | 443 motivation_url, img_subresource_url, recovered_referral_list, &rate)); |
468 EXPECT_SIMILAR(rate, kRateImg / 4); | 444 EXPECT_SIMILAR(rate, kRateImg / 4); |
469 | 445 |
470 for (int i = 0; i < 24; ++i) | 446 for (int i = 0; i < 24; ++i) |
471 predictor->TrimReferrersNow(); | 447 predictor.TrimReferrersNow(); |
472 predictor->SerializeReferrers(&recovered_referral_list); | 448 predictor.SerializeReferrers(&recovered_referral_list); |
473 EXPECT_EQ(2U, recovered_referral_list.GetSize()); | 449 EXPECT_EQ(2U, recovered_referral_list.GetSize()); |
474 EXPECT_TRUE(GetDataFromSerialization( | 450 EXPECT_TRUE(GetDataFromSerialization( |
475 motivation_url, icon_subresource_url, recovered_referral_list, &rate)); | 451 motivation_url, icon_subresource_url, recovered_referral_list, &rate)); |
476 EXPECT_SIMILAR(rate, kRateIcon / 8); | 452 EXPECT_SIMILAR(rate, kRateIcon / 8); |
477 | 453 |
478 // Img is below threshold, and so it gets deleted. | 454 // Img is below threshold, and so it gets deleted. |
479 EXPECT_FALSE(GetDataFromSerialization( | 455 EXPECT_FALSE(GetDataFromSerialization( |
480 motivation_url, img_subresource_url, recovered_referral_list, &rate)); | 456 motivation_url, img_subresource_url, recovered_referral_list, &rate)); |
481 | 457 |
482 for (int i = 0; i < 24; ++i) | 458 for (int i = 0; i < 24; ++i) |
483 predictor->TrimReferrersNow(); | 459 predictor.TrimReferrersNow(); |
484 predictor->SerializeReferrers(&recovered_referral_list); | 460 predictor.SerializeReferrers(&recovered_referral_list); |
485 // Icon is also trimmed away, so entire set gets discarded. | 461 // Icon is also trimmed away, so entire set gets discarded. |
486 EXPECT_EQ(1U, recovered_referral_list.GetSize()); | 462 EXPECT_EQ(1U, recovered_referral_list.GetSize()); |
487 EXPECT_FALSE(GetDataFromSerialization( | 463 EXPECT_FALSE(GetDataFromSerialization( |
488 motivation_url, icon_subresource_url, recovered_referral_list, &rate)); | 464 motivation_url, icon_subresource_url, recovered_referral_list, &rate)); |
489 EXPECT_FALSE(GetDataFromSerialization( | 465 EXPECT_FALSE(GetDataFromSerialization( |
490 motivation_url, img_subresource_url, recovered_referral_list, &rate)); | 466 motivation_url, img_subresource_url, recovered_referral_list, &rate)); |
491 | 467 |
492 predictor->Shutdown(); | 468 predictor.Shutdown(); |
493 } | 469 } |
494 | 470 |
495 | 471 |
496 TEST_F(PredictorTest, PriorityQueuePushPopTest) { | 472 TEST_F(PredictorTest, PriorityQueuePushPopTest) { |
497 Predictor::HostNameQueue queue; | 473 Predictor::HostNameQueue queue; |
498 | 474 |
499 GURL first("http://first:80"), second("http://second:90"); | 475 GURL first("http://first:80"), second("http://second:90"); |
500 | 476 |
501 // First check high priority queue FIFO functionality. | 477 // First check high priority queue FIFO functionality. |
502 EXPECT_TRUE(queue.IsEmpty()); | 478 EXPECT_TRUE(queue.IsEmpty()); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 EXPECT_NE(Predictor::CanonicalizeUrl(http), | 570 EXPECT_NE(Predictor::CanonicalizeUrl(http), |
595 Predictor::CanonicalizeUrl(https)); | 571 Predictor::CanonicalizeUrl(https)); |
596 | 572 |
597 // Https works fine. | 573 // Https works fine. |
598 GURL long_https("https://host:999/path?query=value"); | 574 GURL long_https("https://host:999/path?query=value"); |
599 EXPECT_EQ(Predictor::CanonicalizeUrl(long_https), | 575 EXPECT_EQ(Predictor::CanonicalizeUrl(long_https), |
600 long_https.GetWithEmptyPath()); | 576 long_https.GetWithEmptyPath()); |
601 } | 577 } |
602 | 578 |
603 TEST_F(PredictorTest, DiscardPredictorResults) { | 579 TEST_F(PredictorTest, DiscardPredictorResults) { |
604 scoped_refptr<Predictor> predictor( | 580 Predictor predictor(true); |
605 new Predictor(host_resolver_.get(), | 581 predictor.SetHostResolver(host_resolver_.get()); |
606 default_max_queueing_delay_, | |
607 PredictorInit::kMaxSpeculativeParallelResolves, | |
608 false)); | |
609 ListValue referral_list; | 582 ListValue referral_list; |
610 predictor->SerializeReferrers(&referral_list); | 583 predictor.SerializeReferrers(&referral_list); |
611 EXPECT_EQ(1U, referral_list.GetSize()); | 584 EXPECT_EQ(1U, referral_list.GetSize()); |
612 | 585 |
613 GURL host_1("http://test_1"); | 586 GURL host_1("http://test_1"); |
614 GURL host_2("http://test_2"); | 587 GURL host_2("http://test_2"); |
615 predictor->LearnFromNavigation(host_1, host_2); | 588 predictor.LearnFromNavigation(host_1, host_2); |
616 | 589 |
617 predictor->SerializeReferrers(&referral_list); | 590 predictor.SerializeReferrers(&referral_list); |
618 EXPECT_EQ(2U, referral_list.GetSize()); | 591 EXPECT_EQ(2U, referral_list.GetSize()); |
619 | 592 |
620 predictor->DiscardAllResults(); | 593 predictor.DiscardAllResults(); |
621 predictor->SerializeReferrers(&referral_list); | 594 predictor.SerializeReferrers(&referral_list); |
622 EXPECT_EQ(1U, referral_list.GetSize()); | 595 EXPECT_EQ(1U, referral_list.GetSize()); |
623 | 596 |
624 predictor->Shutdown(); | 597 predictor.Shutdown(); |
625 } | 598 } |
626 | 599 |
627 } // namespace chrome_browser_net | 600 } // namespace chrome_browser_net |
OLD | NEW |