OLD | NEW |
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.h" | 5 #include "net/proxy/proxy_resolver_v8_tracing.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/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 MockCachingHostResolver host_resolver; | 579 MockCachingHostResolver host_resolver; |
580 MockBindings mock_bindings(&host_resolver); | 580 MockBindings mock_bindings(&host_resolver); |
581 | 581 |
582 host_resolver.rules()->AddSimulatedFailure("*"); | 582 host_resolver.rules()->AddSimulatedFailure("*"); |
583 | 583 |
584 scoped_ptr<ProxyResolverV8Tracing> resolver = | 584 scoped_ptr<ProxyResolverV8Tracing> resolver = |
585 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); | 585 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
586 | 586 |
587 const size_t kNumRequests = 5; | 587 const size_t kNumRequests = 5; |
588 ProxyInfo proxy_info[kNumRequests]; | 588 ProxyInfo proxy_info[kNumRequests]; |
589 ProxyResolver::RequestHandle request[kNumRequests]; | 589 scoped_ptr<ProxyResolver::Request> request[kNumRequests]; |
590 | 590 |
591 for (size_t i = 0; i < kNumRequests; ++i) { | 591 for (size_t i = 0; i < kNumRequests; ++i) { |
592 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info[i], | 592 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info[i], |
593 base::Bind(&CrashCallback), &request[i], | 593 base::Bind(&CrashCallback), &request[i], |
594 mock_bindings.CreateBindings()); | 594 mock_bindings.CreateBindings()); |
595 } | 595 } |
596 | 596 |
597 for (size_t i = 0; i < kNumRequests; ++i) { | 597 for (size_t i = 0; i < kNumRequests; ++i) { |
598 resolver->CancelRequest(request[i]); | 598 request[i].reset(); |
599 } | 599 } |
600 } | 600 } |
601 | 601 |
602 // Note the execution order for this test can vary. Since multiple | 602 // Note the execution order for this test can vary. Since multiple |
603 // threads are involved, the cancellation may be received a different | 603 // threads are involved, the cancellation may be received a different |
604 // times. | 604 // times. |
605 TEST_F(ProxyResolverV8TracingTest, CancelSome) { | 605 TEST_F(ProxyResolverV8TracingTest, CancelSome) { |
606 MockCachingHostResolver host_resolver; | 606 MockCachingHostResolver host_resolver; |
607 MockBindings mock_bindings(&host_resolver); | 607 MockBindings mock_bindings(&host_resolver); |
608 | 608 |
609 host_resolver.rules()->AddSimulatedFailure("*"); | 609 host_resolver.rules()->AddSimulatedFailure("*"); |
610 | 610 |
611 scoped_ptr<ProxyResolverV8Tracing> resolver = | 611 scoped_ptr<ProxyResolverV8Tracing> resolver = |
612 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); | 612 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
613 | 613 |
614 ProxyInfo proxy_info1; | 614 ProxyInfo proxy_info1; |
615 ProxyInfo proxy_info2; | 615 ProxyInfo proxy_info2; |
616 ProxyResolver::RequestHandle request1; | 616 scoped_ptr<ProxyResolver::Request> request1; |
617 ProxyResolver::RequestHandle request2; | 617 scoped_ptr<ProxyResolver::Request> request2; |
618 TestCompletionCallback callback; | 618 TestCompletionCallback callback; |
619 | 619 |
620 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1, | 620 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1, |
621 base::Bind(&CrashCallback), &request1, | 621 base::Bind(&CrashCallback), &request1, |
622 mock_bindings.CreateBindings()); | 622 mock_bindings.CreateBindings()); |
623 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info2, | 623 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info2, |
624 callback.callback(), &request2, | 624 callback.callback(), &request2, |
625 mock_bindings.CreateBindings()); | 625 mock_bindings.CreateBindings()); |
626 | 626 |
627 resolver->CancelRequest(request1); | 627 request1.reset(); |
628 | 628 |
629 EXPECT_EQ(OK, callback.WaitForResult()); | 629 EXPECT_EQ(OK, callback.WaitForResult()); |
630 } | 630 } |
631 | 631 |
632 // Cancel a request after it has finished running on the worker thread, and has | 632 // Cancel a request after it has finished running on the worker thread, and has |
633 // posted a task the completion task back to origin thread. | 633 // posted a task the completion task back to origin thread. |
634 TEST_F(ProxyResolverV8TracingTest, CancelWhilePendingCompletionTask) { | 634 TEST_F(ProxyResolverV8TracingTest, CancelWhilePendingCompletionTask) { |
635 MockCachingHostResolver host_resolver; | 635 MockCachingHostResolver host_resolver; |
636 MockBindings mock_bindings(&host_resolver); | 636 MockBindings mock_bindings(&host_resolver); |
637 | 637 |
638 host_resolver.rules()->AddSimulatedFailure("*"); | 638 host_resolver.rules()->AddSimulatedFailure("*"); |
639 | 639 |
640 scoped_ptr<ProxyResolverV8Tracing> resolver = | 640 scoped_ptr<ProxyResolverV8Tracing> resolver = |
641 CreateResolver(mock_bindings.CreateBindings(), "error.js"); | 641 CreateResolver(mock_bindings.CreateBindings(), "error.js"); |
642 | 642 |
643 ProxyInfo proxy_info1; | 643 ProxyInfo proxy_info1; |
644 ProxyInfo proxy_info2; | 644 ProxyInfo proxy_info2; |
645 ProxyResolver::RequestHandle request1; | 645 scoped_ptr<ProxyResolver::Request> request1; |
646 ProxyResolver::RequestHandle request2; | 646 scoped_ptr<ProxyResolver::Request> request2; |
647 TestCompletionCallback callback; | 647 TestCompletionCallback callback; |
648 | 648 |
649 resolver->GetProxyForURL(GURL("http://throw-an-error/"), &proxy_info1, | 649 resolver->GetProxyForURL(GURL("http://throw-an-error/"), &proxy_info1, |
650 base::Bind(&CrashCallback), &request1, | 650 base::Bind(&CrashCallback), &request1, |
651 mock_bindings.CreateBindings()); | 651 mock_bindings.CreateBindings()); |
652 | 652 |
653 // Wait until the first request has finished running on the worker thread. | 653 // Wait until the first request has finished running on the worker thread. |
654 // Cancel the first request, while it is running its completion task on | 654 // Cancel the first request, while it is running its completion task on |
655 // the origin thread. | 655 // the origin thread. |
656 mock_bindings.RunOnError(base::Bind(&ProxyResolverV8Tracing::CancelRequest, | 656 mock_bindings.RunOnError( |
657 base::Unretained(resolver.get()), | 657 base::Bind(&scoped_ptr<ProxyResolver::Request>::reset, |
658 request1)); | 658 base::Unretained(&request1), nullptr)); |
659 | 659 |
660 // Start another request, to make sure it is able to complete. | 660 // Start another request, to make sure it is able to complete. |
661 resolver->GetProxyForURL(GURL("http://i-have-no-idea-what-im-doing/"), | 661 resolver->GetProxyForURL(GURL("http://i-have-no-idea-what-im-doing/"), |
662 &proxy_info2, callback.callback(), &request2, | 662 &proxy_info2, callback.callback(), &request2, |
663 mock_bindings.CreateBindings()); | 663 mock_bindings.CreateBindings()); |
664 | 664 |
665 EXPECT_EQ(OK, callback.WaitForResult()); | 665 EXPECT_EQ(OK, callback.WaitForResult()); |
666 | 666 |
667 EXPECT_EQ("i-approve-this-message:42", proxy_info2.proxy_server().ToURI()); | 667 EXPECT_EQ("i-approve-this-message:42", proxy_info2.proxy_server().ToURI()); |
668 } | 668 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
738 // when the request has an outstanding DNS request in flight. | 738 // when the request has an outstanding DNS request in flight. |
739 TEST_F(ProxyResolverV8TracingTest, CancelWhileOutstandingNonBlockingDns) { | 739 TEST_F(ProxyResolverV8TracingTest, CancelWhileOutstandingNonBlockingDns) { |
740 BlockableHostResolver host_resolver; | 740 BlockableHostResolver host_resolver; |
741 MockBindings mock_bindings(&host_resolver); | 741 MockBindings mock_bindings(&host_resolver); |
742 | 742 |
743 scoped_ptr<ProxyResolverV8Tracing> resolver = | 743 scoped_ptr<ProxyResolverV8Tracing> resolver = |
744 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); | 744 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
745 | 745 |
746 ProxyInfo proxy_info1; | 746 ProxyInfo proxy_info1; |
747 ProxyInfo proxy_info2; | 747 ProxyInfo proxy_info2; |
748 ProxyResolver::RequestHandle request1; | 748 scoped_ptr<ProxyResolver::Request> request1; |
749 ProxyResolver::RequestHandle request2; | 749 scoped_ptr<ProxyResolver::Request> request2; |
750 | 750 |
751 resolver->GetProxyForURL(GURL("http://foo/req1"), &proxy_info1, | 751 resolver->GetProxyForURL(GURL("http://foo/req1"), &proxy_info1, |
752 base::Bind(&CrashCallback), &request1, | 752 base::Bind(&CrashCallback), &request1, |
753 mock_bindings.CreateBindings()); | 753 mock_bindings.CreateBindings()); |
754 | 754 |
755 host_resolver.WaitUntilRequestIsReceived(); | 755 host_resolver.WaitUntilRequestIsReceived(); |
756 | 756 |
757 resolver->GetProxyForURL(GURL("http://foo/req2"), &proxy_info2, | 757 resolver->GetProxyForURL(GURL("http://foo/req2"), &proxy_info2, |
758 base::Bind(&CrashCallback), &request2, | 758 base::Bind(&CrashCallback), &request2, |
759 mock_bindings.CreateBindings()); | 759 mock_bindings.CreateBindings()); |
760 | 760 |
761 host_resolver.WaitUntilRequestIsReceived(); | 761 host_resolver.WaitUntilRequestIsReceived(); |
762 | 762 |
763 resolver->CancelRequest(request1); | 763 request1.reset(); |
764 resolver->CancelRequest(request2); | 764 request2.reset(); |
765 | 765 |
766 EXPECT_EQ(2, host_resolver.num_cancelled_requests()); | 766 EXPECT_EQ(2, host_resolver.num_cancelled_requests()); |
767 | 767 |
768 // After leaving this scope, the ProxyResolver is destroyed. | 768 // After leaving this scope, the ProxyResolver is destroyed. |
769 // This should not cause any problems, as the outstanding work | 769 // This should not cause any problems, as the outstanding work |
770 // should have been cancelled. | 770 // should have been cancelled. |
771 } | 771 } |
772 | 772 |
773 void CancelRequestAndPause(ProxyResolverV8Tracing* resolver, | 773 void CancelRequestAndPause(scoped_ptr<ProxyResolver::Request>* request) { |
774 ProxyResolver::RequestHandle request) { | 774 request->reset(); |
775 resolver->CancelRequest(request); | |
776 | 775 |
777 // Sleep for a little bit. This makes it more likely for the worker | 776 // Sleep for a little bit. This makes it more likely for the worker |
778 // thread to have returned from its call, and serves as a regression | 777 // thread to have returned from its call, and serves as a regression |
779 // test for http://crbug.com/173373. | 778 // test for http://crbug.com/173373. |
780 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); | 779 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); |
781 } | 780 } |
782 | 781 |
783 // In non-blocking mode, the worker thread actually does block for | 782 // In non-blocking mode, the worker thread actually does block for |
784 // a short time to see if the result is in the DNS cache. Test | 783 // a short time to see if the result is in the DNS cache. Test |
785 // cancellation while the worker thread is waiting on this event. | 784 // cancellation while the worker thread is waiting on this event. |
786 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns) { | 785 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns) { |
787 BlockableHostResolver host_resolver; | 786 BlockableHostResolver host_resolver; |
788 MockBindings mock_bindings(&host_resolver); | 787 MockBindings mock_bindings(&host_resolver); |
789 | 788 |
790 scoped_ptr<ProxyResolverV8Tracing> resolver = | 789 scoped_ptr<ProxyResolverV8Tracing> resolver = |
791 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); | 790 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
792 | 791 |
793 ProxyInfo proxy_info; | 792 ProxyInfo proxy_info; |
794 ProxyResolver::RequestHandle request; | 793 scoped_ptr<ProxyResolver::Request> request; |
795 | 794 |
796 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 795 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
797 base::Bind(&CrashCallback), &request, | 796 base::Bind(&CrashCallback), &request, |
798 mock_bindings.CreateBindings()); | 797 mock_bindings.CreateBindings()); |
799 | 798 |
800 host_resolver.SetAction( | 799 host_resolver.SetAction(base::Bind(CancelRequestAndPause, &request)); |
801 base::Bind(CancelRequestAndPause, resolver.get(), request)); | |
802 | 800 |
803 host_resolver.WaitUntilRequestIsReceived(); | 801 host_resolver.WaitUntilRequestIsReceived(); |
804 } | 802 } |
805 | 803 |
806 // Cancel the request while there is a pending DNS request, however before | 804 // Cancel the request while there is a pending DNS request, however before |
807 // the request is sent to the host resolver. | 805 // the request is sent to the host resolver. |
808 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns2) { | 806 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns2) { |
809 MockCachingHostResolver host_resolver; | 807 MockCachingHostResolver host_resolver; |
810 MockBindings mock_bindings(&host_resolver); | 808 MockBindings mock_bindings(&host_resolver); |
811 | 809 |
812 scoped_ptr<ProxyResolverV8Tracing> resolver = | 810 scoped_ptr<ProxyResolverV8Tracing> resolver = |
813 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); | 811 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
814 | 812 |
815 ProxyInfo proxy_info; | 813 ProxyInfo proxy_info; |
816 ProxyResolver::RequestHandle request; | 814 scoped_ptr<ProxyResolver::Request> request; |
817 | 815 |
818 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 816 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
819 base::Bind(&CrashCallback), &request, | 817 base::Bind(&CrashCallback), &request, |
820 mock_bindings.CreateBindings()); | 818 mock_bindings.CreateBindings()); |
821 | 819 |
822 // Wait a bit, so the DNS task has hopefully been posted. The test will | 820 // Wait a bit, so the DNS task has hopefully been posted. The test will |
823 // work whatever the delay is here, but it is most useful if the delay | 821 // work whatever the delay is here, but it is most useful if the delay |
824 // is large enough to allow a task to be posted back. | 822 // is large enough to allow a task to be posted back. |
825 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | 823 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
826 resolver->CancelRequest(request); | 824 request.reset(); |
827 | 825 |
828 EXPECT_EQ(0u, host_resolver.num_resolve()); | 826 EXPECT_EQ(0u, host_resolver.num_resolve()); |
829 } | 827 } |
830 | 828 |
831 TEST_F(ProxyResolverV8TracingTest, | 829 TEST_F(ProxyResolverV8TracingTest, |
832 CancelCreateResolverWhileOutstandingBlockingDns) { | 830 CancelCreateResolverWhileOutstandingBlockingDns) { |
833 BlockableHostResolver host_resolver; | 831 BlockableHostResolver host_resolver; |
834 MockBindings mock_bindings(&host_resolver); | 832 MockBindings mock_bindings(&host_resolver); |
835 | 833 |
836 scoped_ptr<ProxyResolverV8TracingFactory> factory( | 834 scoped_ptr<ProxyResolverV8TracingFactory> factory( |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1014 proxy_uri.substr(0, proxy_uri.find(':') + 1)); | 1012 proxy_uri.substr(0, proxy_uri.find(':') + 1)); |
1015 } else { | 1013 } else { |
1016 NOTREACHED(); | 1014 NOTREACHED(); |
1017 } | 1015 } |
1018 } | 1016 } |
1019 } | 1017 } |
1020 | 1018 |
1021 } // namespace | 1019 } // namespace |
1022 | 1020 |
1023 } // namespace net | 1021 } // namespace net |
OLD | NEW |