OLD | NEW |
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 <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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 } | 42 } |
43 | 43 |
44 void Run() { | 44 void Run() { |
45 for (UrlList::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i) | 45 for (UrlList::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i) |
46 if (predictor_->GetResolutionDuration(*i) == | 46 if (predictor_->GetResolutionDuration(*i) == |
47 UrlInfo::NullDuration()) | 47 UrlInfo::NullDuration()) |
48 return; // We don't have resolution for that host. | 48 return; // We don't have resolution for that host. |
49 | 49 |
50 // When all hostnames have been resolved, exit the loop. | 50 // When all hostnames have been resolved, exit the loop. |
51 timer_->Stop(); | 51 timer_->Stop(); |
52 MessageLoop::current()->Quit(); | 52 base::MessageLoop::current()->Quit(); |
53 delete timer_; | 53 delete timer_; |
54 delete this; | 54 delete this; |
55 } | 55 } |
56 | 56 |
57 private: | 57 private: |
58 Predictor* predictor_; | 58 Predictor* predictor_; |
59 const UrlList hosts_; | 59 const UrlList hosts_; |
60 HelperTimer* timer_; | 60 HelperTimer* timer_; |
61 }; | 61 }; |
62 | 62 |
(...skipping 22 matching lines...) Expand all Loading... |
85 rules->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70); | 85 rules->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70); |
86 rules->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44); | 86 rules->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44); |
87 rules->AddRuleWithLatency("gmail.com", "127.0.0.1", 63); | 87 rules->AddRuleWithLatency("gmail.com", "127.0.0.1", 63); |
88 } | 88 } |
89 | 89 |
90 void WaitForResolution(Predictor* predictor, const UrlList& hosts) { | 90 void WaitForResolution(Predictor* predictor, const UrlList& hosts) { |
91 HelperTimer* timer = new HelperTimer(); | 91 HelperTimer* timer = new HelperTimer(); |
92 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100), | 92 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100), |
93 new WaitForResolutionHelper(predictor, hosts, timer), | 93 new WaitForResolutionHelper(predictor, hosts, timer), |
94 &WaitForResolutionHelper::Run); | 94 &WaitForResolutionHelper::Run); |
95 MessageLoop::current()->Run(); | 95 base::MessageLoop::current()->Run(); |
96 } | 96 } |
97 | 97 |
98 private: | 98 private: |
99 // IMPORTANT: do not move this below |host_resolver_|; the host resolver | 99 // IMPORTANT: do not move this below |host_resolver_|; the host resolver |
100 // must not outlive the message loop, otherwise bad things can happen | 100 // must not outlive the message loop, otherwise bad things can happen |
101 // (like posting to a deleted message loop). | 101 // (like posting to a deleted message loop). |
102 MessageLoopForUI loop_; | 102 base::MessageLoopForUI loop_; |
103 content::TestBrowserThread ui_thread_; | 103 content::TestBrowserThread ui_thread_; |
104 content::TestBrowserThread io_thread_; | 104 content::TestBrowserThread io_thread_; |
105 | 105 |
106 protected: | 106 protected: |
107 scoped_ptr<net::MockCachingHostResolver> host_resolver_; | 107 scoped_ptr<net::MockCachingHostResolver> host_resolver_; |
108 }; | 108 }; |
109 | 109 |
110 //------------------------------------------------------------------------------ | 110 //------------------------------------------------------------------------------ |
111 | 111 |
112 TEST_F(PredictorTest, StartupShutdownTest) { | 112 TEST_F(PredictorTest, StartupShutdownTest) { |
113 Predictor testing_master(true); | 113 Predictor testing_master(true); |
114 testing_master.Shutdown(); | 114 testing_master.Shutdown(); |
115 } | 115 } |
116 | 116 |
117 | 117 |
118 TEST_F(PredictorTest, ShutdownWhenResolutionIsPendingTest) { | 118 TEST_F(PredictorTest, ShutdownWhenResolutionIsPendingTest) { |
119 scoped_ptr<net::HostResolver> host_resolver(new net::HangingHostResolver()); | 119 scoped_ptr<net::HostResolver> host_resolver(new net::HangingHostResolver()); |
120 | 120 |
121 Predictor testing_master(true); | 121 Predictor testing_master(true); |
122 testing_master.SetHostResolver(host_resolver.get()); | 122 testing_master.SetHostResolver(host_resolver.get()); |
123 | 123 |
124 GURL localhost("http://localhost:80"); | 124 GURL localhost("http://localhost:80"); |
125 UrlList names; | 125 UrlList names; |
126 names.push_back(localhost); | 126 names.push_back(localhost); |
127 | 127 |
128 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); | 128 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); |
129 | 129 |
130 MessageLoop::current()->PostDelayedTask( | 130 base::MessageLoop::current()->PostDelayedTask( |
131 FROM_HERE, | 131 FROM_HERE, |
132 MessageLoop::QuitClosure(), | 132 base::MessageLoop::QuitClosure(), |
133 base::TimeDelta::FromMilliseconds(500)); | 133 base::TimeDelta::FromMilliseconds(500)); |
134 MessageLoop::current()->Run(); | 134 base::MessageLoop::current()->Run(); |
135 | 135 |
136 EXPECT_FALSE(testing_master.WasFound(localhost)); | 136 EXPECT_FALSE(testing_master.WasFound(localhost)); |
137 | 137 |
138 testing_master.Shutdown(); | 138 testing_master.Shutdown(); |
139 | 139 |
140 // Clean up after ourselves. | 140 // Clean up after ourselves. |
141 MessageLoop::current()->RunUntilIdle(); | 141 base::MessageLoop::current()->RunUntilIdle(); |
142 } | 142 } |
143 | 143 |
144 TEST_F(PredictorTest, SingleLookupTest) { | 144 TEST_F(PredictorTest, SingleLookupTest) { |
145 Predictor testing_master(true); | 145 Predictor testing_master(true); |
146 testing_master.SetHostResolver(host_resolver_.get()); | 146 testing_master.SetHostResolver(host_resolver_.get()); |
147 | 147 |
148 GURL goog("http://www.google.com:80"); | 148 GURL goog("http://www.google.com:80"); |
149 | 149 |
150 UrlList names; | 150 UrlList names; |
151 names.push_back(goog); | 151 names.push_back(goog); |
152 | 152 |
153 // Try to flood the predictor with many concurrent requests. | 153 // Try to flood the predictor with many concurrent requests. |
154 for (int i = 0; i < 10; i++) | 154 for (int i = 0; i < 10; i++) |
155 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); | 155 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); |
156 | 156 |
157 WaitForResolution(&testing_master, names); | 157 WaitForResolution(&testing_master, names); |
158 | 158 |
159 EXPECT_TRUE(testing_master.WasFound(goog)); | 159 EXPECT_TRUE(testing_master.WasFound(goog)); |
160 | 160 |
161 MessageLoop::current()->RunUntilIdle(); | 161 base::MessageLoop::current()->RunUntilIdle(); |
162 | 162 |
163 EXPECT_GT(testing_master.peak_pending_lookups(), names.size() / 2); | 163 EXPECT_GT(testing_master.peak_pending_lookups(), names.size() / 2); |
164 EXPECT_LE(testing_master.peak_pending_lookups(), names.size()); | 164 EXPECT_LE(testing_master.peak_pending_lookups(), names.size()); |
165 EXPECT_LE(testing_master.peak_pending_lookups(), | 165 EXPECT_LE(testing_master.peak_pending_lookups(), |
166 testing_master.max_concurrent_dns_lookups()); | 166 testing_master.max_concurrent_dns_lookups()); |
167 | 167 |
168 testing_master.Shutdown(); | 168 testing_master.Shutdown(); |
169 } | 169 } |
170 | 170 |
171 TEST_F(PredictorTest, ConcurrentLookupTest) { | 171 TEST_F(PredictorTest, ConcurrentLookupTest) { |
(...skipping 24 matching lines...) Expand all Loading... |
196 | 196 |
197 WaitForResolution(&testing_master, names); | 197 WaitForResolution(&testing_master, names); |
198 | 198 |
199 EXPECT_TRUE(testing_master.WasFound(goog)); | 199 EXPECT_TRUE(testing_master.WasFound(goog)); |
200 EXPECT_TRUE(testing_master.WasFound(goog3)); | 200 EXPECT_TRUE(testing_master.WasFound(goog3)); |
201 EXPECT_TRUE(testing_master.WasFound(goog2)); | 201 EXPECT_TRUE(testing_master.WasFound(goog2)); |
202 EXPECT_TRUE(testing_master.WasFound(goog4)); | 202 EXPECT_TRUE(testing_master.WasFound(goog4)); |
203 EXPECT_FALSE(testing_master.WasFound(bad1)); | 203 EXPECT_FALSE(testing_master.WasFound(bad1)); |
204 EXPECT_FALSE(testing_master.WasFound(bad2)); | 204 EXPECT_FALSE(testing_master.WasFound(bad2)); |
205 | 205 |
206 MessageLoop::current()->RunUntilIdle(); | 206 base::MessageLoop::current()->RunUntilIdle(); |
207 | 207 |
208 EXPECT_FALSE(testing_master.WasFound(bad1)); | 208 EXPECT_FALSE(testing_master.WasFound(bad1)); |
209 EXPECT_FALSE(testing_master.WasFound(bad2)); | 209 EXPECT_FALSE(testing_master.WasFound(bad2)); |
210 | 210 |
211 EXPECT_LE(testing_master.peak_pending_lookups(), names.size()); | 211 EXPECT_LE(testing_master.peak_pending_lookups(), names.size()); |
212 EXPECT_LE(testing_master.peak_pending_lookups(), | 212 EXPECT_LE(testing_master.peak_pending_lookups(), |
213 testing_master.max_concurrent_dns_lookups()); | 213 testing_master.max_concurrent_dns_lookups()); |
214 | 214 |
215 testing_master.Shutdown(); | 215 testing_master.Shutdown(); |
216 } | 216 } |
217 | 217 |
218 TEST_F(PredictorTest, MassiveConcurrentLookupTest) { | 218 TEST_F(PredictorTest, MassiveConcurrentLookupTest) { |
219 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); | 219 host_resolver_->rules()->AddSimulatedFailure("*.notfound"); |
220 | 220 |
221 Predictor testing_master(true); | 221 Predictor testing_master(true); |
222 testing_master.SetHostResolver(host_resolver_.get()); | 222 testing_master.SetHostResolver(host_resolver_.get()); |
223 | 223 |
224 UrlList names; | 224 UrlList names; |
225 for (int i = 0; i < 100; i++) | 225 for (int i = 0; i < 100; i++) |
226 names.push_back(GURL( | 226 names.push_back(GURL( |
227 "http://host" + base::IntToString(i) + ".notfound:80")); | 227 "http://host" + base::IntToString(i) + ".notfound:80")); |
228 | 228 |
229 // Try to flood the predictor with many concurrent requests. | 229 // Try to flood the predictor with many concurrent requests. |
230 for (int i = 0; i < 10; i++) | 230 for (int i = 0; i < 10; i++) |
231 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); | 231 testing_master.ResolveList(names, UrlInfo::PAGE_SCAN_MOTIVATED); |
232 | 232 |
233 WaitForResolution(&testing_master, names); | 233 WaitForResolution(&testing_master, names); |
234 | 234 |
235 MessageLoop::current()->RunUntilIdle(); | 235 base::MessageLoop::current()->RunUntilIdle(); |
236 | 236 |
237 EXPECT_LE(testing_master.peak_pending_lookups(), names.size()); | 237 EXPECT_LE(testing_master.peak_pending_lookups(), names.size()); |
238 EXPECT_LE(testing_master.peak_pending_lookups(), | 238 EXPECT_LE(testing_master.peak_pending_lookups(), |
239 testing_master.max_concurrent_dns_lookups()); | 239 testing_master.max_concurrent_dns_lookups()); |
240 | 240 |
241 testing_master.Shutdown(); | 241 testing_master.Shutdown(); |
242 } | 242 } |
243 | 243 |
244 //------------------------------------------------------------------------------ | 244 //------------------------------------------------------------------------------ |
245 // Functions to help synthesize and test serializations of subresource referrer | 245 // Functions to help synthesize and test serializations of subresource referrer |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 EXPECT_EQ(2U, referral_list.GetSize()); | 675 EXPECT_EQ(2U, referral_list.GetSize()); |
676 | 676 |
677 predictor.DiscardAllResults(); | 677 predictor.DiscardAllResults(); |
678 predictor.SerializeReferrers(&referral_list); | 678 predictor.SerializeReferrers(&referral_list); |
679 EXPECT_EQ(1U, referral_list.GetSize()); | 679 EXPECT_EQ(1U, referral_list.GetSize()); |
680 | 680 |
681 predictor.Shutdown(); | 681 predictor.Shutdown(); |
682 } | 682 } |
683 | 683 |
684 } // namespace chrome_browser_net | 684 } // namespace chrome_browser_net |
OLD | NEW |