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

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

Issue 1355063004: Template methods on Timer classes instead of the classes themselves. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: timer: . Created 5 years, 3 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 <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 21 matching lines...) Expand all
32 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
33 33
34 using base::Time; 34 using base::Time;
35 using base::TimeDelta; 35 using base::TimeDelta;
36 using content::BrowserThread; 36 using content::BrowserThread;
37 37
38 namespace chrome_browser_net { 38 namespace chrome_browser_net {
39 39
40 class WaitForResolutionHelper; 40 class WaitForResolutionHelper;
41 41
42 typedef base::RepeatingTimer<WaitForResolutionHelper> HelperTimer;
danakj 2015/09/22 00:49:27 I dropped this typedef also, and just use a Repeat
43
44 class WaitForResolutionHelper { 42 class WaitForResolutionHelper {
45 public: 43 public:
46 WaitForResolutionHelper(Predictor* predictor, const UrlList& hosts, 44 WaitForResolutionHelper(Predictor* predictor,
47 HelperTimer* timer, int checks_until_quit) 45 const UrlList& hosts,
46 base::RepeatingTimer* timer,
47 int checks_until_quit)
48 : predictor_(predictor), 48 : predictor_(predictor),
49 hosts_(hosts), 49 hosts_(hosts),
50 timer_(timer), 50 timer_(timer),
51 checks_until_quit_(checks_until_quit) { 51 checks_until_quit_(checks_until_quit) {}
52 }
53 52
54 void CheckIfResolutionsDone() { 53 void CheckIfResolutionsDone() {
55 if (--checks_until_quit_ > 0) { 54 if (--checks_until_quit_ > 0) {
56 for (UrlList::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i) 55 for (UrlList::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i)
57 if (predictor_->GetResolutionDuration(*i) == 56 if (predictor_->GetResolutionDuration(*i) ==
58 UrlInfo::NullDuration()) 57 UrlInfo::NullDuration())
59 return; // We don't have resolution for that host. 58 return; // We don't have resolution for that host.
60 } 59 }
61 60
62 // When all hostnames have been resolved, or we've hit the limit, 61 // When all hostnames have been resolved, or we've hit the limit,
63 // exit the loop. 62 // exit the loop.
64 timer_->Stop(); 63 timer_->Stop();
65 base::MessageLoop::current()->Quit(); 64 base::MessageLoop::current()->Quit();
66 delete timer_; 65 delete timer_;
67 delete this; 66 delete this;
68 } 67 }
69 68
70 private: 69 private:
71 Predictor* predictor_; 70 Predictor* predictor_;
72 const UrlList hosts_; 71 const UrlList hosts_;
73 HelperTimer* timer_; 72 base::RepeatingTimer* timer_;
74 int checks_until_quit_; 73 int checks_until_quit_;
75 }; 74 };
76 75
77 class PredictorTest : public testing::Test { 76 class PredictorTest : public testing::Test {
78 public: 77 public:
79 PredictorTest() 78 PredictorTest()
80 : ui_thread_(BrowserThread::UI, &loop_), 79 : ui_thread_(BrowserThread::UI, &loop_),
81 io_thread_(BrowserThread::IO, &loop_), 80 io_thread_(BrowserThread::IO, &loop_),
82 host_resolver_(new net::MockCachingHostResolver()) { 81 host_resolver_(new net::MockCachingHostResolver()) {
83 } 82 }
(...skipping 11 matching lines...) Expand all
95 // only be incurred by the first request, after which the result will be 94 // only be incurred by the first request, after which the result will be
96 // cached internally by |host_resolver_|. 95 // cached internally by |host_resolver_|.
97 net::RuleBasedHostResolverProc* rules = host_resolver_->rules(); 96 net::RuleBasedHostResolverProc* rules = host_resolver_->rules();
98 rules->AddRuleWithLatency("www.google.com", "127.0.0.1", 50); 97 rules->AddRuleWithLatency("www.google.com", "127.0.0.1", 50);
99 rules->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70); 98 rules->AddRuleWithLatency("gmail.google.com.com", "127.0.0.1", 70);
100 rules->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44); 99 rules->AddRuleWithLatency("mail.google.com", "127.0.0.1", 44);
101 rules->AddRuleWithLatency("gmail.com", "127.0.0.1", 63); 100 rules->AddRuleWithLatency("gmail.com", "127.0.0.1", 63);
102 } 101 }
103 102
104 void WaitForResolution(Predictor* predictor, const UrlList& hosts) { 103 void WaitForResolution(Predictor* predictor, const UrlList& hosts) {
105 HelperTimer* timer = new HelperTimer(); 104 base::RepeatingTimer* timer = new base::RepeatingTimer();
106 // By default allow the loop to run for a minute -- 600 iterations. 105 // By default allow the loop to run for a minute -- 600 iterations.
107 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100), 106 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100),
108 new WaitForResolutionHelper(predictor, hosts, timer, 600), 107 new WaitForResolutionHelper(predictor, hosts, timer, 600),
109 &WaitForResolutionHelper::CheckIfResolutionsDone); 108 &WaitForResolutionHelper::CheckIfResolutionsDone);
110 base::MessageLoop::current()->Run(); 109 base::MessageLoop::current()->Run();
111 } 110 }
112 111
113 void WaitForResolutionWithLimit( 112 void WaitForResolutionWithLimit(
114 Predictor* predictor, const UrlList& hosts, int limit) { 113 Predictor* predictor, const UrlList& hosts, int limit) {
115 HelperTimer* timer = new HelperTimer(); 114 base::RepeatingTimer* timer = new base::RepeatingTimer();
116 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100), 115 timer->Start(FROM_HERE, TimeDelta::FromMilliseconds(100),
117 new WaitForResolutionHelper(predictor, hosts, timer, limit), 116 new WaitForResolutionHelper(predictor, hosts, timer, limit),
118 &WaitForResolutionHelper::CheckIfResolutionsDone); 117 &WaitForResolutionHelper::CheckIfResolutionsDone);
119 base::MessageLoop::current()->Run(); 118 base::MessageLoop::current()->Run();
120 } 119 }
121 120
122 private: 121 private:
123 // IMPORTANT: do not move this below |host_resolver_|; the host resolver 122 // IMPORTANT: do not move this below |host_resolver_|; the host resolver
124 // must not outlive the message loop, otherwise bad things can happen 123 // must not outlive the message loop, otherwise bad things can happen
125 // (like posting to a deleted message loop). 124 // (like posting to a deleted message loop).
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 testing_master.Resolve(goog, UrlInfo::OMNIBOX_MOTIVATED); 837 testing_master.Resolve(goog, UrlInfo::OMNIBOX_MOTIVATED);
839 838
840 // Proxy may not be in use (the PAC script has not yet been evaluated), so the 839 // Proxy may not be in use (the PAC script has not yet been evaluated), so the
841 // name has been registered for pre-resolve. 840 // name has been registered for pre-resolve.
842 EXPECT_FALSE(testing_master.work_queue_.IsEmpty()); 841 EXPECT_FALSE(testing_master.work_queue_.IsEmpty());
843 842
844 testing_master.Shutdown(); 843 testing_master.Shutdown();
845 } 844 }
846 845
847 } // namespace chrome_browser_net 846 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698