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