OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/message_loop_proxy.h" | 5 #include "base/message_loop_proxy.h" |
6 #include "base/synchronization/waitable_event.h" | 6 #include "base/synchronization/waitable_event.h" |
7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "chrome/common/net/url_fetcher.h" | 9 #include "chrome/common/net/url_fetcher.h" |
10 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
(...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 722 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
723 ASSERT_TRUE(test_server.Start()); | 723 ASSERT_TRUE(test_server.Start()); |
724 | 724 |
725 GURL url(test_server.GetURL("defaultresponse")); | 725 GURL url(test_server.GetURL("defaultresponse")); |
726 | 726 |
727 // Registers an entry for test url. It only allows 3 requests to be sent | 727 // Registers an entry for test url. It only allows 3 requests to be sent |
728 // in 200 milliseconds. | 728 // in 200 milliseconds. |
729 net::URLRequestThrottlerManager* manager = | 729 net::URLRequestThrottlerManager* manager = |
730 net::URLRequestThrottlerManager::GetInstance(); | 730 net::URLRequestThrottlerManager::GetInstance(); |
731 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 731 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
732 new net::URLRequestThrottlerEntry(manager, 200, 3, 1, 2.0, 0.0, 256)); | 732 new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256)); |
733 manager->OverrideEntryForTests(url, entry); | 733 manager->OverrideEntryForTests(url, entry); |
734 | 734 |
735 CreateFetcher(url); | 735 CreateFetcher(url); |
736 | 736 |
737 MessageLoop::current()->Run(); | 737 MessageLoop::current()->Run(); |
738 | 738 |
739 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); | 739 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); |
740 } | 740 } |
741 | 741 |
742 TEST_F(URLFetcherProtectTest, ServerUnavailable) { | 742 TEST_F(URLFetcherProtectTest, ServerUnavailable) { |
743 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 743 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
744 ASSERT_TRUE(test_server.Start()); | 744 ASSERT_TRUE(test_server.Start()); |
745 | 745 |
746 GURL url(test_server.GetURL("files/server-unavailable.html")); | 746 GURL url(test_server.GetURL("files/server-unavailable.html")); |
747 | 747 |
748 // Registers an entry for test url. The backoff time is calculated by: | 748 // Registers an entry for test url. The backoff time is calculated by: |
749 // new_backoff = 2.0 * old_backoff + 0 | 749 // new_backoff = 2.0 * old_backoff + 0 |
750 // and maximum backoff time is 256 milliseconds. | 750 // and maximum backoff time is 256 milliseconds. |
751 // Maximum retries allowed is set to 11. | 751 // Maximum retries allowed is set to 11. |
752 net::URLRequestThrottlerManager* manager = | 752 net::URLRequestThrottlerManager* manager = |
753 net::URLRequestThrottlerManager::GetInstance(); | 753 net::URLRequestThrottlerManager::GetInstance(); |
754 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 754 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
755 new net::URLRequestThrottlerEntry(manager, 200, 3, 1, 2.0, 0.0, 256)); | 755 new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256)); |
756 manager->OverrideEntryForTests(url, entry); | 756 manager->OverrideEntryForTests(url, entry); |
757 | 757 |
758 CreateFetcher(url); | 758 CreateFetcher(url); |
759 | 759 |
760 MessageLoop::current()->Run(); | 760 MessageLoop::current()->Run(); |
761 | 761 |
762 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); | 762 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); |
763 } | 763 } |
764 | 764 |
765 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { | 765 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { |
766 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 766 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
767 ASSERT_TRUE(test_server.Start()); | 767 ASSERT_TRUE(test_server.Start()); |
768 | 768 |
769 GURL url(test_server.GetURL("files/server-unavailable.html")); | 769 GURL url(test_server.GetURL("files/server-unavailable.html")); |
770 | 770 |
771 // Registers an entry for test url. The backoff time is calculated by: | 771 // Registers an entry for test url. The backoff time is calculated by: |
772 // new_backoff = 2.0 * old_backoff + 0 | 772 // new_backoff = 2.0 * old_backoff + 0 |
773 // and maximum backoff time is 150000 milliseconds. | 773 // and maximum backoff time is 150000 milliseconds. |
774 // Maximum retries allowed is set to 11. | 774 // Maximum retries allowed is set to 11. |
775 net::URLRequestThrottlerManager* manager = | 775 net::URLRequestThrottlerManager* manager = |
776 net::URLRequestThrottlerManager::GetInstance(); | 776 net::URLRequestThrottlerManager::GetInstance(); |
777 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 777 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
778 new net::URLRequestThrottlerEntry( | 778 new net::URLRequestThrottlerEntry( |
779 manager, 200, 3, 100, 2.0, 0.0, 150000)); | 779 manager, "", 200, 3, 100, 2.0, 0.0, 150000)); |
780 // Total time if *not* for not doing automatic backoff would be 150s. | 780 // Total time if *not* for not doing automatic backoff would be 150s. |
781 // In reality it should be "as soon as server responds". | 781 // In reality it should be "as soon as server responds". |
782 manager->OverrideEntryForTests(url, entry); | 782 manager->OverrideEntryForTests(url, entry); |
783 | 783 |
784 CreateFetcher(url); | 784 CreateFetcher(url); |
785 | 785 |
786 MessageLoop::current()->Run(); | 786 MessageLoop::current()->Run(); |
787 | 787 |
788 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); | 788 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); |
789 } | 789 } |
(...skipping 25 matching lines...) Expand all Loading... |
815 GURL url(test_server.GetURL("files/server-unavailable.html")); | 815 GURL url(test_server.GetURL("files/server-unavailable.html")); |
816 | 816 |
817 // Registers an entry for test url. The backoff time is calculated by: | 817 // Registers an entry for test url. The backoff time is calculated by: |
818 // new_backoff = 2.0 * old_backoff +0 | 818 // new_backoff = 2.0 * old_backoff +0 |
819 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 819 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
820 // Maximum retries allowed is set to 2. | 820 // Maximum retries allowed is set to 2. |
821 net::URLRequestThrottlerManager* manager = | 821 net::URLRequestThrottlerManager* manager = |
822 net::URLRequestThrottlerManager::GetInstance(); | 822 net::URLRequestThrottlerManager::GetInstance(); |
823 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 823 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
824 new net::URLRequestThrottlerEntry( | 824 new net::URLRequestThrottlerEntry( |
825 manager, 200, 3, 2000, 2.0, 0.0, 4000)); | 825 manager, "", 200, 3, 2000, 2.0, 0.0, 4000)); |
826 manager->OverrideEntryForTests(url, entry); | 826 manager->OverrideEntryForTests(url, entry); |
827 | 827 |
828 // Create a separate thread that will create the URLFetcher. The current | 828 // Create a separate thread that will create the URLFetcher. The current |
829 // (main) thread will do the IO, and when the fetch is complete it will | 829 // (main) thread will do the IO, and when the fetch is complete it will |
830 // terminate the main thread's message loop; then the other thread's | 830 // terminate the main thread's message loop; then the other thread's |
831 // message loop will be shut down automatically as the thread goes out of | 831 // message loop will be shut down automatically as the thread goes out of |
832 // scope. | 832 // scope. |
833 base::Thread t("URLFetcher test thread"); | 833 base::Thread t("URLFetcher test thread"); |
834 ASSERT_TRUE(t.Start()); | 834 ASSERT_TRUE(t.Start()); |
835 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); | 835 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); |
836 | 836 |
837 MessageLoop::current()->Run(); | 837 MessageLoop::current()->Run(); |
838 | 838 |
839 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); | 839 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); |
840 } | 840 } |
841 | 841 |
842 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { | 842 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { |
843 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 843 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
844 ASSERT_TRUE(test_server.Start()); | 844 ASSERT_TRUE(test_server.Start()); |
845 | 845 |
846 GURL url(test_server.GetURL("files/server-unavailable.html")); | 846 GURL url(test_server.GetURL("files/server-unavailable.html")); |
847 | 847 |
848 // Register an entry for test url. | 848 // Register an entry for test url. |
849 // Using a sliding window of 4 seconds, and max of 1 request, under a fast | 849 // Using a sliding window of 4 seconds, and max of 1 request, under a fast |
850 // run we expect to have a 4 second delay when posting the Start task. | 850 // run we expect to have a 4 second delay when posting the Start task. |
851 net::URLRequestThrottlerManager* manager = | 851 net::URLRequestThrottlerManager* manager = |
852 net::URLRequestThrottlerManager::GetInstance(); | 852 net::URLRequestThrottlerManager::GetInstance(); |
853 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 853 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
854 new net::URLRequestThrottlerEntry( | 854 new net::URLRequestThrottlerEntry( |
855 manager, 4000, 1, 2000, 2.0, 0.0, 4000)); | 855 manager, "", 4000, 1, 2000, 2.0, 0.0, 4000)); |
856 manager->OverrideEntryForTests(url, entry); | 856 manager->OverrideEntryForTests(url, entry); |
857 // Fake that a request has just started. | 857 // Fake that a request has just started. |
858 entry->ReserveSendingTimeForNextRequest(base::TimeTicks()); | 858 entry->ReserveSendingTimeForNextRequest(base::TimeTicks()); |
859 | 859 |
860 // The next request we try to send will be delayed by ~4 seconds. | 860 // The next request we try to send will be delayed by ~4 seconds. |
861 // The slower the test runs, the less the delay will be (since it takes the | 861 // The slower the test runs, the less the delay will be (since it takes the |
862 // time difference from now). | 862 // time difference from now). |
863 | 863 |
864 base::Thread t("URLFetcher test thread"); | 864 base::Thread t("URLFetcher test thread"); |
865 ASSERT_TRUE(t.Start()); | 865 ASSERT_TRUE(t.Start()); |
(...skipping 27 matching lines...) Expand all Loading... |
893 FROM_HERE, | 893 FROM_HERE, |
894 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current())); | 894 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current())); |
895 MessageLoop::current()->Run(); | 895 MessageLoop::current()->Run(); |
896 EXPECT_EQ(1, GetNumFetcherCores()); | 896 EXPECT_EQ(1, GetNumFetcherCores()); |
897 URLFetcher::CancelAll(); | 897 URLFetcher::CancelAll(); |
898 EXPECT_EQ(0, GetNumFetcherCores()); | 898 EXPECT_EQ(0, GetNumFetcherCores()); |
899 delete fetcher_; | 899 delete fetcher_; |
900 } | 900 } |
901 | 901 |
902 } // namespace. | 902 } // namespace. |
OLD | NEW |