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

Side by Side Diff: net/proxy/dhcp_proxy_script_adapter_fetcher_win_unittest.cc

Issue 10692155: Switch to TimeDelta interfaces for process waiting functions in net and ipc. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 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
« no previous file with comments | « net/dns/dns_transaction_unittest.cc ('k') | net/proxy/dhcp_proxy_script_fetcher_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" 5 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h"
6 6
7 #include "base/perftimer.h" 7 #include "base/perftimer.h"
8 #include "base/synchronization/waitable_event.h" 8 #include "base/synchronization/waitable_event.h"
9 #include "base/test/test_timeouts.h" 9 #include "base/test/test_timeouts.h"
10 #include "base/timer.h" 10 #include "base/timer.h"
(...skipping 17 matching lines...) Expand all
28 // APIs and the network. In this file we test only by stubbing out 28 // APIs and the network. In this file we test only by stubbing out
29 // functionality. 29 // functionality.
30 30
31 // Version of DhcpProxyScriptAdapterFetcher that mocks out dependencies 31 // Version of DhcpProxyScriptAdapterFetcher that mocks out dependencies
32 // to allow unit testing. 32 // to allow unit testing.
33 class MockDhcpProxyScriptAdapterFetcher 33 class MockDhcpProxyScriptAdapterFetcher
34 : public DhcpProxyScriptAdapterFetcher { 34 : public DhcpProxyScriptAdapterFetcher {
35 public: 35 public:
36 explicit MockDhcpProxyScriptAdapterFetcher(URLRequestContext* context) 36 explicit MockDhcpProxyScriptAdapterFetcher(URLRequestContext* context)
37 : DhcpProxyScriptAdapterFetcher(context), 37 : DhcpProxyScriptAdapterFetcher(context),
38 dhcp_delay_ms_(1), 38 dhcp_delay_(base::TimeDelta::FromMilliseconds(1)),
39 timeout_ms_(TestTimeouts::action_timeout_ms()), 39 timeout_(TestTimeouts::action_timeout()),
40 configured_url_(kPacUrl), 40 configured_url_(kPacUrl),
41 fetcher_delay_ms_(1), 41 fetcher_delay_ms_(1),
42 fetcher_result_(OK), 42 fetcher_result_(OK),
43 pac_script_("bingo") { 43 pac_script_("bingo") {
44 } 44 }
45 45
46 void Cancel() { 46 void Cancel() {
47 DhcpProxyScriptAdapterFetcher::Cancel(); 47 DhcpProxyScriptAdapterFetcher::Cancel();
48 fetcher_ = NULL; 48 fetcher_ = NULL;
49 } 49 }
(...skipping 18 matching lines...) Expand all
68 } 68 }
69 69
70 std::string ImplGetPacURLFromDhcp( 70 std::string ImplGetPacURLFromDhcp(
71 const std::string& adapter_name) OVERRIDE { 71 const std::string& adapter_name) OVERRIDE {
72 PerfTimer timer; 72 PerfTimer timer;
73 test_finished_event_.TimedWait(dhcp_delay_); 73 test_finished_event_.TimedWait(dhcp_delay_);
74 return configured_url_; 74 return configured_url_;
75 } 75 }
76 76
77 base::WaitableEvent test_finished_event_; 77 base::WaitableEvent test_finished_event_;
78 TimeDelta dhcp_delay_; 78 base::TimeDelta dhcp_delay_;
79 std::string configured_url_; 79 std::string configured_url_;
80 }; 80 };
81 81
82 virtual DhcpQuery* ImplCreateDhcpQuery() OVERRIDE { 82 virtual DhcpQuery* ImplCreateDhcpQuery() OVERRIDE {
83 dhcp_query_ = new DelayingDhcpQuery(); 83 dhcp_query_ = new DelayingDhcpQuery();
84 dhcp_query_->dhcp_delay_ = TimeDelta::FromMilliseconds(dhcp_delay_ms_); 84 dhcp_query_->dhcp_delay_ = dhcp_delay_;
85 dhcp_query_->configured_url_ = configured_url_; 85 dhcp_query_->configured_url_ = configured_url_;
86 return dhcp_query_; 86 return dhcp_query_;
87 } 87 }
88 88
89 // Use a shorter timeout so tests can finish more quickly. 89 // Use a shorter timeout so tests can finish more quickly.
90 virtual base::TimeDelta ImplGetTimeout() const OVERRIDE { 90 virtual base::TimeDelta ImplGetTimeout() const OVERRIDE {
91 return base::TimeDelta::FromMilliseconds(timeout_ms_); 91 return timeout_;
92 } 92 }
93 93
94 void OnFetcherTimer() { 94 void OnFetcherTimer() {
95 // Note that there is an assumption by this mock implementation that 95 // Note that there is an assumption by this mock implementation that
96 // DhcpProxyScriptAdapterFetcher::Fetch will call ImplCreateScriptFetcher 96 // DhcpProxyScriptAdapterFetcher::Fetch will call ImplCreateScriptFetcher
97 // and call Fetch on the fetcher before the message loop is re-entered. 97 // and call Fetch on the fetcher before the message loop is re-entered.
98 // This holds true today, but if you hit this DCHECK the problem can 98 // This holds true today, but if you hit this DCHECK the problem can
99 // possibly be resolved by having a separate subclass of 99 // possibly be resolved by having a separate subclass of
100 // MockProxyScriptFetcher that adds the delay internally (instead of 100 // MockProxyScriptFetcher that adds the delay internally (instead of
101 // the simple approach currently used in ImplCreateScriptFetcher above). 101 // the simple approach currently used in ImplCreateScriptFetcher above).
102 DCHECK(fetcher_ && fetcher_->has_pending_request()); 102 DCHECK(fetcher_ && fetcher_->has_pending_request());
103 fetcher_->NotifyFetchCompletion(fetcher_result_, pac_script_); 103 fetcher_->NotifyFetchCompletion(fetcher_result_, pac_script_);
104 fetcher_ = NULL; 104 fetcher_ = NULL;
105 } 105 }
106 106
107 bool IsWaitingForFetcher() const { 107 bool IsWaitingForFetcher() const {
108 return state() == STATE_WAIT_URL; 108 return state() == STATE_WAIT_URL;
109 } 109 }
110 110
111 bool WasCancelled() const { 111 bool WasCancelled() const {
112 return state() == STATE_CANCEL; 112 return state() == STATE_CANCEL;
113 } 113 }
114 114
115 void FinishTest() { 115 void FinishTest() {
116 DCHECK(dhcp_query_); 116 DCHECK(dhcp_query_);
117 dhcp_query_->test_finished_event_.Signal(); 117 dhcp_query_->test_finished_event_.Signal();
118 } 118 }
119 119
120 int dhcp_delay_ms_; 120 base::TimeDelta dhcp_delay_;
121 int timeout_ms_; 121 base::TimeDelta timeout_;
122 std::string configured_url_; 122 std::string configured_url_;
123 int fetcher_delay_ms_; 123 int fetcher_delay_ms_;
124 int fetcher_result_; 124 int fetcher_result_;
125 std::string pac_script_; 125 std::string pac_script_;
126 MockProxyScriptFetcher* fetcher_; 126 MockProxyScriptFetcher* fetcher_;
127 base::OneShotTimer<MockDhcpProxyScriptAdapterFetcher> fetcher_timer_; 127 base::OneShotTimer<MockDhcpProxyScriptAdapterFetcher> fetcher_timer_;
128 scoped_refptr<DelayingDhcpQuery> dhcp_query_; 128 scoped_refptr<DelayingDhcpQuery> dhcp_query_;
129 }; 129 };
130 130
131 class FetcherClient { 131 class FetcherClient {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 } 176 }
177 177
178 TEST(DhcpProxyScriptAdapterFetcher, TimeoutDuringDhcp) { 178 TEST(DhcpProxyScriptAdapterFetcher, TimeoutDuringDhcp) {
179 // Does a Fetch() with a long enough delay on accessing DHCP that the 179 // Does a Fetch() with a long enough delay on accessing DHCP that the
180 // fetcher should time out. This is to test a case manual testing found, 180 // fetcher should time out. This is to test a case manual testing found,
181 // where under certain circumstances (e.g. adapter enabled for DHCP and 181 // where under certain circumstances (e.g. adapter enabled for DHCP and
182 // needs to retrieve its configuration from DHCP, but no DHCP server 182 // needs to retrieve its configuration from DHCP, but no DHCP server
183 // present on the network) accessing DHCP can take on the order of tens 183 // present on the network) accessing DHCP can take on the order of tens
184 // of seconds. 184 // of seconds.
185 FetcherClient client; 185 FetcherClient client;
186 client.fetcher_->dhcp_delay_ms_ = TestTimeouts::action_max_timeout_ms(); 186 client.fetcher_->dhcp_delay_ = TestTimeouts::action_max_timeout();
187 client.fetcher_->timeout_ms_ = 25; 187 client.fetcher_->timeout_ = base::TimeDelta::FromMilliseconds(25);
188 188
189 PerfTimer timer; 189 PerfTimer timer;
190 client.RunTest(); 190 client.RunTest();
191 // An error different from this would be received if the timeout didn't 191 // An error different from this would be received if the timeout didn't
192 // kick in. 192 // kick in.
193 client.WaitForResult(ERR_TIMED_OUT); 193 client.WaitForResult(ERR_TIMED_OUT);
194 194
195 ASSERT_TRUE(client.fetcher_->DidFinish()); 195 ASSERT_TRUE(client.fetcher_->DidFinish());
196 EXPECT_EQ(ERR_TIMED_OUT, client.fetcher_->GetResult()); 196 EXPECT_EQ(ERR_TIMED_OUT, client.fetcher_->GetResult());
197 EXPECT_EQ(string16(L""), client.fetcher_->GetPacScript()); 197 EXPECT_EQ(string16(L""), client.fetcher_->GetPacScript());
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 ASSERT_TRUE(client.fetcher_->DidFinish()); 288 ASSERT_TRUE(client.fetcher_->DidFinish());
289 EXPECT_EQ(OK, client.fetcher_->GetResult()); 289 EXPECT_EQ(OK, client.fetcher_->GetResult());
290 EXPECT_EQ(string16(L"-downloadable.pac-\n"), client.fetcher_->GetPacScript()); 290 EXPECT_EQ(string16(L"-downloadable.pac-\n"), client.fetcher_->GetPacScript());
291 EXPECT_EQ(configured_url, 291 EXPECT_EQ(configured_url,
292 client.fetcher_->GetPacURL()); 292 client.fetcher_->GetPacURL());
293 } 293 }
294 294
295 } // namespace 295 } // namespace
296 296
297 } // namespace net 297 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/dns_transaction_unittest.cc ('k') | net/proxy/dhcp_proxy_script_fetcher_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698