Chromium Code Reviews| 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/offline_pages/background/request_picker.h" | 5 #include "components/offline_pages/background/request_picker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/test/test_simple_task_runner.h" | 8 #include "base/test/test_simple_task_runner.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "components/offline_pages/background/device_conditions.h" | 11 #include "components/offline_pages/background/device_conditions.h" |
| 12 #include "components/offline_pages/background/request_queue.h" | 12 #include "components/offline_pages/background/request_queue.h" |
| 13 #include "components/offline_pages/background/request_queue_in_memory_store.h" | 13 #include "components/offline_pages/background/request_queue_in_memory_store.h" |
| 14 #include "components/offline_pages/background/save_page_request.h" | 14 #include "components/offline_pages/background/save_page_request.h" |
| 15 #include "components/offline_pages/offline_page_item.h" | 15 #include "components/offline_pages/offline_page_item.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace offline_pages { | 18 namespace offline_pages { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 // Data for request 1. | 21 // Data for request 1. |
| 22 const int64_t kRequestId1 = 17; | 22 const int64_t kRequestId1 = 17; |
| 23 const GURL kUrl1("https://google.com"); | 23 const GURL kUrl1("https://google.com"); |
| 24 const ClientId kClientId1("bookmark", "1234"); | 24 const ClientId kClientId1("bookmark", "1234"); |
| 25 // Data for request 2. | 25 // Data for request 2. |
| 26 const int64_t kRequestId2 = 42; | 26 const int64_t kRequestId2 = 42; |
| 27 const GURL kUrl2("http://nytimes.com"); | 27 const GURL kUrl2("http://nytimes.com"); |
| 28 const ClientId kClientId2("bookmark", "5678"); | 28 const ClientId kClientId2("bookmark", "5678"); |
| 29 const bool kUserRequested = true; | 29 const bool kUserRequested = true; |
| 30 const int kRetryCount2 = 1; | |
| 31 | |
| 32 // Constants for policy values | |
| 33 const bool kPreferUntried = false; | |
|
fgorski
2016/07/25 16:22:57
nit: Did you intentionally set this to false? The
Pete Williamson
2016/07/25 19:54:21
Your interpretation is correct, so setting kPrefer
fgorski
2016/07/26 16:39:14
Ack. This is my OCD around positive sounding const
| |
| 34 const bool kPreferEarlier = true; | |
| 35 const bool kPreferRetryCount = true; | |
| 30 } // namespace | 36 } // namespace |
| 31 | 37 |
| 32 class RequestPickerTest : public testing::Test { | 38 class RequestPickerTest : public testing::Test { |
| 33 public: | 39 public: |
| 34 RequestPickerTest(); | 40 RequestPickerTest(); |
| 35 | 41 |
| 36 ~RequestPickerTest() override; | 42 ~RequestPickerTest() override; |
| 37 | 43 |
| 38 void SetUp() override; | 44 void SetUp() override; |
| 39 | 45 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 const SavePageRequest& request) {} | 89 const SavePageRequest& request) {} |
| 84 | 90 |
| 85 void RequestPickerTest::RequestPicked(const SavePageRequest& request) { | 91 void RequestPickerTest::RequestPicked(const SavePageRequest& request) { |
| 86 last_picked_.reset(new SavePageRequest(request)); | 92 last_picked_.reset(new SavePageRequest(request)); |
| 87 } | 93 } |
| 88 | 94 |
| 89 void RequestPickerTest::RequestQueueEmpty() { | 95 void RequestPickerTest::RequestQueueEmpty() { |
| 90 request_queue_empty_called_ = true; | 96 request_queue_empty_called_ = true; |
| 91 } | 97 } |
| 92 | 98 |
| 93 TEST_F(RequestPickerTest, ChooseNextRequest) { | 99 TEST_F(RequestPickerTest, PickFromEmptyQueue) { |
| 100 DeviceConditions conditions; | |
| 101 picker_->ChooseNextRequest( | |
| 102 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | |
| 103 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), | |
| 104 &conditions); | |
| 105 | |
| 106 // Pump the loop again to give the async queue the opportunity to return | |
| 107 // results from the Get operation, and for the picker to call the "QueueEmpty" | |
| 108 // callback. | |
| 109 PumpLoop(); | |
| 110 | |
| 111 EXPECT_TRUE(request_queue_empty_called_); | |
| 112 } | |
| 113 | |
| 114 TEST_F(RequestPickerTest, ChooseRequestWithHigherRetryCount) { | |
| 94 base::Time creation_time = base::Time::Now(); | 115 base::Time creation_time = base::Time::Now(); |
| 95 DeviceConditions conditions; | 116 DeviceConditions conditions; |
| 96 SavePageRequest request1( | 117 SavePageRequest request1( |
| 97 kRequestId1, kUrl1, kClientId1, creation_time, kUserRequested); | 118 kRequestId1, kUrl1, kClientId1, creation_time, kUserRequested); |
| 98 SavePageRequest request2( | 119 SavePageRequest request2( |
| 99 kRequestId2, kUrl2, kClientId2, creation_time, kUserRequested); | 120 kRequestId2, kUrl2, kClientId2, creation_time, kUserRequested); |
| 121 request2.set_attempt_count(kRetryCount2); | |
|
fgorski
2016/07/25 16:22:57
nit: consider renaming kRetryCount2 to kAttemptCou
Pete Williamson
2016/07/25 19:54:21
Done.
| |
| 100 // Put some test requests on the Queue. | 122 // Put some test requests on the Queue. |
| 101 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, | 123 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, |
| 102 base::Unretained(this))); | 124 base::Unretained(this))); |
| 103 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, | 125 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, |
| 104 base::Unretained(this))); | 126 base::Unretained(this))); |
| 105 | 127 |
| 106 // Pump the loop to give the async queue the opportunity to do the adds. | 128 // Pump the loop to give the async queue the opportunity to do the adds. |
| 107 PumpLoop(); | 129 PumpLoop(); |
| 108 | 130 |
| 109 picker_->ChooseNextRequest( | 131 picker_->ChooseNextRequest( |
| 110 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | 132 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), |
| 111 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), | 133 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), |
| 112 &conditions); | 134 &conditions); |
| 113 | 135 |
| 114 // Pump the loop again to give the async queue the opportunity to return | 136 // Pump the loop again to give the async queue the opportunity to return |
| 115 // results from the Get operation, and for the picker to call the "picked" | 137 // results from the Get operation, and for the picker to call the "picked" |
| 116 // callback. | 138 // callback. |
| 117 PumpLoop(); | 139 PumpLoop(); |
| 118 | 140 |
| 119 EXPECT_EQ(kRequestId2, last_picked_->request_id()); | 141 EXPECT_EQ(kRequestId2, last_picked_->request_id()); |
| 120 EXPECT_FALSE(request_queue_empty_called_); | 142 EXPECT_FALSE(request_queue_empty_called_); |
| 121 } | 143 } |
| 122 | 144 |
| 123 TEST_F(RequestPickerTest, PickFromEmptyQueue) { | 145 TEST_F(RequestPickerTest, ChooseRequestWithSameRetryCountButEarlier) { |
| 146 base::Time creation_time1 = | |
| 147 base::Time::Now() - base::TimeDelta::FromSeconds(10); | |
| 148 base::Time creation_time2 = base::Time::Now(); | |
| 124 DeviceConditions conditions; | 149 DeviceConditions conditions; |
| 150 SavePageRequest request1( | |
| 151 kRequestId1, kUrl1, kClientId1, creation_time1, kUserRequested); | |
| 152 SavePageRequest request2( | |
| 153 kRequestId2, kUrl2, kClientId2, creation_time2, kUserRequested); | |
| 154 // Put some test requests on the Queue. | |
|
dougarnett
2016/07/25 18:32:10
"Put some" => "Add ... to ..." here and other ca
Pete Williamson
2016/07/25 19:54:21
Done.
| |
| 155 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 156 base::Unretained(this))); | |
| 157 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 158 base::Unretained(this))); | |
| 159 | |
| 160 // Pump the loop to give the async queue the opportunity to do the adds. | |
| 161 PumpLoop(); | |
| 162 | |
| 125 picker_->ChooseNextRequest( | 163 picker_->ChooseNextRequest( |
| 126 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | 164 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), |
| 127 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), | 165 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), |
| 128 &conditions); | 166 &conditions); |
| 129 | 167 |
| 130 // Pump the loop again to give the async queue the opportunity to return | 168 // Pump the loop again to give the async queue the opportunity to return |
| 131 // results from the Get operation, and for the picker to call the "QueueEmpty" | 169 // results from the Get operation, and for the picker to call the "picked" |
| 132 // callback. | 170 // callback. |
| 133 PumpLoop(); | 171 PumpLoop(); |
| 134 | 172 |
| 135 EXPECT_TRUE(request_queue_empty_called_); | 173 EXPECT_EQ(kRequestId1, last_picked_->request_id()); |
| 174 EXPECT_FALSE(request_queue_empty_called_); | |
| 175 } | |
| 176 | |
| 177 TEST_F(RequestPickerTest, ChooseEarlierRequest) { | |
| 178 // We need a custom policy object prefering recency to retry count. | |
| 179 policy_.reset( | |
| 180 new OfflinerPolicy(kPreferUntried, kPreferEarlier, !kPreferRetryCount)); | |
| 181 picker_.reset(new RequestPicker(queue_.get(), policy_.get())); | |
| 182 | |
| 183 base::Time creation_time1 = | |
| 184 base::Time::Now() - base::TimeDelta::FromSeconds(10); | |
| 185 base::Time creation_time2 = base::Time::Now(); | |
| 186 DeviceConditions conditions; | |
| 187 SavePageRequest request1( | |
| 188 kRequestId1, kUrl1, kClientId1, creation_time1, kUserRequested); | |
| 189 SavePageRequest request2( | |
| 190 kRequestId2, kUrl2, kClientId2, creation_time2, kUserRequested); | |
| 191 request2.set_attempt_count(kRetryCount2); | |
| 192 // Put some test requests on the Queue. | |
| 193 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, | |
|
dougarnett
2016/07/25 18:32:10
This pattern of adding requests, pump loop, choose
Pete Williamson
2016/07/25 19:54:21
Done.
| |
| 194 base::Unretained(this))); | |
| 195 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 196 base::Unretained(this))); | |
| 197 | |
| 198 // Pump the loop to give the async queue the opportunity to do the adds. | |
| 199 PumpLoop(); | |
| 200 | |
| 201 picker_->ChooseNextRequest( | |
| 202 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | |
| 203 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), | |
| 204 &conditions); | |
| 205 | |
| 206 // Pump the loop again to give the async queue the opportunity to return | |
| 207 // results from the Get operation, and for the picker to call the "picked" | |
| 208 // callback. | |
| 209 PumpLoop(); | |
| 210 | |
| 211 EXPECT_EQ(kRequestId1, last_picked_->request_id()); | |
| 212 EXPECT_FALSE(request_queue_empty_called_); | |
| 213 } | |
| 214 | |
| 215 TEST_F(RequestPickerTest, ChooseSameTimeRequestWithHigherRetryCount) { | |
| 216 // We need a custom policy object preferring recency to retry count. | |
| 217 policy_.reset( | |
| 218 new OfflinerPolicy(kPreferUntried, kPreferEarlier, !kPreferRetryCount)); | |
| 219 picker_.reset(new RequestPicker(queue_.get(), policy_.get())); | |
| 220 | |
| 221 base::Time creation_time = base::Time::Now(); | |
| 222 DeviceConditions conditions; | |
| 223 SavePageRequest request1( | |
| 224 kRequestId1, kUrl1, kClientId1, creation_time, kUserRequested); | |
| 225 SavePageRequest request2( | |
| 226 kRequestId2, kUrl2, kClientId2, creation_time, kUserRequested); | |
| 227 request2.set_attempt_count(kRetryCount2); | |
| 228 | |
| 229 // Put some test requests on the Queue. | |
| 230 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 231 base::Unretained(this))); | |
| 232 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 233 base::Unretained(this))); | |
| 234 | |
| 235 // Pump the loop to give the async queue the opportunity to do the adds. | |
| 236 PumpLoop(); | |
| 237 | |
| 238 picker_->ChooseNextRequest( | |
| 239 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | |
| 240 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), | |
| 241 &conditions); | |
| 242 | |
| 243 // Pump the loop again to give the async queue the opportunity to return | |
| 244 // results from the Get operation, and for the picker to call the "picked" | |
| 245 // callback. | |
| 246 PumpLoop(); | |
| 247 | |
| 248 EXPECT_EQ(kRequestId2, last_picked_->request_id()); | |
| 249 EXPECT_FALSE(request_queue_empty_called_); | |
| 250 } | |
| 251 | |
| 252 TEST_F(RequestPickerTest, ChooseRequestWithLowerRetryCount) { | |
| 253 // We need a custom policy object preferring lower retry count. | |
| 254 policy_.reset( | |
| 255 new OfflinerPolicy(!kPreferUntried, kPreferEarlier, kPreferRetryCount)); | |
| 256 picker_.reset(new RequestPicker(queue_.get(), policy_.get())); | |
| 257 | |
| 258 base::Time creation_time = base::Time::Now(); | |
| 259 DeviceConditions conditions; | |
| 260 SavePageRequest request1( | |
| 261 kRequestId1, kUrl1, kClientId1, creation_time, kUserRequested); | |
| 262 SavePageRequest request2( | |
| 263 kRequestId2, kUrl2, kClientId2, creation_time, kUserRequested); | |
| 264 request2.set_attempt_count(kRetryCount2); | |
| 265 // Put some test requests on the Queue. | |
| 266 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 267 base::Unretained(this))); | |
| 268 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 269 base::Unretained(this))); | |
| 270 | |
| 271 // Pump the loop to give the async queue the opportunity to do the adds. | |
| 272 PumpLoop(); | |
| 273 | |
| 274 picker_->ChooseNextRequest( | |
| 275 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | |
| 276 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), | |
| 277 &conditions); | |
| 278 | |
| 279 // Pump the loop again to give the async queue the opportunity to return | |
| 280 // results from the Get operation, and for the picker to call the "picked" | |
| 281 // callback. | |
| 282 PumpLoop(); | |
| 283 | |
| 284 EXPECT_EQ(kRequestId1, last_picked_->request_id()); | |
| 285 EXPECT_FALSE(request_queue_empty_called_); | |
| 286 } | |
| 287 | |
| 288 TEST_F(RequestPickerTest, ChooseLaterRequest) { | |
| 289 // We need a custom policy preferring recency over retry, and later requests. | |
| 290 policy_.reset( | |
| 291 new OfflinerPolicy(kPreferUntried, !kPreferEarlier, !kPreferRetryCount)); | |
| 292 picker_.reset(new RequestPicker(queue_.get(), policy_.get())); | |
| 293 | |
| 294 base::Time creation_time1 = | |
| 295 base::Time::Now() - base::TimeDelta::FromSeconds(10); | |
| 296 base::Time creation_time2 = base::Time::Now(); | |
| 297 DeviceConditions conditions; | |
| 298 SavePageRequest request1( | |
| 299 kRequestId1, kUrl1, kClientId1, creation_time1, kUserRequested); | |
| 300 SavePageRequest request2( | |
| 301 kRequestId2, kUrl2, kClientId2, creation_time2, kUserRequested); | |
| 302 // Put some test requests on the Queue. | |
| 303 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 304 base::Unretained(this))); | |
| 305 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, | |
| 306 base::Unretained(this))); | |
| 307 | |
| 308 // Pump the loop to give the async queue the opportunity to do the adds. | |
| 309 PumpLoop(); | |
| 310 | |
| 311 picker_->ChooseNextRequest( | |
| 312 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | |
| 313 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), | |
| 314 &conditions); | |
| 315 | |
| 316 // Pump the loop again to give the async queue the opportunity to return | |
| 317 // results from the Get operation, and for the picker to call the "picked" | |
| 318 // callback. | |
| 319 PumpLoop(); | |
| 320 | |
| 321 EXPECT_EQ(kRequestId2, last_picked_->request_id()); | |
| 322 EXPECT_FALSE(request_queue_empty_called_); | |
| 136 } | 323 } |
| 137 | 324 |
| 138 } // namespace offline_pages | 325 } // namespace offline_pages |
| OLD | NEW |