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 "net/dns/host_resolver_impl.h" | 5 #include "net/dns/host_resolver_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 } | 263 } |
264 | 264 |
265 bool HasOneAddress(const std::string& address, uint16 port) const { | 265 bool HasOneAddress(const std::string& address, uint16 port) const { |
266 return HasAddress(address, port) && (NumberOfAddresses() == 1u); | 266 return HasAddress(address, port) && (NumberOfAddresses() == 1u); |
267 } | 267 } |
268 | 268 |
269 // Returns ERR_UNEXPECTED if timed out. | 269 // Returns ERR_UNEXPECTED if timed out. |
270 int WaitForResult() { | 270 int WaitForResult() { |
271 if (completed()) | 271 if (completed()) |
272 return result_; | 272 return result_; |
273 base::CancelableClosure closure(base::MessageLoop::QuitClosure()); | 273 base::CancelableClosure closure(base::MessageLoop::QuitWhenIdleClosure()); |
274 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 274 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
275 FROM_HERE, closure.callback(), TestTimeouts::action_max_timeout()); | 275 FROM_HERE, closure.callback(), TestTimeouts::action_max_timeout()); |
276 quit_on_complete_ = true; | 276 quit_on_complete_ = true; |
277 base::MessageLoop::current()->Run(); | 277 base::MessageLoop::current()->Run(); |
278 bool did_quit = !quit_on_complete_; | 278 bool did_quit = !quit_on_complete_; |
279 quit_on_complete_ = false; | 279 quit_on_complete_ = false; |
280 closure.Cancel(); | 280 closure.Cancel(); |
281 if (did_quit) | 281 if (did_quit) |
282 return result_; | 282 return result_; |
283 else | 283 else |
284 return ERR_UNEXPECTED; | 284 return ERR_UNEXPECTED; |
285 } | 285 } |
286 | 286 |
287 private: | 287 private: |
288 void OnComplete(int rv) { | 288 void OnComplete(int rv) { |
289 EXPECT_TRUE(pending()); | 289 EXPECT_TRUE(pending()); |
290 EXPECT_EQ(ERR_IO_PENDING, result_); | 290 EXPECT_EQ(ERR_IO_PENDING, result_); |
291 EXPECT_NE(ERR_IO_PENDING, rv); | 291 EXPECT_NE(ERR_IO_PENDING, rv); |
292 result_ = rv; | 292 result_ = rv; |
293 handle_ = NULL; | 293 handle_ = NULL; |
294 if (!list_.empty()) { | 294 if (!list_.empty()) { |
295 EXPECT_EQ(OK, result_); | 295 EXPECT_EQ(OK, result_); |
296 EXPECT_EQ(info_.port(), list_.front().port()); | 296 EXPECT_EQ(info_.port(), list_.front().port()); |
297 } | 297 } |
298 if (handler_) | 298 if (handler_) |
299 handler_->Handle(this); | 299 handler_->Handle(this); |
300 if (quit_on_complete_) { | 300 if (quit_on_complete_) { |
301 base::MessageLoop::current()->Quit(); | 301 base::MessageLoop::current()->QuitWhenIdle(); |
302 quit_on_complete_ = false; | 302 quit_on_complete_ = false; |
303 } | 303 } |
304 } | 304 } |
305 | 305 |
306 HostResolver::RequestInfo info_; | 306 HostResolver::RequestInfo info_; |
307 RequestPriority priority_; | 307 RequestPriority priority_; |
308 size_t index_; | 308 size_t index_; |
309 HostResolver* resolver_; | 309 HostResolver* resolver_; |
310 Handler* handler_; | 310 Handler* handler_; |
311 bool quit_on_complete_; | 311 bool quit_on_complete_; |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
840 struct MyHandler : public Handler { | 840 struct MyHandler : public Handler { |
841 void Handle(Request* req) override { | 841 void Handle(Request* req) override { |
842 EXPECT_EQ("a", req->info().hostname()); | 842 EXPECT_EQ("a", req->info().hostname()); |
843 EXPECT_EQ(80, req->info().port()); | 843 EXPECT_EQ(80, req->info().port()); |
844 | 844 |
845 DeleteResolver(); | 845 DeleteResolver(); |
846 | 846 |
847 // Quit after returning from OnCompleted (to give it a chance at | 847 // Quit after returning from OnCompleted (to give it a chance at |
848 // incorrectly running the cancelled tasks). | 848 // incorrectly running the cancelled tasks). |
849 base::ThreadTaskRunnerHandle::Get()->PostTask( | 849 base::ThreadTaskRunnerHandle::Get()->PostTask( |
850 FROM_HERE, base::MessageLoop::QuitClosure()); | 850 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
851 } | 851 } |
852 }; | 852 }; |
853 set_handler(new MyHandler()); | 853 set_handler(new MyHandler()); |
854 | 854 |
855 for (size_t i = 0; i < 4; ++i) { | 855 for (size_t i = 0; i < 4; ++i) { |
856 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i; | 856 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i; |
857 } | 857 } |
858 | 858 |
859 proc_->SignalMultiple(1u); // One for "a". | 859 proc_->SignalMultiple(1u); // One for "a". |
860 | 860 |
861 // |MyHandler| will send quit message once all the requests have finished. | 861 // |MyHandler| will send quit message once all the requests have finished. |
862 base::MessageLoop::current()->Run(); | 862 base::MessageLoop::current()->Run(); |
863 } | 863 } |
864 | 864 |
865 TEST_F(HostResolverImplTest, DeleteWithinAbortedCallback) { | 865 TEST_F(HostResolverImplTest, DeleteWithinAbortedCallback) { |
866 struct MyHandler : public Handler { | 866 struct MyHandler : public Handler { |
867 void Handle(Request* req) override { | 867 void Handle(Request* req) override { |
868 EXPECT_EQ("a", req->info().hostname()); | 868 EXPECT_EQ("a", req->info().hostname()); |
869 EXPECT_EQ(80, req->info().port()); | 869 EXPECT_EQ(80, req->info().port()); |
870 | 870 |
871 DeleteResolver(); | 871 DeleteResolver(); |
872 | 872 |
873 // Quit after returning from OnCompleted (to give it a chance at | 873 // Quit after returning from OnCompleted (to give it a chance at |
874 // incorrectly running the cancelled tasks). | 874 // incorrectly running the cancelled tasks). |
875 base::ThreadTaskRunnerHandle::Get()->PostTask( | 875 base::ThreadTaskRunnerHandle::Get()->PostTask( |
876 FROM_HERE, base::MessageLoop::QuitClosure()); | 876 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
877 } | 877 } |
878 }; | 878 }; |
879 set_handler(new MyHandler()); | 879 set_handler(new MyHandler()); |
880 | 880 |
881 // This test assumes that the Jobs will be Aborted in order ["a", "b"] | 881 // This test assumes that the Jobs will be Aborted in order ["a", "b"] |
882 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve()); | 882 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve()); |
883 // HostResolverImpl will be deleted before later Requests can complete. | 883 // HostResolverImpl will be deleted before later Requests can complete. |
884 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 81)->Resolve()); | 884 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 81)->Resolve()); |
885 // Job for 'b' will be aborted before it can complete. | 885 // Job for 'b' will be aborted before it can complete. |
886 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 82)->Resolve()); | 886 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 82)->Resolve()); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
944 EXPECT_EQ(OK, CreateRequest(hostname, 75)->ResolveFromCache()); | 944 EXPECT_EQ(OK, CreateRequest(hostname, 75)->ResolveFromCache()); |
945 | 945 |
946 // Ok good. Now make sure that if we ask to bypass the cache, it can no | 946 // Ok good. Now make sure that if we ask to bypass the cache, it can no |
947 // longer service the request synchronously. | 947 // longer service the request synchronously. |
948 HostResolver::RequestInfo info(HostPortPair(hostname, 71)); | 948 HostResolver::RequestInfo info(HostPortPair(hostname, 71)); |
949 info.set_allow_cached_response(false); | 949 info.set_allow_cached_response(false); |
950 EXPECT_EQ(ERR_IO_PENDING, | 950 EXPECT_EQ(ERR_IO_PENDING, |
951 CreateRequest(info, DEFAULT_PRIORITY)->Resolve()); | 951 CreateRequest(info, DEFAULT_PRIORITY)->Resolve()); |
952 } else if (71 == req->info().port()) { | 952 } else if (71 == req->info().port()) { |
953 // Test is done. | 953 // Test is done. |
954 base::MessageLoop::current()->Quit(); | 954 base::MessageLoop::current()->QuitWhenIdle(); |
955 } else { | 955 } else { |
956 FAIL() << "Unexpected request"; | 956 FAIL() << "Unexpected request"; |
957 } | 957 } |
958 } | 958 } |
959 }; | 959 }; |
960 set_handler(new MyHandler()); | 960 set_handler(new MyHandler()); |
961 | 961 |
962 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve()); | 962 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve()); |
963 proc_->SignalMultiple(3u); // Only need two, but be generous. | 963 proc_->SignalMultiple(3u); // Only need two, but be generous. |
964 | 964 |
(...skipping 1204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2169 | 2169 |
2170 EXPECT_EQ(OK, requests_[0]->WaitForResult()); | 2170 EXPECT_EQ(OK, requests_[0]->WaitForResult()); |
2171 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80)); | 2171 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80)); |
2172 EXPECT_EQ(OK, requests_[1]->WaitForResult()); | 2172 EXPECT_EQ(OK, requests_[1]->WaitForResult()); |
2173 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80)); | 2173 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80)); |
2174 EXPECT_EQ(OK, requests_[2]->WaitForResult()); | 2174 EXPECT_EQ(OK, requests_[2]->WaitForResult()); |
2175 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80)); | 2175 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80)); |
2176 } | 2176 } |
2177 | 2177 |
2178 } // namespace net | 2178 } // namespace net |
OLD | NEW |