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 <string> | 12 #include <string> |
12 | 13 |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "net/http/http_status_code.h" | 16 #include "net/http/http_status_code.h" |
17 #include "net/url_request/test_url_fetcher_factory.h" | 17 #include "net/url_request/test_url_fetcher_factory.h" |
18 #include "net/url_request/url_fetcher_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
19 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
24 namespace precache { | 24 namespace precache { |
(...skipping 16 matching lines...) Expand all Loading... |
41 MOCK_METHOD3(OnURLFetchDownloadProgress, | 41 MOCK_METHOD3(OnURLFetchDownloadProgress, |
42 void(const URLFetcher* source, int64_t current, int64_t total)); | 42 void(const URLFetcher* source, int64_t current, int64_t total)); |
43 MOCK_METHOD3(OnURLFetchUploadProgress, | 43 MOCK_METHOD3(OnURLFetchUploadProgress, |
44 void(const URLFetcher* source, int64_t current, int64_t total)); | 44 void(const URLFetcher* source, int64_t current, int64_t total)); |
45 }; | 45 }; |
46 | 46 |
47 TEST(FetcherPoolTest, AddDelete) { | 47 TEST(FetcherPoolTest, AddDelete) { |
48 // It also tests IsAvailable. | 48 // It also tests IsAvailable. |
49 base::MessageLoop loop; | 49 base::MessageLoop loop; |
50 MockURLFetcherDelegate delegate; | 50 MockURLFetcherDelegate delegate; |
51 scoped_ptr<URLFetcher> url_fetcher( | 51 std::unique_ptr<URLFetcher> url_fetcher( |
52 new FakeURLFetcher(GURL("http://a.com"), &delegate, "irrelevant", HTTP_OK, | 52 new FakeURLFetcher(GURL("http://a.com"), &delegate, "irrelevant", HTTP_OK, |
53 URLRequestStatus::SUCCESS)); | 53 URLRequestStatus::SUCCESS)); |
54 URLFetcher* url_fetcher_ptr = url_fetcher.get(); | 54 URLFetcher* url_fetcher_ptr = url_fetcher.get(); |
55 | 55 |
56 FetcherPool<URLFetcher> pool(1); | 56 FetcherPool<URLFetcher> pool(1); |
57 EXPECT_TRUE(pool.IsAvailable()); | 57 EXPECT_TRUE(pool.IsAvailable()); |
58 EXPECT_TRUE(pool.IsEmpty()); | 58 EXPECT_TRUE(pool.IsEmpty()); |
59 pool.Add(std::move(url_fetcher)); | 59 pool.Add(std::move(url_fetcher)); |
60 url_fetcher_ptr->Start(); | 60 url_fetcher_ptr->Start(); |
61 EXPECT_FALSE(pool.IsAvailable()); | 61 EXPECT_FALSE(pool.IsAvailable()); |
62 EXPECT_FALSE(pool.IsEmpty()); | 62 EXPECT_FALSE(pool.IsEmpty()); |
63 EXPECT_CALL(delegate, OnURLFetchComplete(url_fetcher_ptr)); | 63 EXPECT_CALL(delegate, OnURLFetchComplete(url_fetcher_ptr)); |
64 | 64 |
65 loop.RunUntilIdle(); | 65 loop.RunUntilIdle(); |
66 | 66 |
67 pool.Delete(*url_fetcher_ptr); | 67 pool.Delete(*url_fetcher_ptr); |
68 EXPECT_TRUE(pool.IsEmpty()); | 68 EXPECT_TRUE(pool.IsEmpty()); |
69 EXPECT_TRUE(pool.IsAvailable()); | 69 EXPECT_TRUE(pool.IsAvailable()); |
70 } | 70 } |
71 | 71 |
72 TEST(FetcherPoolTest, Delete) { | 72 TEST(FetcherPoolTest, Delete) { |
73 const size_t kSize = 42; | 73 const size_t kSize = 42; |
74 base::MessageLoop loop; | 74 base::MessageLoop loop; |
75 MockURLFetcherDelegate delegate; | 75 MockURLFetcherDelegate delegate; |
76 scoped_ptr<URLFetcher> url_fetcher( | 76 std::unique_ptr<URLFetcher> url_fetcher( |
77 new FakeURLFetcher(GURL("http://a.com"), &delegate, "irrelevant", HTTP_OK, | 77 new FakeURLFetcher(GURL("http://a.com"), &delegate, "irrelevant", HTTP_OK, |
78 URLRequestStatus::SUCCESS)); | 78 URLRequestStatus::SUCCESS)); |
79 URLFetcher* url_fetcher_ptr = url_fetcher.get(); | 79 URLFetcher* url_fetcher_ptr = url_fetcher.get(); |
80 | 80 |
81 FetcherPool<URLFetcher> pool(kSize); | 81 FetcherPool<URLFetcher> pool(kSize); |
82 pool.Add(std::move(url_fetcher)); | 82 pool.Add(std::move(url_fetcher)); |
83 url_fetcher_ptr->Start(); | 83 url_fetcher_ptr->Start(); |
84 pool.Delete(*url_fetcher_ptr); | 84 pool.Delete(*url_fetcher_ptr); |
85 | 85 |
86 EXPECT_TRUE(pool.IsEmpty()); | 86 EXPECT_TRUE(pool.IsEmpty()); |
87 | 87 |
88 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); | 88 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); |
89 loop.RunUntilIdle(); | 89 loop.RunUntilIdle(); |
90 } | 90 } |
91 | 91 |
92 TEST(FetcherPoolTest, ParallelURLFetchers) { | 92 TEST(FetcherPoolTest, ParallelURLFetchers) { |
93 // It also tests IsEmpty. | 93 // It also tests IsEmpty. |
94 const size_t kSize = 42; | 94 const size_t kSize = 42; |
95 base::MessageLoop loop; | 95 base::MessageLoop loop; |
96 MockURLFetcherDelegate delegate; | 96 MockURLFetcherDelegate delegate; |
97 FetcherPool<URLFetcher> pool(kSize); | 97 FetcherPool<URLFetcher> pool(kSize); |
98 std::string urls[] = {"http://a.com", "http://b.com", "http://c.com"}; | 98 std::string urls[] = {"http://a.com", "http://b.com", "http://c.com"}; |
99 // To make sure that nothing slip through while setting the expectations. | 99 // To make sure that nothing slip through while setting the expectations. |
100 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); | 100 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); |
101 int num_requests_in_flight = 0; | 101 int num_requests_in_flight = 0; |
102 for (const auto& url : urls) { | 102 for (const auto& url : urls) { |
103 scoped_ptr<URLFetcher> url_fetcher( | 103 std::unique_ptr<URLFetcher> url_fetcher( |
104 new FakeURLFetcher(GURL(url), &delegate, "irrelevant", HTTP_OK, | 104 new FakeURLFetcher(GURL(url), &delegate, "irrelevant", HTTP_OK, |
105 URLRequestStatus::SUCCESS)); | 105 URLRequestStatus::SUCCESS)); |
106 num_requests_in_flight++; | 106 num_requests_in_flight++; |
107 url_fetcher->Start(); | 107 url_fetcher->Start(); |
108 pool.Add(std::move(url_fetcher)); | 108 pool.Add(std::move(url_fetcher)); |
109 EXPECT_FALSE(pool.IsEmpty()); | 109 EXPECT_FALSE(pool.IsEmpty()); |
110 EXPECT_TRUE(pool.IsAvailable()); | 110 EXPECT_TRUE(pool.IsAvailable()); |
111 } | 111 } |
112 EXPECT_CALL(delegate, OnURLFetchComplete(_)) | 112 EXPECT_CALL(delegate, OnURLFetchComplete(_)) |
113 .Times(3) | 113 .Times(3) |
114 .WillRepeatedly(Invoke([&pool](const URLFetcher* fetcher) { | 114 .WillRepeatedly(Invoke([&pool](const URLFetcher* fetcher) { |
115 EXPECT_TRUE(fetcher); | 115 EXPECT_TRUE(fetcher); |
116 pool.Delete(*fetcher); | 116 pool.Delete(*fetcher); |
117 })); | 117 })); |
118 | 118 |
119 loop.RunUntilIdle(); | 119 loop.RunUntilIdle(); |
120 | 120 |
121 EXPECT_TRUE(pool.IsEmpty()); | 121 EXPECT_TRUE(pool.IsEmpty()); |
122 EXPECT_TRUE(pool.IsAvailable()); | 122 EXPECT_TRUE(pool.IsAvailable()); |
123 } | 123 } |
124 | 124 |
125 TEST(FetcherPoolTest, DeleteAll) { | 125 TEST(FetcherPoolTest, DeleteAll) { |
126 const size_t kSize = 42; | 126 const size_t kSize = 42; |
127 base::MessageLoop loop; | 127 base::MessageLoop loop; |
128 MockURLFetcherDelegate delegate; | 128 MockURLFetcherDelegate delegate; |
129 FetcherPool<URLFetcher> pool(kSize); | 129 FetcherPool<URLFetcher> pool(kSize); |
130 std::string urls[] = {"http://a.com", "http://b.com", "http://c.com"}; | 130 std::string urls[] = {"http://a.com", "http://b.com", "http://c.com"}; |
131 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); | 131 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); |
132 for (const auto& url : urls) { | 132 for (const auto& url : urls) { |
133 scoped_ptr<URLFetcher> url_fetcher( | 133 std::unique_ptr<URLFetcher> url_fetcher( |
134 new FakeURLFetcher(GURL(url), &delegate, "irrelevant", HTTP_OK, | 134 new FakeURLFetcher(GURL(url), &delegate, "irrelevant", HTTP_OK, |
135 URLRequestStatus::SUCCESS)); | 135 URLRequestStatus::SUCCESS)); |
136 url_fetcher->Start(); | 136 url_fetcher->Start(); |
137 pool.Add(std::move(url_fetcher)); | 137 pool.Add(std::move(url_fetcher)); |
138 } | 138 } |
139 | 139 |
140 pool.DeleteAll(); | 140 pool.DeleteAll(); |
141 | 141 |
142 loop.RunUntilIdle(); | 142 loop.RunUntilIdle(); |
143 | 143 |
144 EXPECT_TRUE(pool.IsEmpty()); | 144 EXPECT_TRUE(pool.IsEmpty()); |
145 EXPECT_TRUE(pool.IsAvailable()); | 145 EXPECT_TRUE(pool.IsAvailable()); |
146 } | 146 } |
147 | 147 |
148 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 148 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
149 | 149 |
150 TEST(FetcherPoolTest, AddTooManyURLFetchers) { | 150 TEST(FetcherPoolTest, AddTooManyURLFetchers) { |
151 MockURLFetcherDelegate delegate; | 151 MockURLFetcherDelegate delegate; |
152 FetcherPool<URLFetcher> pool(0); | 152 FetcherPool<URLFetcher> pool(0); |
153 scoped_ptr<URLFetcher> url_fetcher( | 153 std::unique_ptr<URLFetcher> url_fetcher( |
154 new FakeURLFetcher(GURL("http://queso.es"), &delegate, "irrelevant", | 154 new FakeURLFetcher(GURL("http://queso.es"), &delegate, "irrelevant", |
155 HTTP_OK, URLRequestStatus::SUCCESS)); | 155 HTTP_OK, URLRequestStatus::SUCCESS)); |
156 EXPECT_DEBUG_DEATH(pool.Add(std::move(url_fetcher)), | 156 EXPECT_DEBUG_DEATH(pool.Add(std::move(url_fetcher)), |
157 "FetcherPool size exceeded"); | 157 "FetcherPool size exceeded"); |
158 } | 158 } |
159 | 159 |
160 TEST(FetcherPoolTest, AddNullURLFetcher) { | 160 TEST(FetcherPoolTest, AddNullURLFetcher) { |
161 FetcherPool<URLFetcher> pool(1); | 161 FetcherPool<URLFetcher> pool(1); |
162 scoped_ptr<URLFetcher> null_ptr; | 162 std::unique_ptr<URLFetcher> null_ptr; |
163 EXPECT_DEBUG_DEATH(pool.Add(std::move(null_ptr)), "cannot be null"); | 163 EXPECT_DEBUG_DEATH(pool.Add(std::move(null_ptr)), "cannot be null"); |
164 } | 164 } |
165 | 165 |
166 TEST(FetcherPoolTest, DeleteUnregisteredURLFetcher) { | 166 TEST(FetcherPoolTest, DeleteUnregisteredURLFetcher) { |
167 MockURLFetcherDelegate delegate; | 167 MockURLFetcherDelegate delegate; |
168 FetcherPool<URLFetcher> pool(1); | 168 FetcherPool<URLFetcher> pool(1); |
169 FakeURLFetcher url_fetcher(GURL("http://queso.es"), &delegate, "irrelevant", | 169 FakeURLFetcher url_fetcher(GURL("http://queso.es"), &delegate, "irrelevant", |
170 HTTP_OK, URLRequestStatus::SUCCESS); | 170 HTTP_OK, URLRequestStatus::SUCCESS); |
171 EXPECT_DEBUG_DEATH(pool.Delete(url_fetcher), | 171 EXPECT_DEBUG_DEATH(pool.Delete(url_fetcher), |
172 "doesn't contain the given element"); | 172 "doesn't contain the given element"); |
173 } | 173 } |
174 | 174 |
175 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 175 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
176 | 176 |
177 TEST(FetcherPoolTest, ExampleUsage) { | 177 TEST(FetcherPoolTest, ExampleUsage) { |
178 base::MessageLoop loop; | 178 base::MessageLoop loop; |
179 FetcherPool<URLFetcher> pool(2); | 179 FetcherPool<URLFetcher> pool(2); |
180 MockURLFetcherDelegate delegate; | 180 MockURLFetcherDelegate delegate; |
181 | 181 |
182 std::list<GURL> pending_urls{ | 182 std::list<GURL> pending_urls{ |
183 {GURL("http://a.com"), GURL("http://b.com"), GURL("http://c.com")}}; | 183 {GURL("http://a.com"), GURL("http://b.com"), GURL("http://c.com")}}; |
184 | 184 |
185 std::function<void()> start_next_batch = [&pending_urls, &pool, &delegate]() { | 185 std::function<void()> start_next_batch = [&pending_urls, &pool, &delegate]() { |
186 while (!pending_urls.empty() && pool.IsAvailable()) { | 186 while (!pending_urls.empty() && pool.IsAvailable()) { |
187 // Called CreateAndStartUrlFetcher in the documentation. | 187 // Called CreateAndStartUrlFetcher in the documentation. |
188 scoped_ptr<URLFetcher> fetcher( | 188 std::unique_ptr<URLFetcher> fetcher( |
189 new FakeURLFetcher(GURL(pending_urls.front()), &delegate, | 189 new FakeURLFetcher(GURL(pending_urls.front()), &delegate, |
190 "irrelevant", HTTP_OK, URLRequestStatus::SUCCESS)); | 190 "irrelevant", HTTP_OK, URLRequestStatus::SUCCESS)); |
191 fetcher->Start(); | 191 fetcher->Start(); |
192 pending_urls.pop_front(); | 192 pending_urls.pop_front(); |
193 pool.Add(std::move(fetcher)); | 193 pool.Add(std::move(fetcher)); |
194 } | 194 } |
195 }; | 195 }; |
196 | 196 |
197 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); // 3 and no more. | 197 EXPECT_CALL(delegate, OnURLFetchComplete(_)).Times(0); // 3 and no more. |
198 EXPECT_CALL(delegate, OnURLFetchComplete(_)) | 198 EXPECT_CALL(delegate, OnURLFetchComplete(_)) |
(...skipping 11 matching lines...) Expand all Loading... |
210 | 210 |
211 loop.RunUntilIdle(); | 211 loop.RunUntilIdle(); |
212 | 212 |
213 EXPECT_TRUE(pool.IsEmpty()); | 213 EXPECT_TRUE(pool.IsEmpty()); |
214 EXPECT_TRUE(pool.IsAvailable()); | 214 EXPECT_TRUE(pool.IsAvailable()); |
215 } | 215 } |
216 | 216 |
217 } // namespace | 217 } // namespace |
218 | 218 |
219 } // namespace precache | 219 } // namespace precache |
OLD | NEW |