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

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

Issue 2845643003: Allow ProxyService to share URLRequestContext with everything else. (Closed)
Patch Set: Fix fetcher shutdown with no active request, add test, add comment Created 3 years, 7 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/proxy/proxy_script_decider.cc ('k') | net/proxy/proxy_script_fetcher.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 <vector> 5 #include <vector>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 const Rules::Rule& rule = rules_->GetRuleByUrl(url); 121 const Rules::Rule& rule = rules_->GetRuleByUrl(url);
122 int rv = rule.fetch_error; 122 int rv = rule.fetch_error;
123 EXPECT_NE(ERR_UNEXPECTED, rv); 123 EXPECT_NE(ERR_UNEXPECTED, rv);
124 if (rv == OK) 124 if (rv == OK)
125 *text = rule.text(); 125 *text = rule.text();
126 return rv; 126 return rv;
127 } 127 }
128 128
129 void Cancel() override {} 129 void Cancel() override {}
130 130
131 void OnShutdown() override { request_context_ = nullptr; }
132
131 URLRequestContext* GetRequestContext() const override { 133 URLRequestContext* GetRequestContext() const override {
132 return request_context_; 134 return request_context_;
133 } 135 }
134 136
135 private: 137 private:
136 const Rules* rules_; 138 const Rules* rules_;
137 URLRequestContext* request_context_; 139 URLRequestContext* request_context_;
138 }; 140 };
139 141
140 // A mock retriever, returns asynchronously when CompleteRequests() is called. 142 // A mock retriever, returns asynchronously when CompleteRequests() is called.
141 class MockDhcpProxyScriptFetcher : public DhcpProxyScriptFetcher { 143 class MockDhcpProxyScriptFetcher : public DhcpProxyScriptFetcher {
142 public: 144 public:
143 MockDhcpProxyScriptFetcher(); 145 MockDhcpProxyScriptFetcher();
144 ~MockDhcpProxyScriptFetcher() override; 146 ~MockDhcpProxyScriptFetcher() override;
145 147
146 int Fetch(base::string16* utf16_text, 148 int Fetch(base::string16* utf16_text,
147 const CompletionCallback& callback) override; 149 const CompletionCallback& callback) override;
148 void Cancel() override; 150 void Cancel() override;
151 void OnShutdown() override;
149 const GURL& GetPacURL() const override; 152 const GURL& GetPacURL() const override;
150 153
151 virtual void SetPacURL(const GURL& url); 154 virtual void SetPacURL(const GURL& url);
152 155
153 virtual void CompleteRequests(int result, const base::string16& script); 156 virtual void CompleteRequests(int result, const base::string16& script);
154 157
155 private: 158 private:
156 CompletionCallback callback_; 159 CompletionCallback callback_;
157 base::string16* utf16_text_; 160 base::string16* utf16_text_;
158 GURL gurl_; 161 GURL gurl_;
159 DISALLOW_COPY_AND_ASSIGN(MockDhcpProxyScriptFetcher); 162 DISALLOW_COPY_AND_ASSIGN(MockDhcpProxyScriptFetcher);
160 }; 163 };
161 164
162 MockDhcpProxyScriptFetcher::MockDhcpProxyScriptFetcher() { } 165 MockDhcpProxyScriptFetcher::MockDhcpProxyScriptFetcher() { }
163 166
164 MockDhcpProxyScriptFetcher::~MockDhcpProxyScriptFetcher() { } 167 MockDhcpProxyScriptFetcher::~MockDhcpProxyScriptFetcher() { }
165 168
166 int MockDhcpProxyScriptFetcher::Fetch(base::string16* utf16_text, 169 int MockDhcpProxyScriptFetcher::Fetch(base::string16* utf16_text,
167 const CompletionCallback& callback) { 170 const CompletionCallback& callback) {
168 utf16_text_ = utf16_text; 171 utf16_text_ = utf16_text;
169 callback_ = callback; 172 callback_ = callback;
170 return ERR_IO_PENDING; 173 return ERR_IO_PENDING;
171 } 174 }
172 175
173 void MockDhcpProxyScriptFetcher::Cancel() { } 176 void MockDhcpProxyScriptFetcher::Cancel() { }
174 177
178 void MockDhcpProxyScriptFetcher::OnShutdown() {}
179
175 const GURL& MockDhcpProxyScriptFetcher::GetPacURL() const { 180 const GURL& MockDhcpProxyScriptFetcher::GetPacURL() const {
176 return gurl_; 181 return gurl_;
177 } 182 }
178 183
179 void MockDhcpProxyScriptFetcher::SetPacURL(const GURL& url) { 184 void MockDhcpProxyScriptFetcher::SetPacURL(const GURL& url) {
180 gurl_ = url; 185 gurl_ = url;
181 } 186 }
182 187
183 void MockDhcpProxyScriptFetcher::CompleteRequests( 188 void MockDhcpProxyScriptFetcher::CompleteRequests(
184 int result, const base::string16& script) { 189 int result, const base::string16& script) {
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 config_.set_auto_detect(true); 331 config_.set_auto_detect(true);
327 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher_, NULL)); 332 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher_, NULL));
328 } 333 }
329 334
330 int StartDecider() { 335 int StartDecider() {
331 return decider_->Start(config_, base::TimeDelta(), true, 336 return decider_->Start(config_, base::TimeDelta(), true,
332 callback_.callback()); 337 callback_.callback());
333 } 338 }
334 339
335 protected: 340 protected:
336 std::unique_ptr<ProxyScriptDecider> decider_;
337 MockHostResolver resolver_; 341 MockHostResolver resolver_;
338 Rules rules_; 342 Rules rules_;
339 Rules::Rule rule_; 343 Rules::Rule rule_;
340 TestCompletionCallback callback_; 344 TestCompletionCallback callback_;
341 RuleBasedProxyScriptFetcher fetcher_; 345 RuleBasedProxyScriptFetcher fetcher_;
342 ProxyConfig config_; 346 ProxyConfig config_;
343 DoNothingDhcpProxyScriptFetcher dhcp_fetcher_; 347 DoNothingDhcpProxyScriptFetcher dhcp_fetcher_;
348 std::unique_ptr<ProxyScriptDecider> decider_;
344 349
345 private: 350 private:
346 URLRequestContext request_context_; 351 URLRequestContext request_context_;
347 }; 352 };
348 353
349 // Fails if a synchronous DNS lookup success for wpad causes QuickCheck to fail. 354 // Fails if a synchronous DNS lookup success for wpad causes QuickCheck to fail.
350 TEST_F(ProxyScriptDeciderQuickCheckTest, SyncSuccess) { 355 TEST_F(ProxyScriptDeciderQuickCheckTest, SyncSuccess) {
351 resolver_.set_synchronous_mode(true); 356 resolver_.set_synchronous_mode(true);
352 resolver_.rules()->AddRule("wpad", "1.2.3.4"); 357 resolver_.rules()->AddRule("wpad", "1.2.3.4");
353 358
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 config_.set_pac_url(GURL(kCustomUrl)); 436 config_.set_pac_url(GURL(kCustomUrl));
432 Rules::Rule rule = rules_.AddSuccessRule(kCustomUrl); 437 Rules::Rule rule = rules_.AddSuccessRule(kCustomUrl);
433 resolver_.rules()->AddSimulatedFailure("wpad"); 438 resolver_.rules()->AddSimulatedFailure("wpad");
434 resolver_.rules()->AddRule("custom", "1.2.3.4"); 439 resolver_.rules()->AddRule("custom", "1.2.3.4");
435 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING)); 440 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING));
436 callback_.WaitForResult(); 441 callback_.WaitForResult();
437 EXPECT_TRUE(decider_->effective_config().has_pac_url()); 442 EXPECT_TRUE(decider_->effective_config().has_pac_url());
438 EXPECT_EQ(rule.url, decider_->effective_config().pac_url()); 443 EXPECT_EQ(rule.url, decider_->effective_config().pac_url());
439 } 444 }
440 445
446 TEST_F(ProxyScriptDeciderQuickCheckTest, ShutdownDuringResolve) {
447 resolver_.set_ondemand_mode(true);
448
449 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING));
450 EXPECT_TRUE(resolver_.has_pending_requests());
451
452 decider_->OnShutdown();
453 EXPECT_FALSE(resolver_.has_pending_requests());
454 EXPECT_EQ(ERR_CONTEXT_SHUT_DOWN, callback_.WaitForResult());
455 }
456
441 // Regression test for http://crbug.com/409698. 457 // Regression test for http://crbug.com/409698.
442 // This test lets the state machine get into state QUICK_CHECK_COMPLETE, then 458 // This test lets the state machine get into state QUICK_CHECK_COMPLETE, then
443 // destroys the decider, causing a cancel. 459 // destroys the decider, causing a cancel.
444 TEST_F(ProxyScriptDeciderQuickCheckTest, CancelPartway) { 460 TEST_F(ProxyScriptDeciderQuickCheckTest, CancelPartway) {
445 resolver_.set_synchronous_mode(false); 461 resolver_.set_synchronous_mode(false);
446 resolver_.set_ondemand_mode(true); 462 resolver_.set_ondemand_mode(true);
447 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING)); 463 EXPECT_THAT(StartDecider(), IsError(ERR_IO_PENDING));
448 decider_.reset(NULL); 464 decider_.reset(NULL);
449 } 465 }
450 466
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 } 679 }
664 680
665 int Fetch(base::string16* utf16_text, 681 int Fetch(base::string16* utf16_text,
666 const CompletionCallback& callback) override { 682 const CompletionCallback& callback) override {
667 *utf16_text = expected_text_; 683 *utf16_text = expected_text_;
668 return OK; 684 return OK;
669 } 685 }
670 686
671 void Cancel() override {} 687 void Cancel() override {}
672 688
689 void OnShutdown() override {}
690
673 const GURL& GetPacURL() const override { return gurl_; } 691 const GURL& GetPacURL() const override { return gurl_; }
674 692
675 const base::string16& expected_text() const { 693 const base::string16& expected_text() const {
676 return expected_text_; 694 return expected_text_;
677 } 695 }
678 696
679 private: 697 private:
680 GURL gurl_; 698 GURL gurl_;
681 base::string16 expected_text_; 699 base::string16 expected_text_;
682 700
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 const CompletionCallback& callback) override { 763 const CompletionCallback& callback) override {
746 callback_ = callback; 764 callback_ = callback;
747 base::ThreadTaskRunnerHandle::Get()->PostTask( 765 base::ThreadTaskRunnerHandle::Get()->PostTask(
748 FROM_HERE, 766 FROM_HERE,
749 base::Bind(&AsyncFailDhcpFetcher::CallbackWithFailure, AsWeakPtr())); 767 base::Bind(&AsyncFailDhcpFetcher::CallbackWithFailure, AsWeakPtr()));
750 return ERR_IO_PENDING; 768 return ERR_IO_PENDING;
751 } 769 }
752 770
753 void Cancel() override { callback_.Reset(); } 771 void Cancel() override { callback_.Reset(); }
754 772
773 void OnShutdown() override {}
774
755 const GURL& GetPacURL() const override { return dummy_gurl_; } 775 const GURL& GetPacURL() const override { return dummy_gurl_; }
756 776
757 void CallbackWithFailure() { 777 void CallbackWithFailure() {
758 if (!callback_.is_null()) 778 if (!callback_.is_null())
759 callback_.Run(ERR_PAC_NOT_IN_DHCP); 779 callback_.Run(ERR_PAC_NOT_IN_DHCP);
760 } 780 }
761 781
762 private: 782 private:
763 GURL dummy_gurl_; 783 GURL dummy_gurl_;
764 CompletionCallback callback_; 784 CompletionCallback callback_;
(...skipping 24 matching lines...) Expand all
789 809
790 // Run the message loop to let the DHCP fetch complete and post the results 810 // Run the message loop to let the DHCP fetch complete and post the results
791 // back. Before the fix linked to above, this would try to invoke on 811 // back. Before the fix linked to above, this would try to invoke on
792 // the callback object provided by ProxyScriptDecider after it was 812 // the callback object provided by ProxyScriptDecider after it was
793 // no longer valid. 813 // no longer valid.
794 base::RunLoop().RunUntilIdle(); 814 base::RunLoop().RunUntilIdle();
795 } 815 }
796 816
797 } // namespace 817 } // namespace
798 } // namespace net 818 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/proxy_script_decider.cc ('k') | net/proxy/proxy_script_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698