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

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

Issue 1439053002: Change ProxyResolver::GetProxyForURL() to take a scoped_ptr<Request>* rather than a RequestHandle* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ToT Created 4 years, 11 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/proxy_resolver_v8_tracing_wrapper.h" 5 #include "net/proxy/proxy_resolver_v8_tracing_wrapper.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 MockCachingHostResolver host_resolver; 660 MockCachingHostResolver host_resolver;
661 MockErrorObserver* error_observer = new MockErrorObserver; 661 MockErrorObserver* error_observer = new MockErrorObserver;
662 662
663 host_resolver.rules()->AddSimulatedFailure("*"); 663 host_resolver.rules()->AddSimulatedFailure("*");
664 664
665 scoped_ptr<ProxyResolver> resolver = CreateResolver( 665 scoped_ptr<ProxyResolver> resolver = CreateResolver(
666 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); 666 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js");
667 667
668 const size_t kNumRequests = 5; 668 const size_t kNumRequests = 5;
669 ProxyInfo proxy_info[kNumRequests]; 669 ProxyInfo proxy_info[kNumRequests];
670 ProxyResolver::RequestHandle request[kNumRequests]; 670 scoped_ptr<ProxyResolver::Request> request[kNumRequests];
671 671
672 for (size_t i = 0; i < kNumRequests; ++i) { 672 for (size_t i = 0; i < kNumRequests; ++i) {
673 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info[i], 673 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info[i],
674 base::Bind(&CrashCallback), &request[i], 674 base::Bind(&CrashCallback), &request[i],
675 BoundNetLog()); 675 BoundNetLog());
676 EXPECT_EQ(ERR_IO_PENDING, rv); 676 EXPECT_EQ(ERR_IO_PENDING, rv);
677 } 677 }
678 678
679 for (size_t i = 0; i < kNumRequests; ++i) { 679 for (size_t i = 0; i < kNumRequests; ++i) {
680 resolver->CancelRequest(request[i]); 680 request[i].reset();
681 } 681 }
682 } 682 }
683 683
684 // Note the execution order for this test can vary. Since multiple 684 // Note the execution order for this test can vary. Since multiple
685 // threads are involved, the cancellation may be received a different 685 // threads are involved, the cancellation may be received a different
686 // times. 686 // times.
687 TEST_F(ProxyResolverV8TracingWrapperTest, CancelSome) { 687 TEST_F(ProxyResolverV8TracingWrapperTest, CancelSome) {
688 MockCachingHostResolver host_resolver; 688 MockCachingHostResolver host_resolver;
689 MockErrorObserver* error_observer = new MockErrorObserver; 689 MockErrorObserver* error_observer = new MockErrorObserver;
690 690
691 host_resolver.rules()->AddSimulatedFailure("*"); 691 host_resolver.rules()->AddSimulatedFailure("*");
692 692
693 scoped_ptr<ProxyResolver> resolver = CreateResolver( 693 scoped_ptr<ProxyResolver> resolver = CreateResolver(
694 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); 694 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js");
695 695
696 ProxyInfo proxy_info1; 696 ProxyInfo proxy_info1;
697 ProxyInfo proxy_info2; 697 ProxyInfo proxy_info2;
698 ProxyResolver::RequestHandle request1; 698 scoped_ptr<ProxyResolver::Request> request1;
699 ProxyResolver::RequestHandle request2; 699 scoped_ptr<ProxyResolver::Request> request2;
700 TestCompletionCallback callback; 700 TestCompletionCallback callback;
701 701
702 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1, 702 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1,
703 base::Bind(&CrashCallback), &request1, 703 base::Bind(&CrashCallback), &request1,
704 BoundNetLog()); 704 BoundNetLog());
705 EXPECT_EQ(ERR_IO_PENDING, rv); 705 EXPECT_EQ(ERR_IO_PENDING, rv);
706 706
707 rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info2, 707 rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info2,
708 callback.callback(), &request2, BoundNetLog()); 708 callback.callback(), &request2, BoundNetLog());
709 EXPECT_EQ(ERR_IO_PENDING, rv); 709 EXPECT_EQ(ERR_IO_PENDING, rv);
710 710
711 resolver->CancelRequest(request1); 711 request1.reset();
712 712
713 EXPECT_EQ(OK, callback.WaitForResult()); 713 EXPECT_EQ(OK, callback.WaitForResult());
714 } 714 }
715 715
716 // Cancel a request after it has finished running on the worker thread, and has 716 // Cancel a request after it has finished running on the worker thread, and has
717 // posted a task the completion task back to origin thread. 717 // posted a task the completion task back to origin thread.
718 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhilePendingCompletionTask) { 718 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhilePendingCompletionTask) {
719 MockCachingHostResolver host_resolver; 719 MockCachingHostResolver host_resolver;
720 MockErrorObserver* error_observer = new MockErrorObserver; 720 MockErrorObserver* error_observer = new MockErrorObserver;
721 721
722 host_resolver.rules()->AddSimulatedFailure("*"); 722 host_resolver.rules()->AddSimulatedFailure("*");
723 723
724 scoped_ptr<ProxyResolver> resolver = CreateResolver( 724 scoped_ptr<ProxyResolver> resolver = CreateResolver(
725 nullptr, &host_resolver, make_scoped_ptr(error_observer), "error.js"); 725 nullptr, &host_resolver, make_scoped_ptr(error_observer), "error.js");
726 726
727 ProxyInfo proxy_info1; 727 ProxyInfo proxy_info1;
728 ProxyInfo proxy_info2; 728 ProxyInfo proxy_info2;
729 ProxyResolver::RequestHandle request1; 729 scoped_ptr<ProxyResolver::Request> request1;
730 ProxyResolver::RequestHandle request2; 730 scoped_ptr<ProxyResolver::Request> request2;
731 TestCompletionCallback callback; 731 TestCompletionCallback callback;
732 732
733 int rv = resolver->GetProxyForURL(GURL("http://throw-an-error/"), 733 int rv = resolver->GetProxyForURL(GURL("http://throw-an-error/"),
734 &proxy_info1, base::Bind(&CrashCallback), 734 &proxy_info1, base::Bind(&CrashCallback),
735 &request1, BoundNetLog()); 735 &request1, BoundNetLog());
736 EXPECT_EQ(ERR_IO_PENDING, rv); 736 EXPECT_EQ(ERR_IO_PENDING, rv);
737 737
738 // Wait until the first request has finished running on the worker thread. 738 // Wait until the first request has finished running on the worker thread.
739 // Cancel the first request, while it has a pending completion task on 739 // Cancel the first request, while it has a pending completion task on
740 // the origin thread. 740 // the origin thread. Reset deletes Request object which cancels the request.
741 error_observer->RunOnError(base::Bind(&ProxyResolver::CancelRequest, 741 error_observer->RunOnError(
742 base::Unretained(resolver.get()), 742 base::Bind(&scoped_ptr<ProxyResolver::Request>::reset,
743 request1)); 743 base::Unretained(&request1), nullptr));
744 744
745 // Start another request, to make sure it is able to complete. 745 // Start another request, to make sure it is able to complete.
746 rv = resolver->GetProxyForURL(GURL("http://i-have-no-idea-what-im-doing/"), 746 rv = resolver->GetProxyForURL(GURL("http://i-have-no-idea-what-im-doing/"),
747 &proxy_info2, callback.callback(), &request2, 747 &proxy_info2, callback.callback(), &request2,
748 BoundNetLog()); 748 BoundNetLog());
749 EXPECT_EQ(ERR_IO_PENDING, rv); 749 EXPECT_EQ(ERR_IO_PENDING, rv);
750 750
751 EXPECT_EQ(OK, callback.WaitForResult()); 751 EXPECT_EQ(OK, callback.WaitForResult());
752 752
753 EXPECT_EQ("i-approve-this-message:42", proxy_info2.proxy_server().ToURI()); 753 EXPECT_EQ("i-approve-this-message:42", proxy_info2.proxy_server().ToURI());
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
821 TEST_F(ProxyResolverV8TracingWrapperTest, 821 TEST_F(ProxyResolverV8TracingWrapperTest,
822 CancelWhileOutstandingNonBlockingDns) { 822 CancelWhileOutstandingNonBlockingDns) {
823 BlockableHostResolver host_resolver; 823 BlockableHostResolver host_resolver;
824 MockErrorObserver* error_observer = new MockErrorObserver; 824 MockErrorObserver* error_observer = new MockErrorObserver;
825 825
826 scoped_ptr<ProxyResolver> resolver = CreateResolver( 826 scoped_ptr<ProxyResolver> resolver = CreateResolver(
827 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); 827 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js");
828 828
829 ProxyInfo proxy_info1; 829 ProxyInfo proxy_info1;
830 ProxyInfo proxy_info2; 830 ProxyInfo proxy_info2;
831 ProxyResolver::RequestHandle request1; 831 scoped_ptr<ProxyResolver::Request> request1;
832 ProxyResolver::RequestHandle request2; 832 scoped_ptr<ProxyResolver::Request> request2;
833 833
834 int rv = resolver->GetProxyForURL(GURL("http://foo/req1"), &proxy_info1, 834 int rv = resolver->GetProxyForURL(GURL("http://foo/req1"), &proxy_info1,
835 base::Bind(&CrashCallback), &request1, 835 base::Bind(&CrashCallback), &request1,
836 BoundNetLog()); 836 BoundNetLog());
837 837
838 EXPECT_EQ(ERR_IO_PENDING, rv); 838 EXPECT_EQ(ERR_IO_PENDING, rv);
839 839
840 host_resolver.WaitUntilRequestIsReceived(); 840 host_resolver.WaitUntilRequestIsReceived();
841 841
842 rv = resolver->GetProxyForURL(GURL("http://foo/req2"), &proxy_info2, 842 rv = resolver->GetProxyForURL(GURL("http://foo/req2"), &proxy_info2,
843 base::Bind(&CrashCallback), &request2, 843 base::Bind(&CrashCallback), &request2,
844 BoundNetLog()); 844 BoundNetLog());
845 845
846 EXPECT_EQ(ERR_IO_PENDING, rv); 846 EXPECT_EQ(ERR_IO_PENDING, rv);
847 847
848 host_resolver.WaitUntilRequestIsReceived(); 848 host_resolver.WaitUntilRequestIsReceived();
849 849
850 resolver->CancelRequest(request1); 850 request1.reset();
851 resolver->CancelRequest(request2); 851 request2.reset();
852 852
853 EXPECT_EQ(2, host_resolver.num_cancelled_requests()); 853 EXPECT_EQ(2, host_resolver.num_cancelled_requests());
854 854
855 // After leaving this scope, the ProxyResolver is destroyed. 855 // After leaving this scope, the ProxyResolver is destroyed.
856 // This should not cause any problems, as the outstanding work 856 // This should not cause any problems, as the outstanding work
857 // should have been cancelled. 857 // should have been cancelled.
858 } 858 }
859 859
860 void CancelRequestAndPause(ProxyResolver* resolver, 860 void CancelRequestAndPause(scoped_ptr<ProxyResolver::Request>* request) {
861 ProxyResolver::RequestHandle request) { 861 request->reset();
862 resolver->CancelRequest(request);
863 862
864 // Sleep for a little bit. This makes it more likely for the worker 863 // Sleep for a little bit. This makes it more likely for the worker
865 // thread to have returned from its call, and serves as a regression 864 // thread to have returned from its call, and serves as a regression
866 // test for http://crbug.com/173373. 865 // test for http://crbug.com/173373.
867 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); 866 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30));
868 } 867 }
869 868
870 // In non-blocking mode, the worker thread actually does block for 869 // In non-blocking mode, the worker thread actually does block for
871 // a short time to see if the result is in the DNS cache. Test 870 // a short time to see if the result is in the DNS cache. Test
872 // cancellation while the worker thread is waiting on this event. 871 // cancellation while the worker thread is waiting on this event.
873 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhileBlockedInNonBlockingDns) { 872 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhileBlockedInNonBlockingDns) {
874 BlockableHostResolver host_resolver; 873 BlockableHostResolver host_resolver;
875 MockErrorObserver* error_observer = new MockErrorObserver; 874 MockErrorObserver* error_observer = new MockErrorObserver;
876 875
877 scoped_ptr<ProxyResolver> resolver = CreateResolver( 876 scoped_ptr<ProxyResolver> resolver = CreateResolver(
878 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); 877 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js");
879 878
880 ProxyInfo proxy_info; 879 ProxyInfo proxy_info;
881 ProxyResolver::RequestHandle request; 880 scoped_ptr<ProxyResolver::Request> request;
882 881
883 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, 882 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info,
884 base::Bind(&CrashCallback), &request, 883 base::Bind(&CrashCallback), &request,
885 BoundNetLog()); 884 BoundNetLog());
886 885
887 EXPECT_EQ(ERR_IO_PENDING, rv); 886 EXPECT_EQ(ERR_IO_PENDING, rv);
888 887
889 host_resolver.SetAction( 888 host_resolver.SetAction(base::Bind(CancelRequestAndPause, &request));
890 base::Bind(CancelRequestAndPause, resolver.get(), request));
891 889
892 host_resolver.WaitUntilRequestIsReceived(); 890 host_resolver.WaitUntilRequestIsReceived();
893 } 891 }
894 892
895 // Cancel the request while there is a pending DNS request, however before 893 // Cancel the request while there is a pending DNS request, however before
896 // the request is sent to the host resolver. 894 // the request is sent to the host resolver.
897 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhileBlockedInNonBlockingDns2) { 895 TEST_F(ProxyResolverV8TracingWrapperTest, CancelWhileBlockedInNonBlockingDns2) {
898 MockCachingHostResolver host_resolver; 896 MockCachingHostResolver host_resolver;
899 MockErrorObserver* error_observer = new MockErrorObserver; 897 MockErrorObserver* error_observer = new MockErrorObserver;
900 898
901 scoped_ptr<ProxyResolver> resolver = CreateResolver( 899 scoped_ptr<ProxyResolver> resolver = CreateResolver(
902 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); 900 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js");
903 901
904 ProxyInfo proxy_info; 902 ProxyInfo proxy_info;
905 ProxyResolver::RequestHandle request; 903 scoped_ptr<ProxyResolver::Request> request;
906 904
907 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, 905 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info,
908 base::Bind(&CrashCallback), &request, 906 base::Bind(&CrashCallback), &request,
909 BoundNetLog()); 907 BoundNetLog());
910 908
911 EXPECT_EQ(ERR_IO_PENDING, rv); 909 EXPECT_EQ(ERR_IO_PENDING, rv);
912 910
913 // Wait a bit, so the DNS task has hopefully been posted. The test will 911 // Wait a bit, so the DNS task has hopefully been posted. The test will
914 // work whatever the delay is here, but it is most useful if the delay 912 // work whatever the delay is here, but it is most useful if the delay
915 // is large enough to allow a task to be posted back. 913 // is large enough to allow a task to be posted back.
916 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); 914 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
917 resolver->CancelRequest(request); 915 request.reset();
918 916
919 EXPECT_EQ(0u, host_resolver.num_resolve()); 917 EXPECT_EQ(0u, host_resolver.num_resolve());
920 } 918 }
921 919
922 TEST_F(ProxyResolverV8TracingWrapperTest, 920 TEST_F(ProxyResolverV8TracingWrapperTest,
923 CancelCreateResolverWhileOutstandingBlockingDns) { 921 CancelCreateResolverWhileOutstandingBlockingDns) {
924 BlockableHostResolver host_resolver; 922 BlockableHostResolver host_resolver;
925 MockErrorObserver* error_observer = new MockErrorObserver; 923 MockErrorObserver* error_observer = new MockErrorObserver;
926 924
927 ProxyResolverFactoryV8TracingWrapper factory( 925 ProxyResolverFactoryV8TracingWrapper factory(
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
1125 proxy_uri.substr(0, proxy_uri.find(':') + 1)); 1123 proxy_uri.substr(0, proxy_uri.find(':') + 1));
1126 } else { 1124 } else {
1127 NOTREACHED(); 1125 NOTREACHED();
1128 } 1126 }
1129 } 1127 }
1130 } 1128 }
1131 1129
1132 } // namespace 1130 } // namespace
1133 1131
1134 } // namespace net 1132 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698