| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/precache/core/fetcher_pool.h" | 5 #include "components/precache/core/fetcher_pool.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <array> | 8 #include <array> |
| 9 #include <functional> | 9 #include <functional> |
| 10 #include <list> | 10 #include <list> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/run_loop.h" |
| 16 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 17 #include "net/url_request/test_url_fetcher_factory.h" | 18 #include "net/url_request/test_url_fetcher_factory.h" |
| 18 #include "net/url_request/url_fetcher_delegate.h" | 19 #include "net/url_request/url_fetcher_delegate.h" |
| 19 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 namespace precache { | 25 namespace precache { |
| 25 | 26 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 55 | 56 |
| 56 FetcherPool<URLFetcher> pool(1); | 57 FetcherPool<URLFetcher> pool(1); |
| 57 EXPECT_TRUE(pool.IsAvailable()); | 58 EXPECT_TRUE(pool.IsAvailable()); |
| 58 EXPECT_TRUE(pool.IsEmpty()); | 59 EXPECT_TRUE(pool.IsEmpty()); |
| 59 pool.Add(std::move(url_fetcher)); | 60 pool.Add(std::move(url_fetcher)); |
| 60 url_fetcher_ptr->Start(); | 61 url_fetcher_ptr->Start(); |
| 61 EXPECT_FALSE(pool.IsAvailable()); | 62 EXPECT_FALSE(pool.IsAvailable()); |
| 62 EXPECT_FALSE(pool.IsEmpty()); | 63 EXPECT_FALSE(pool.IsEmpty()); |
| 63 EXPECT_CALL(delegate, OnURLFetchComplete(url_fetcher_ptr)); | 64 EXPECT_CALL(delegate, OnURLFetchComplete(url_fetcher_ptr)); |
| 64 | 65 |
| 65 loop.RunUntilIdle(); | 66 base::RunLoop().RunUntilIdle(); |
| 66 | 67 |
| 67 pool.Delete(*url_fetcher_ptr); | 68 pool.Delete(*url_fetcher_ptr); |
| 68 EXPECT_TRUE(pool.IsEmpty()); | 69 EXPECT_TRUE(pool.IsEmpty()); |
| 69 EXPECT_TRUE(pool.IsAvailable()); | 70 EXPECT_TRUE(pool.IsAvailable()); |
| 70 } | 71 } |
| 71 | 72 |
| 72 TEST(FetcherPoolTest, Delete) { | 73 TEST(FetcherPoolTest, Delete) { |
| 73 const size_t kSize = 42; | 74 const size_t kSize = 42; |
| 74 base::MessageLoop loop; | 75 base::MessageLoop loop; |
| 75 MockURLFetcherDelegate delegate; | 76 MockURLFetcherDelegate delegate; |
| 76 std::unique_ptr<URLFetcher> url_fetcher( | 77 std::unique_ptr<URLFetcher> url_fetcher( |
| 77 new FakeURLFetcher(GURL("http://a.com"), &delegate, "irrelevant", HTTP_OK, | 78 new FakeURLFetcher(GURL("http://a.com"), &delegate, "irrelevant", HTTP_OK, |
| 78 URLRequestStatus::SUCCESS)); | 79 URLRequestStatus::SUCCESS)); |
| 79 URLFetcher* url_fetcher_ptr = url_fetcher.get(); | 80 URLFetcher* url_fetcher_ptr = url_fetcher.get(); |
| 80 | 81 |
| 81 FetcherPool<URLFetcher> pool(kSize); | 82 FetcherPool<URLFetcher> pool(kSize); |
| 82 pool.Add(std::move(url_fetcher)); | 83 pool.Add(std::move(url_fetcher)); |
| 83 url_fetcher_ptr->Start(); | 84 url_fetcher_ptr->Start(); |
| 84 pool.Delete(*url_fetcher_ptr); | 85 pool.Delete(*url_fetcher_ptr); |
| 85 | 86 |
| 86 EXPECT_TRUE(pool.IsEmpty()); | 87 EXPECT_TRUE(pool.IsEmpty()); |
| 87 | 88 |
| 88 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); | 89 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); |
| 89 loop.RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
| 90 } | 91 } |
| 91 | 92 |
| 92 TEST(FetcherPoolTest, ParallelURLFetchers) { | 93 TEST(FetcherPoolTest, ParallelURLFetchers) { |
| 93 // It also tests IsEmpty. | 94 // It also tests IsEmpty. |
| 94 const size_t kSize = 42; | 95 const size_t kSize = 42; |
| 95 base::MessageLoop loop; | 96 base::MessageLoop loop; |
| 96 MockURLFetcherDelegate delegate; | 97 MockURLFetcherDelegate delegate; |
| 97 FetcherPool<URLFetcher> pool(kSize); | 98 FetcherPool<URLFetcher> pool(kSize); |
| 98 std::string urls[] = {"http://a.com", "http://b.com", "http://c.com"}; | 99 std::string urls[] = {"http://a.com", "http://b.com", "http://c.com"}; |
| 99 // To make sure that nothing slip through while setting the expectations. | 100 // To make sure that nothing slip through while setting the expectations. |
| 100 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); | 101 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); |
| 101 int num_requests_in_flight = 0; | 102 int num_requests_in_flight = 0; |
| 102 for (const auto& url : urls) { | 103 for (const auto& url : urls) { |
| 103 std::unique_ptr<URLFetcher> url_fetcher( | 104 std::unique_ptr<URLFetcher> url_fetcher( |
| 104 new FakeURLFetcher(GURL(url), &delegate, "irrelevant", HTTP_OK, | 105 new FakeURLFetcher(GURL(url), &delegate, "irrelevant", HTTP_OK, |
| 105 URLRequestStatus::SUCCESS)); | 106 URLRequestStatus::SUCCESS)); |
| 106 num_requests_in_flight++; | 107 num_requests_in_flight++; |
| 107 url_fetcher->Start(); | 108 url_fetcher->Start(); |
| 108 pool.Add(std::move(url_fetcher)); | 109 pool.Add(std::move(url_fetcher)); |
| 109 EXPECT_FALSE(pool.IsEmpty()); | 110 EXPECT_FALSE(pool.IsEmpty()); |
| 110 EXPECT_TRUE(pool.IsAvailable()); | 111 EXPECT_TRUE(pool.IsAvailable()); |
| 111 } | 112 } |
| 112 EXPECT_CALL(delegate, OnURLFetchComplete(_)) | 113 EXPECT_CALL(delegate, OnURLFetchComplete(_)) |
| 113 .Times(3) | 114 .Times(3) |
| 114 .WillRepeatedly(Invoke([&pool](const URLFetcher* fetcher) { | 115 .WillRepeatedly(Invoke([&pool](const URLFetcher* fetcher) { |
| 115 EXPECT_TRUE(fetcher); | 116 EXPECT_TRUE(fetcher); |
| 116 pool.Delete(*fetcher); | 117 pool.Delete(*fetcher); |
| 117 })); | 118 })); |
| 118 | 119 |
| 119 loop.RunUntilIdle(); | 120 base::RunLoop().RunUntilIdle(); |
| 120 | 121 |
| 121 EXPECT_TRUE(pool.IsEmpty()); | 122 EXPECT_TRUE(pool.IsEmpty()); |
| 122 EXPECT_TRUE(pool.IsAvailable()); | 123 EXPECT_TRUE(pool.IsAvailable()); |
| 123 } | 124 } |
| 124 | 125 |
| 125 TEST(FetcherPoolTest, DeleteAll) { | 126 TEST(FetcherPoolTest, DeleteAll) { |
| 126 const size_t kSize = 42; | 127 const size_t kSize = 42; |
| 127 base::MessageLoop loop; | 128 base::MessageLoop loop; |
| 128 MockURLFetcherDelegate delegate; | 129 MockURLFetcherDelegate delegate; |
| 129 FetcherPool<URLFetcher> pool(kSize); | 130 FetcherPool<URLFetcher> pool(kSize); |
| 130 std::string urls[] = {"http://a.com", "http://b.com", "http://c.com"}; | 131 std::string urls[] = {"http://a.com", "http://b.com", "http://c.com"}; |
| 131 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); | 132 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); |
| 132 for (const auto& url : urls) { | 133 for (const auto& url : urls) { |
| 133 std::unique_ptr<URLFetcher> url_fetcher( | 134 std::unique_ptr<URLFetcher> url_fetcher( |
| 134 new FakeURLFetcher(GURL(url), &delegate, "irrelevant", HTTP_OK, | 135 new FakeURLFetcher(GURL(url), &delegate, "irrelevant", HTTP_OK, |
| 135 URLRequestStatus::SUCCESS)); | 136 URLRequestStatus::SUCCESS)); |
| 136 url_fetcher->Start(); | 137 url_fetcher->Start(); |
| 137 pool.Add(std::move(url_fetcher)); | 138 pool.Add(std::move(url_fetcher)); |
| 138 } | 139 } |
| 139 | 140 |
| 140 pool.DeleteAll(); | 141 pool.DeleteAll(); |
| 141 | 142 |
| 142 loop.RunUntilIdle(); | 143 base::RunLoop().RunUntilIdle(); |
| 143 | 144 |
| 144 EXPECT_TRUE(pool.IsEmpty()); | 145 EXPECT_TRUE(pool.IsEmpty()); |
| 145 EXPECT_TRUE(pool.IsAvailable()); | 146 EXPECT_TRUE(pool.IsAvailable()); |
| 146 } | 147 } |
| 147 | 148 |
| 148 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 149 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 149 | 150 |
| 150 TEST(FetcherPoolTest, AddTooManyURLFetchers) { | 151 TEST(FetcherPoolTest, AddTooManyURLFetchers) { |
| 151 MockURLFetcherDelegate delegate; | 152 MockURLFetcherDelegate delegate; |
| 152 FetcherPool<URLFetcher> pool(0); | 153 FetcherPool<URLFetcher> pool(0); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 Invoke([&pool, &start_next_batch](const URLFetcher* fetcher) { | 202 Invoke([&pool, &start_next_batch](const URLFetcher* fetcher) { |
| 202 EXPECT_TRUE(fetcher); | 203 EXPECT_TRUE(fetcher); |
| 203 pool.Delete(*fetcher); | 204 pool.Delete(*fetcher); |
| 204 start_next_batch(); | 205 start_next_batch(); |
| 205 })); | 206 })); |
| 206 | 207 |
| 207 start_next_batch(); | 208 start_next_batch(); |
| 208 EXPECT_FALSE(pool.IsEmpty()); | 209 EXPECT_FALSE(pool.IsEmpty()); |
| 209 EXPECT_FALSE(pool.IsAvailable()); | 210 EXPECT_FALSE(pool.IsAvailable()); |
| 210 | 211 |
| 211 loop.RunUntilIdle(); | 212 base::RunLoop().RunUntilIdle(); |
| 212 | 213 |
| 213 EXPECT_TRUE(pool.IsEmpty()); | 214 EXPECT_TRUE(pool.IsEmpty()); |
| 214 EXPECT_TRUE(pool.IsAvailable()); | 215 EXPECT_TRUE(pool.IsAvailable()); |
| 215 } | 216 } |
| 216 | 217 |
| 217 } // namespace | 218 } // namespace |
| 218 | 219 |
| 219 } // namespace precache | 220 } // namespace precache |
| OLD | NEW |